Tibor Karaszi : Trace, indexeshttp://sqlblog.com/blogs/tibor_karaszi/archive/tags/Trace/indexes/default.aspxTags: Trace, indexesenCommunityServer 2.1 SP2 (Build: 61129.1)Match those types!http://sqlblog.com/blogs/tibor_karaszi/archive/2009/04/28/match-those-types.aspxTue, 28 Apr 2009 10:30:00 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:13606TiborKaraszi17http://sqlblog.com/blogs/tibor_karaszi/comments/13606.aspxhttp://sqlblog.com/blogs/tibor_karaszi/commentrss.aspx?PostID=13606<P>This is a recommendation I believe is worth repeating from time to time: Make sure you match data types when you write TSQL code. Else you in most cases end up with an implicit data type conversion. And in worst case, this conversion is performed at the column side - not the literal side of your query. What does that mean? Consider below:</P>
<P>WHERE Col = Val</P>
<P>Now, say that the types for above don't match. Val might be some parameter (to a stored procedure, for instance), a variable or a written value (literal). In any case, when SQL Server need to do some operation )like comparison like here) involving several values which aren't of the same type, then one of the values need to be converted so it is of the same type as the other. Which one is determined by the rules for "<A title='Data Type Precendence"' href="http://msdn.microsoft.com/en-us/library/ms190309.aspx">Data Type Precedence</A>". </P>
<P>What we don't want is a conversion at the column side. Why? I would argue that an implicit conversion in the first place in many cases mean I misunderstood something about the types in my system and am on my way of producing a bug in my code. But having a more strict language is not the topic for this particular post (check out <A title=this href="http://sommarskog.se/wishlist.html#strictchecks">this</A>). My point here is that it is bad for performance reasons. Just yesterday and today I was <A title="involved in a thread" href="http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/76f22f8a-53b1-4a30-a741-14da5cf539f1/">involved in a thread</A> on the MSDN forum. Here's the repro script from that thread (slightly adjusted by me):</P>
<P><CODE style="FONT-SIZE:12px;"><SPAN style="COLOR:blue;">USE&nbsp;</SPAN><SPAN style="COLOR:black;">tempdb <BR>GO <BR></SPAN><SPAN style="COLOR:blue;">IF&nbsp;</SPAN><SPAN style="COLOR:magenta;">OBJECT_ID</SPAN><SPAN style="COLOR:gray;">(</SPAN><SPAN style="COLOR:red;">'t'</SPAN><SPAN style="COLOR:gray;">)&nbsp;</SPAN><SPAN style="COLOR:blue;">IS&nbsp;</SPAN><SPAN style="COLOR:gray;">NOT&nbsp;NULL&nbsp;</SPAN><SPAN style="COLOR:blue;">DROP&nbsp;TABLE&nbsp;</SPAN><SPAN style="COLOR:black;">t <BR>GO <BR></SPAN><SPAN style="COLOR:blue;">CREATE&nbsp;TABLE&nbsp;</SPAN><SPAN style="COLOR:black;">t&nbsp; <BR></SPAN><SPAN style="COLOR:gray;">( <BR>&nbsp;</SPAN><SPAN style="COLOR:black;">c1&nbsp;int</SPAN><SPAN style="COLOR:blue;">&nbsp;</SPAN><SPAN style="COLOR:#434343;">IDENTITY</SPAN><SPAN style="COLOR:gray;">(</SPAN><SPAN style="COLOR:black;">10000001</SPAN><SPAN style="COLOR:gray;">,</SPAN><SPAN style="COLOR:black;">1</SPAN><SPAN style="COLOR:gray;">)&nbsp;NOT&nbsp;NULL&nbsp; <BR>,</SPAN><SPAN style="COLOR:black;">c2&nbsp;</SPAN><SPAN style="COLOR:blue;">char</SPAN><SPAN style="COLOR:gray;">(</SPAN><SPAN style="COLOR:black;">8</SPAN><SPAN style="COLOR:gray;">)&nbsp;NULL <BR>,</SPAN><SPAN style="COLOR:black;">c3&nbsp;datetime&nbsp;</SPAN><SPAN style="COLOR:gray;">NULL <BR>) <BR></SPAN><SPAN style="COLOR:black;">GO <BR></SPAN><SPAN style="COLOR:blue;">INSERT&nbsp;INTO&nbsp;</SPAN><SPAN style="COLOR:black;">t</SPAN><SPAN style="COLOR:gray;">(</SPAN><SPAN style="COLOR:black;">c3</SPAN><SPAN style="COLOR:gray;">)&nbsp; <BR></SPAN><SPAN style="COLOR:blue;">SELECT&nbsp;TOP </SPAN><SPAN style="COLOR:black;">3000000</SPAN><SPAN style="COLOR:gray;">&nbsp;</SPAN><SPAN style="COLOR:red;">'20080203'&nbsp; <BR></SPAN><SPAN style="COLOR:blue;">FROM&nbsp;</SPAN><SPAN style="COLOR:black;">master..spt_values&nbsp;t1 <BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR:gray;">CROSS&nbsp;</SPAN><SPAN style="COLOR:blue;">JOIN&nbsp;</SPAN><SPAN style="COLOR:black;">master..spt_values&nbsp;t2 <BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR:gray;">CROSS&nbsp;</SPAN><SPAN style="COLOR:blue;">JOIN&nbsp;</SPAN><SPAN style="COLOR:black;">master..spt_values&nbsp;t3 <BR></SPAN><SPAN style="COLOR:blue;">UPDATE&nbsp;</SPAN><SPAN style="COLOR:black;">t&nbsp;</SPAN><SPAN style="COLOR:blue;">SET&nbsp;</SPAN><SPAN style="COLOR:black;">c2&nbsp;</SPAN><SPAN style="COLOR:blue;">=&nbsp;</SPAN><SPAN style="COLOR:magenta;">CAST</SPAN><SPAN style="COLOR:gray;">(</SPAN><SPAN style="COLOR:black;">c1&nbsp;</SPAN><SPAN style="COLOR:blue;">AS&nbsp;char</SPAN><SPAN style="COLOR:gray;">(</SPAN><SPAN style="COLOR:black;">8</SPAN><SPAN style="COLOR:gray;">)) <BR></SPAN><SPAN style="COLOR:blue;">CREATE&nbsp;UNIQUE&nbsp;CLUSTERED INDEX&nbsp;</SPAN><SPAN style="COLOR:black;">x&nbsp;</SPAN><SPAN style="COLOR:blue;">ON&nbsp;</SPAN><SPAN style="COLOR:black;">t</SPAN><SPAN style="COLOR:gray;">(</SPAN><SPAN style="COLOR:black;">c2</SPAN><SPAN style="COLOR:gray;">) <BR></SPAN><SPAN style="COLOR:black;">GO <BR><BR></SPAN><SPAN style="COLOR:blue;">SELECT&nbsp;</SPAN><SPAN style="COLOR:gray;">*&nbsp;</SPAN><SPAN style="COLOR:blue;">FROM&nbsp;</SPAN><SPAN style="COLOR:black;">t&nbsp;</SPAN><SPAN style="COLOR:blue;">WHERE&nbsp;</SPAN><SPAN style="COLOR:black;">c2&nbsp;</SPAN><SPAN style="COLOR:blue;">=&nbsp;</SPAN><SPAN style="COLOR:red;">N'10000009' <BR></SPAN><SPAN style="COLOR:blue;">SELECT&nbsp;</SPAN><SPAN style="COLOR:gray;">*&nbsp;</SPAN><SPAN style="COLOR:blue;">FROM&nbsp;</SPAN><SPAN style="COLOR:black;">t&nbsp;</SPAN><SPAN style="COLOR:blue;">WHERE&nbsp;</SPAN><SPAN style="COLOR:black;">c2&nbsp;</SPAN><SPAN style="COLOR:blue;">=&nbsp;</SPAN><SPAN style="COLOR:red;">N'10000008'</SPAN></CODE> </P>
<P>Don't bother too much about the first section. The relevant part here is that we have char(8) column with values which are unique (enforced by a unique index), on a table with three million rows.</P>
<P>Now, before we dig deeper: What is the problem with above queries? The type do not match! The type in the table is char(8), but&nbsp;literal is Unicode (the N before the string). Since nchar has higher precedence than char, then the column side needs to be converted to the value side. Not good. Try the code on SQL Server 2000 and you will see disaster. For me, STATISTICS IO gave 10755 logical reads for both queries, with Clustered Index Scan (which is the same as table scan). Hoover on the Clustered Index Scan icon and you will find "WHERE:(Convert([t].[c2])=[@1])". See how the conversion is done at the column side? This, my friend, mean that SQL Server cannot seek an index to find the rows. Ouch! That's just because we were a bit sloppy... Compare to below:</P><CODE style="FONT-SIZE:12px;"><SPAN style="COLOR:blue;">SELECT&nbsp;</SPAN><SPAN style="COLOR:gray;">*&nbsp;</SPAN><SPAN style="COLOR:blue;">FROM&nbsp;</SPAN><SPAN style="COLOR:black;">t&nbsp;</SPAN><SPAN style="COLOR:blue;">WHERE&nbsp;</SPAN><SPAN style="COLOR:black;">c2&nbsp;</SPAN><SPAN style="COLOR:blue;">=&nbsp;</SPAN><SPAN style="COLOR:red;">'10000009' <BR></SPAN><SPAN style="COLOR:blue;">SELECT&nbsp;</SPAN><SPAN style="COLOR:gray;">*&nbsp;</SPAN><SPAN style="COLOR:blue;">FROM&nbsp;</SPAN><SPAN style="COLOR:black;">t&nbsp;</SPAN><SPAN style="COLOR:blue;">WHERE&nbsp;</SPAN><SPAN style="COLOR:black;">c2&nbsp;</SPAN><SPAN style="COLOR:blue;">=&nbsp;</SPAN><SPAN style="COLOR:red;">'10000008'</SPAN></CODE>
<P>You will find for both two above we have Clustered Index Seek with only three logical reads. That is a good improvement.</P>
<P>OK, enough about SQL Server 2000, lets discuss 2008/2005. At first I couldn't see any difference between the two original queries (N'10000009' and N'10000008'). Both seemed to be instant, and the same execution plan for both. I even saved the execution plan as XML and used WinDiff to compare the xml files. No difference. But the poster said the "9" query was slower than the "8" query. I executed to text and had ctrl-E pressed for a while. And now I could see a difference. The flicker was quicker for the "8" version vs. the "9" version. I used Profiler and confirmed. The "9" version had consistently about 90000 microsecond duration where the "8" version had about 0 duration. This was also reflected in CPU usage, as shown by Profiler. I hoped to use Extended Events to find something deeper about this, but that didn't give me anything. Perhaps I didn't capture the relevant events, quite simply. X/Event experts are welcome to suggest event session configurations. :-)</P>
<P>My guess for the difference is in the execution plan. You will see an index seek for both. That is good, since SQL Server nowadays potentially can use an index even if you end up with an implicit conversion at the column side. But you will see a few more operators in addition to the index seek. See that Constant Scalar followed by a Compute Scalar? My guess is that the built-in internal function used inside the Compute Scalar operator is more expensive for N'10000009' compared to N'10000008'. You can see this function if you use SET STATISTICS PROFILE ON. See the GetRangeThroughConvert() function. My guess is that the answer is the cost for this function. (Search the Internet for things like "Constant Scan" and "GetRangeThroughConvert" for other blog post on this topic.)</P>
<P>The moral of the story? Here we have a cost which can be relevant in some cases, but we will only see this if we happen to execute our query with the right values and are very very observant. But we can eliminate all this mess this by using the right type for the column in the first place!</P><img src="http://sqlblog.com/aggbug.aspx?PostID=13606" width="1" height="1">indexesPerformanceToolsTraceTroubleshooting