Let's start with the example. Create table, populate it and create an index on varchar column.
Next, let's run 2 selects. First one with varchar parameter. Plan is perfect.
Second, let's use unicode parameter.
Oops – index scan. It does not look like the big deal – who is going to use unicode for varchar field search. But..
We deal with the client applications. Unfortunately, modern development languages implement String as the unicode. And if you don't specify the parameter type, it would be unicode.
As you can see, .Net application defines @UntypedParam as nvarchar(5). And it produces 21 times more reads. Another thing worth to mention – the size of the parameter matches the string length. As result, if next statement uses "100" as the parameter value, it would be nvarchar(3) – recompilation, potentially additional plan in the cache and so on.
Keep that in mind – always specify parameter type and size at the client application.
This entry was posted in Client Development, SQL Server 2005, SQL Server 2008, T-SQL on December 9, 2010 by Dmitri Korotkevitch.