<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>6.0 Best Programming Practices </title><link>http://blogs.msdn.com/sqlprogrammability/archive/2007/01/13/6-0-best-programming-practices.aspx</link><description>In this section we will outline some programming practices for efficient plan cache usage: 6.1 Client Side Parameterization of Queries If your application has repeated execution of the same query with only parameter values changing from query to another,</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: 6.0 Best Programming Practices </title><link>http://blogs.msdn.com/sqlprogrammability/archive/2007/01/13/6-0-best-programming-practices.aspx#1492736</link><pubDate>Fri, 19 Jan 2007 14:46:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1492736</guid><dc:creator>D@TPBS</dc:creator><description>&lt;p&gt;I tested the example in &amp;quot;6.5 Exact Match of High Re-use Adhoc Query Text&amp;quot; and found that changing the comment had no effect. Only one plan was cached with whatever the first comment was. Inserting extra white space did however create a new plan. Presumably each comment is treated as a single 'white space' token so the text of the comment is ignored, but adding a 2nd comment would count as changing white space.&lt;/p&gt;</description></item><item><title>re: 6.0 Best Programming Practices </title><link>http://blogs.msdn.com/sqlprogrammability/archive/2007/01/13/6-0-best-programming-practices.aspx#1494338</link><pubDate>Fri, 19 Jan 2007 22:27:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1494338</guid><dc:creator>sangeethashekar</dc:creator><description>&lt;p&gt;D@TPBS, &lt;/p&gt;
&lt;p&gt;Your comment is incorrect. Changing the comment text will amount to a different sql handle. The sql handle is an MD5 hash of the entire batch including comments. If use the query pasted below you will be able to see 2 entries in the cache with different sql handles. This is the behavior on SQL Server 2005 and 2000 also.&lt;/p&gt;
&lt;p&gt;What version of SQL Server do you have installed?&lt;/p&gt;
</description></item><item><title>re: 6.0 Best Programming Practices </title><link>http://blogs.msdn.com/sqlprogrammability/archive/2007/01/13/6-0-best-programming-practices.aspx#1552133</link><pubDate>Mon, 29 Jan 2007 19:28:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1552133</guid><dc:creator>D@TPBS</dc:creator><description>&lt;p&gt;@@version is:&lt;/p&gt;
&lt;p&gt;Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86) &amp;nbsp; Apr 14 2006 01:12:25 &amp;nbsp; Copyright (c) 1988-2005 Microsoft Corporation &amp;nbsp;Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 2) &lt;/p&gt;
&lt;p&gt;I executed this:&lt;/p&gt;
&lt;p&gt;use scratch&lt;/p&gt;
&lt;p&gt;---this is an example of an adhoc query&lt;/p&gt;
&lt;p&gt;if exists(select col1 from t1 where col2 &amp;gt; 5) declare @x int;&lt;/p&gt;
&lt;p&gt;go&lt;/p&gt;
&lt;p&gt; ---this is an example of an adhoc query with different comment&lt;/p&gt;
&lt;p&gt;if exists(select col1 from t1 where col2 &amp;gt; 5) declare @x int;&lt;/p&gt;
&lt;p&gt;go&lt;/p&gt;
&lt;p&gt;select text, sql_handle, plan_handle &lt;/p&gt;
&lt;p&gt;from sys.dm_exec_query_stats qs cross apply sys.dm_exec_sql_text(sql_handle)&lt;/p&gt;
&lt;p&gt;where text like '%---this%'&lt;/p&gt;
&lt;p&gt;go&lt;/p&gt;
&lt;p&gt;...And get this...&lt;/p&gt;
&lt;p&gt;text	sql_handle	plan_handle&lt;/p&gt;
&lt;p&gt; &amp;nbsp;select text, sql_handle, plan_handle &amp;nbsp; from sys.dm_exec_query_stats qs cross apply sys.dm_exec_sql_text(sql_handle) &amp;nbsp;where text like '%---this%' &amp;nbsp;	0x02000000247B772F7B42AAA94F7A0A7220E383AA855AEDAC	0x06000F00247B772FB841920A000000000000000000000000&lt;/p&gt;
&lt;p&gt;use scratch &amp;nbsp; &amp;nbsp;---this is an example of an adhoc query &amp;nbsp;if exists(select col1 from t1 where col2 &amp;gt; 5) declare @x int; &amp;nbsp;	0x0200000040BD6E030F37CC9BF7E531A29F4C7379BB6ABCF7	0x06000D0040BD6E03B8417E0A000000000000000000000000&lt;/p&gt;
&lt;p&gt;...Going further, I tried...&lt;/p&gt;
&lt;p&gt;use scratch&lt;/p&gt;
&lt;p&gt;---this is an example of an adhoc query&lt;/p&gt;
&lt;p&gt;if exists(select col1 from t1 where col2 &amp;gt; 5) declare @x int;&lt;/p&gt;
&lt;p&gt;go&lt;/p&gt;
&lt;p&gt; ---this is an example of an adhoc query with different comment&lt;/p&gt;
&lt;p&gt;if exists(select col1 from t1 where col2 &amp;gt; 5) declare @x int;&lt;/p&gt;
&lt;p&gt;go&lt;/p&gt;
&lt;p&gt; ---this really hacks the comment&lt;/p&gt;
&lt;p&gt;-- and is an example of an adhoc query with different comment&lt;/p&gt;
&lt;p&gt;if exists(select col1 from t1 where col2 &amp;gt; 5) declare @x int;&lt;/p&gt;
&lt;p&gt;go&lt;/p&gt;
&lt;p&gt;-- OK&lt;/p&gt;
&lt;p&gt; ---this is an example of an adhoc query with different comment&lt;/p&gt;
&lt;p&gt;if exists(select col1 from t1 where col2 &amp;gt; 5) declare @x int;&lt;/p&gt;
&lt;p&gt;-- more hacking&lt;/p&gt;
&lt;p&gt;go&lt;/p&gt;
&lt;p&gt; ---this is an example of an adhoc query with different comment&lt;/p&gt;
&lt;p&gt;if exists(select col1 from t1 where col2 &amp;gt; 5) declare @x int;&lt;/p&gt;
&lt;p&gt;--xxx&lt;/p&gt;
&lt;p&gt;--bb&lt;/p&gt;
&lt;p&gt;go&lt;/p&gt;
&lt;p&gt;select text, sql_handle, plan_handle &lt;/p&gt;
&lt;p&gt;from sys.dm_exec_query_stats qs cross apply sys.dm_exec_sql_text(sql_handle)&lt;/p&gt;
&lt;p&gt;where text like '%---this%'&lt;/p&gt;
&lt;p&gt;go&lt;/p&gt;
&lt;p&gt;...Which gives...&lt;/p&gt;
&lt;p&gt;text	sql_handle	plan_handle&lt;/p&gt;
&lt;p&gt; &amp;nbsp;select text, sql_handle, plan_handle &amp;nbsp; from sys.dm_exec_query_stats qs cross apply sys.dm_exec_sql_text(sql_handle) &amp;nbsp;where text like '%---this%' &amp;nbsp;	0x02000000247B772F7B42AAA94F7A0A7220E383AA855AEDAC	0x06000F00247B772FB841920A000000000000000000000000&lt;/p&gt;
&lt;p&gt;use scratch &amp;nbsp; &amp;nbsp;---this is an example of an adhoc query &amp;nbsp;if exists(select col1 from t1 where col2 &amp;gt; 5) declare @x int; &amp;nbsp;	0x0200000040BD6E030F37CC9BF7E531A29F4C7379BB6ABCF7	0x06000D0040BD6E03B8417E0A000000000000000000000000&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;select text, sql_handle, plan_handle &amp;nbsp; from sys.dm_exec_query_stats qs cross apply sys.dm_exec_sql_text(sql_handle) &amp;nbsp;where text like '%---this%' &amp;nbsp;	0x02000000102821342B23781363F8FC7784A4D2EA8E6F2537	0x06000F0010282134B861820A000000000000000000000000&lt;/p&gt;
&lt;p&gt;...It seems that it's not as simple you describe. Changing the comment may produce a new hash and hence a new plan but I don't think the rules are simple.&lt;/p&gt;</description></item><item><title>re: 6.0 Best Programming Practices </title><link>http://blogs.msdn.com/sqlprogrammability/archive/2007/01/13/6-0-best-programming-practices.aspx#1553370</link><pubDate>Tue, 30 Jan 2007 00:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1553370</guid><dc:creator>sangeethashekar</dc:creator><description>&lt;P&gt;D@TPBS, I am pasting the queries below from your example above:&lt;/P&gt;
&lt;P&gt;use scratch&lt;/P&gt;
&lt;P&gt;---this is an example of an adhoc query&lt;/P&gt;
&lt;P&gt;if exists(select col1 from t1 where col2 &amp;gt; 5) declare @x int;&lt;/P&gt;
&lt;P&gt;go&lt;/P&gt;
&lt;P&gt;---this is an example of an adhoc query with different comment&lt;/P&gt;
&lt;P&gt;if exists(select col1 from t1 where col2 &amp;gt; 5) declare @x int;&lt;/P&gt;
&lt;P&gt;go&lt;/P&gt;
&lt;P&gt;The first thing to note here is that these 2 batches are NOT identical. The first batch has an extra statement 'use scratch', and this is what will cause the sql handles of the 2 batches above to be different.&lt;/P&gt;
&lt;P&gt;I see that you are working with SQL Server 2005 SP1. In SP1, queries with one or more context switches had a non zero cost. This is not the case in SQL Server 2005 Sp2. This might explain why you dont see an entry in the cache for the 2nd query with a different comment. &lt;/P&gt;
&lt;P&gt;Even in SQL Server 2005 Sp1, if you look at the sys.dm_exec_cached_plans DMV, you will see that the cached plan for the 1st query (in your example above) has a usecount of 1 and not 2. This implies that the plan was not re-used for the 2nd query with a different comment.&lt;/P&gt;
&lt;P&gt;I would recommend you try the example below here on SQL Server 2005 SP1:&lt;/P&gt;
&lt;P&gt;---this is an example of an adhoc query&lt;/P&gt;
&lt;P&gt;select t1.col1 from t1 join t2 on t1.col1 = t2.col1 where t2.col2 &amp;gt; 5&lt;/P&gt;
&lt;P&gt;go&lt;/P&gt;
&lt;P&gt;---this is an example of an adhoc query longer comments&lt;/P&gt;
&lt;P&gt;select t1.col1 from t1 join t2 on t1.col1 = t2.col1 where t2.col2 &amp;gt; 5&lt;/P&gt;
&lt;P&gt;go&lt;/P&gt;
&lt;P&gt;select text, sql_handle, plan_handle &lt;/P&gt;
&lt;P&gt;from sys.dm_exec_query_stats qs cross apply sys.dm_exec_sql_text(sql_handle)&lt;/P&gt;
&lt;P&gt;where text not like '%select text%'&lt;/P&gt;
&lt;P&gt;go&lt;/P&gt;
&lt;P&gt;You will see 2 entries with different sql and plan handles&lt;/P&gt;
&lt;P&gt;The example as in the blog article would work on SQL Server 2005 SP2.&lt;/P&gt;
&lt;P&gt;The blog article: &lt;A href="http://blogs.msdn.com/sqlprogrammability/archive/2007/01/22/3-0-changes-in-caching-behavior-between-sql-server-2000-sql-server-2005-rtm-and-sql-server-2005-sp2.aspx" target=_new rel=nofollow&gt;http://blogs.msdn.com/sqlprogrammability/archive/2007/01/22/3-0-changes-in-caching-behavior-between-sql-server-2000-sql-server-2005-rtm-and-sql-server-2005-sp2.aspx&lt;/A&gt; has more information on changes in SP2 and the exact cost formula.&lt;/P&gt;</description></item><item><title>OPTIMIZE FOR UNKNOWN – a little known SQL Server 2008 feature</title><link>http://blogs.msdn.com/sqlprogrammability/archive/2007/01/13/6-0-best-programming-practices.aspx#9243139</link><pubDate>Sat, 20 Dec 2008 03:27:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9243139</guid><dc:creator>Microsoft SQL ISV Program Management Team</dc:creator><description>&lt;p&gt;Using parameterized queries is a well known SQL Server Best Practice. This technique ensures caching&lt;/p&gt;
</description></item><item><title>OPTIMIZE FOR UNKNOWN – a little known SQL Server 2008 feature</title><link>http://blogs.msdn.com/sqlprogrammability/archive/2007/01/13/6-0-best-programming-practices.aspx#9243140</link><pubDate>Sat, 20 Dec 2008 03:27:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9243140</guid><dc:creator>SQL Programmability &amp; API Development Team Blog</dc:creator><description>&lt;p&gt;Using parameterized queries is a well known SQL Server Best Practice. This technique ensures caching&lt;/p&gt;
</description></item></channel></rss>