{"id":504,"date":"2010-04-28T12:20:00","date_gmt":"2010-04-28T12:20:00","guid":{"rendered":"\/blogs\/kimberly\/post\/Clearing-the-cache-are-there-other-options.aspx"},"modified":"2013-01-11T23:10:40","modified_gmt":"2013-01-12T07:10:40","slug":"clearing-the-cache-are-there-other-options","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/clearing-the-cache-are-there-other-options\/","title":{"rendered":"Clearing the cache &#8211; are there other options?"},"content":{"rendered":"<p>\nOK, I&#39;ve had a bit of a tangent going on optimizing stored procedures and its all revolved around adhoc plan caching and potentially wasted cache due to the single-use plans created (see the most recent posts titled:<a href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/plan-cache-and-optimizing-for-adhoc-workloads\/\">Plan cache and optimizing for adhoc workloads<\/a>&nbsp;and <a href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/plan-cache-adhoc-workloads-and-clearing-the-single-use-plan-cache-bloat\/\">Plan cache, adhoc workloads and clearing the single-use plan cache bloat<\/a>). To be more specific, these are statements that when generated (via adhoc or prepared statements) don&#39;t end up being parameterized (in general &#8211; only relatively simple statements can be effectively parameterized and when your statements complex, SQL Server won&#39;t parameterize them &#8211; this can be good and in some systems this can be terribly bad). But, now you know <em>how <\/em>bad&#8230; you know how much of your cache you&#39;re wasting but where is it going and are there any similarities to these statements? Should you (or can you?) bring them together to see if there is a commonality that can be leveraged?\n<\/p>\n<p>\nIn general, the answer is &quot;it depends&quot; (you <em>knew <\/em>that was coming!). However, I do have good news, you can definitely gauge what&#39;s actually going on and how many of the statements are in fact, similar. This might allow you to leverage another database option (forced parameterization) and\/or determine that you really NEED stored procedures. However, that might end up being more complex as it will require rewriting the app (or at least <em>some <\/em>of the app). Again, the good news here is that you&#39;ll be able to tell <em>which <\/em>part of the app.\n<\/p>\n<p>\nTake for example the following:\n<\/p>\n<blockquote><p>\n\t<font color=\"#0000ff\"><font color=\"#800000\">DBCC FREEPROCCACHE<br \/>\n\t<font size=\"2\" color=\"#0000ff\">go<br \/>\n\t<font color=\"#0000ff\">SELECT <font color=\"#808080\">* <font color=\"#0000ff\">FROM dbo<font color=\"#808080\">.Member <font color=\"#0000ff\">WHERE Lastname <font color=\"#808080\">= <font color=\"#ff0000\">&#39;Tripp&#39;<font size=\"2\" color=\"#808080\">;<br \/>\n\t<font color=\"#0000ff\">go<br \/>\n\tSELECT <font color=\"#808080\">* <font color=\"#0000ff\">FROM dbo<font color=\"#808080\">.Member <font color=\"#0000ff\">WHERE Lastname <font color=\"#808080\">= <font color=\"#ff0000\">&#39;Tripped&#39;<font size=\"2\" color=\"#808080\">;<br \/>\n\t<font color=\"#0000ff\">go<br \/>\n\tSELECT <font color=\"#808080\">* <font color=\"#0000ff\">FROM dbo<font color=\"#808080\">.Member <font color=\"#0000ff\">WHERE Lastname <font color=\"#808080\">= <font color=\"#ff0000\">&#39;Tripper&#39;<font size=\"2\" color=\"#808080\">;<br \/>\n\t<font color=\"#0000ff\">go<br \/>\n\tSELECT <font color=\"#808080\">* <font color=\"#0000ff\">FROM dbo<font color=\"#808080\">.Member <font color=\"#0000ff\">WHERE Lastname <font color=\"#808080\">= <font color=\"#ff0000\">&#39;Tripps&#39;<font size=\"2\" color=\"#808080\">;<br \/>\n\t<font color=\"#0000ff\">go<br \/>\n\tSELECT <font color=\"#808080\">* <font color=\"#0000ff\">FROM dbo<font color=\"#808080\">.Member <font color=\"#0000ff\">WHERE Lastname <font color=\"#808080\">= <font color=\"#ff0000\">&#39;Falls&#39;<font color=\"#808080\">;<br \/>\n\t<font color=\"#0000ff\">go <font color=\"#0000ff\"><font color=\"#0000ff\"><font color=\"#0000ff\"><font color=\"#0000ff\"><font color=\"#0000ff\"><font color=\"#0000ff\"><font color=\"#0000ff\"><\/p>\n<p>\n\tSELECT cp<font color=\"#808080\">.objtype<font color=\"#808080\">, cp<font color=\"#808080\">.cacheobjtype<font color=\"#808080\">, cp<font color=\"#808080\">.size_in_bytes<font color=\"#808080\">, cp<font color=\"#808080\">.refcounts<font color=\"#808080\">, cp<font color=\"#808080\">.usecounts<font color=\"#808080\">, st<font color=\"#808080\">.<font color=\"#0000ff\">text <font color=\"#008000\">&#8211;, *<br \/>\n\t<font color=\"#0000ff\">FROM <font color=\"#008000\">sys<font color=\"#808080\">.<font color=\"#008000\">dm_exec_cached_plans <font color=\"#0000ff\">AS cp<br \/>\n\t<font color=\"#808080\">CROSS <font color=\"#808080\">APPLY <font color=\"#008000\">sys<font color=\"#808080\">.<font color=\"#008000\">dm_exec_sql_text<font color=\"#808080\">(cp<font color=\"#808080\">.plan_handle<font color=\"#808080\">) <font color=\"#0000ff\">AS st<br \/>\n\t<font color=\"#0000ff\">WHERE cp<font color=\"#808080\">.objtype <font color=\"#808080\">IN<font color=\"#0000ff\"> <font color=\"#808080\">(<font color=\"#ff0000\">&#39;Adhoc&#39;<font color=\"#808080\">, <font color=\"#ff0000\">&#39;Prepared&#39;<font color=\"#808080\">)<br \/>\n\t<font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND st<font color=\"#808080\">.<font color=\"#0000ff\">text <font color=\"#808080\">LIKE <font color=\"#ff0000\">&#39;%from dbo.member%&#39;&nbsp;<br \/>\n\t<font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND st<font color=\"#808080\">.<font color=\"#0000ff\">text <font color=\"#808080\">NOT <font color=\"#808080\">LIKE <font color=\"#ff0000\">&#39;%SELECT cp.objecttype%&#39;<br \/>\n\t<font color=\"#0000ff\">ORDER <font color=\"#0000ff\">BY cp<font color=\"#808080\">.objtype<br \/>\n\t<font color=\"#0000ff\">go\n\t<\/p>\n<p>\n\t<strong>objtype&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cacheobjtype&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size_in_bytes&nbsp;&nbsp; refcounts&nbsp; usecounts&nbsp;&nbsp;&nbsp;&nbsp; text<br \/>\n\t<\/strong>Adhoc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Compiled Plan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;24576&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT * FROM dbo.Member WHERE Lastname = &#39;Falls&#39;;&nbsp; <br \/>\n\tAdhoc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Compiled Plan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;24576&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripps&#39;;&nbsp; <br \/>\n\tAdhoc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Compiled Plan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 24576&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripper&#39;;&nbsp; <br \/>\n\tAdhoc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Compiled Plan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 24576&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripped&#39;;&nbsp; <br \/>\n\tAdhoc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Compiled Plan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 24576&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripp&#39;;&nbsp;\n\t<\/p>\n<\/blockquote>\n<p>\nThe first thing to notice is that each of these queries has a *different* statement in the adhoc plan cache. The second is to recognize that each of these statements take 24K in the plan cache. If we were to change to &quot;optimize for ad hoc workloads&quot; then things might be a bit better:\n<\/p>\n<blockquote><p>\n\t<font color=\"#0000ff\">sp_configure<font color=\"#000000\"> <font color=\"#ff0000\">&#39;optimize for ad hoc workloads&#39;<font color=\"#808080\">,<font color=\"#000000\"> 1<br \/>\n\t<font color=\"#0000ff\">go<br \/>\n\treconfigure<br \/>\n\tgo<br \/>\n\tDBCC FREEPROCCACHE<br \/>\n\t<font size=\"2\" color=\"#0000ff\">go<br \/>\n\t<font color=\"#0000ff\">SELECT <font color=\"#808080\">* <font color=\"#0000ff\">FROM dbo<font color=\"#808080\">.Member <font color=\"#0000ff\">WHERE Lastname <font color=\"#808080\">= <font color=\"#ff0000\">&#39;Tripp&#39;<font size=\"2\" color=\"#808080\">;<br \/>\n\t<font color=\"#0000ff\">go<br \/>\n\tSELECT <font color=\"#808080\">* <font color=\"#0000ff\">FROM dbo<font color=\"#808080\">.Member <font color=\"#0000ff\">WHERE Lastname <font color=\"#808080\">= <font color=\"#ff0000\">&#39;Tripped&#39;<font size=\"2\" color=\"#808080\">;<br \/>\n\t<font color=\"#0000ff\">go<br \/>\n\tSELECT <font color=\"#808080\">* <font color=\"#0000ff\">FROM dbo<font color=\"#808080\">.Member <font color=\"#0000ff\">WHERE Lastname <font color=\"#808080\">= <font color=\"#ff0000\">&#39;Tripper&#39;<font size=\"2\" color=\"#808080\">;<br \/>\n\t<font color=\"#0000ff\">go<br \/>\n\tSELECT <font color=\"#808080\">* <font color=\"#0000ff\">FROM dbo<font color=\"#808080\">.Member <font color=\"#0000ff\">WHERE Lastname <font color=\"#808080\">= <font color=\"#ff0000\">&#39;Tripps&#39;<font size=\"2\" color=\"#808080\">;<br \/>\n\t<font color=\"#0000ff\">go<br \/>\n\tSELECT <font color=\"#808080\">* <font color=\"#0000ff\">FROM dbo<font color=\"#808080\">.Member <font color=\"#0000ff\">WHERE Lastname <font color=\"#808080\">= <font color=\"#ff0000\">&#39;Falls&#39;<font color=\"#808080\">;<br \/>\n\t<font color=\"#0000ff\">go <font color=\"#0000ff\"><font color=\"#0000ff\"><font color=\"#0000ff\"><font color=\"#0000ff\"><font color=\"#0000ff\"><\/p>\n<p>\n\tSELECT cp<font color=\"#808080\">.objtype<font color=\"#808080\">, cp<font color=\"#808080\">.cacheobjtype<font color=\"#808080\">, cp<font color=\"#808080\">.size_in_bytes<font color=\"#808080\">, cp<font color=\"#808080\">.refcounts<font color=\"#808080\">, cp<font color=\"#808080\">.usecounts<font color=\"#808080\">, st<font color=\"#808080\">.<font color=\"#0000ff\">text <font color=\"#008000\">&#8211;, *<br \/>\n\t<font color=\"#0000ff\">FROM <font color=\"#008000\">sys<font color=\"#808080\">.<font color=\"#008000\">dm_exec_cached_plans <font color=\"#0000ff\">AS cp<br \/>\n\t<font color=\"#808080\">CROSS <font color=\"#808080\">APPLY <font color=\"#008000\">sys<font color=\"#808080\">.<font color=\"#008000\">dm_exec_sql_text<font color=\"#808080\">(cp<font color=\"#808080\">.plan_handle<font color=\"#808080\">) <font color=\"#0000ff\">AS st<br \/>\n\t<font color=\"#0000ff\">WHERE cp<font color=\"#808080\">.objtype <font color=\"#808080\">IN<font color=\"#0000ff\"> <font color=\"#808080\">(<font color=\"#ff0000\">&#39;Adhoc&#39;<font color=\"#808080\">, <font color=\"#ff0000\">&#39;Prepared&#39;<font color=\"#808080\">)<br \/>\n\t<font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND st<font color=\"#808080\">.<font color=\"#0000ff\">text <font color=\"#808080\">LIKE <font color=\"#ff0000\">&#39;%from dbo.member%&#39;&nbsp;<br \/>\n\t<font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND st<font color=\"#808080\">.<font color=\"#0000ff\">text <font color=\"#808080\">NOT <font color=\"#808080\">LIKE <font color=\"#ff0000\">&#39;%SELECT cp.objecttype%&#39;<br \/>\n\t<font color=\"#0000ff\">ORDER <font color=\"#0000ff\">BY cp<font color=\"#808080\">.objtype<br \/>\n\t<font color=\"#0000ff\">go\n\t<\/p>\n<p>\n\t<strong>objtype&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cacheobjtype&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; size_in_bytes&nbsp;&nbsp; refcounts&nbsp; usecounts&nbsp;&nbsp;&nbsp;&nbsp; text<br \/>\n\t<\/strong>Adhoc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Compiled Plan Stub&nbsp; 320&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT * FROM dbo.Member WHERE Lastname = &#39;Falls&#39;;&nbsp; <br \/>\n\tAdhoc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Compiled Plan Stub&nbsp; 320&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripps&#39;;&nbsp; <br \/>\n\tAdhoc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Compiled Plan Stub&nbsp; 320&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripper&#39;;&nbsp; <br \/>\n\tAdhoc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Compiled Plan Stub&nbsp; 320&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripped&#39;;&nbsp; <br \/>\n\tAdhoc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Compiled Plan Stub&nbsp; 320&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripp&#39;;&nbsp;\n\t<\/p>\n<\/blockquote>\n<p>\nBut, this is not the only issue. Even though each statement will only be in the cache once and will only take 320 bytes (instead of 24K) the cumulative effect of this might be significant. So, how can you see this? If you try to aggregate over these statements then you&#39;ll only be able to use the first 40-50 characters while that works here it definitely won&#39;t work for more complex statements. Until &#8211; SQL Server 2008. In 2008, you can use the query_hash to find similar statements and their overall affect on the query cache.\n<\/p>\n<p><font color=\"#0000ff\"><\/p>\n<blockquote>\n<p>\n\tSELECT qs2<font color=\"#808080\">.query_hash <font color=\"#0000ff\">AS [Query Hash]<br \/>\n\t<font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , <font color=\"#ff00ff\">SUM<font color=\"#808080\">(qs2<font color=\"#808080\">.size_in_bytes<font color=\"#808080\">) <font color=\"#0000ff\">AS [Total Cache Size]<br \/>\n\t<font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , <font color=\"#ff00ff\">SUM<font color=\"#808080\">(qs2<font color=\"#808080\">.total_worker_time<font color=\"#808080\">)\/<font color=\"#ff00ff\">SUM<font color=\"#808080\">(qs2<font color=\"#808080\">.execution_count<font color=\"#808080\">) <font color=\"#0000ff\">AS [Avg CPU Time]<br \/>\n\t<font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , <font color=\"#ff00ff\">SUM<font color=\"#808080\">(qs2<font color=\"#808080\">.total_elapsed_time<font color=\"#808080\">)\/<font color=\"#ff00ff\">SUM<font color=\"#808080\">(qs2<font color=\"#808080\">.execution_count<font color=\"#808080\">) <font color=\"#0000ff\">AS [Avg Duration]<br \/>\n\t<font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , <font color=\"#ff00ff\">COUNT<font color=\"#808080\">(*) <font color=\"#0000ff\">AS [Number of plans]&nbsp;<br \/>\n\t<font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , <font color=\"#ff00ff\">MIN<font color=\"#808080\">(qs2<font color=\"#808080\">.statement_text<font color=\"#808080\">) <font color=\"#0000ff\">AS [Statement Text]<br \/>\n\t<font color=\"#0000ff\">FROM <font color=\"#808080\">(<font color=\"#0000ff\">SELECT qs<font color=\"#808080\">.*, cp<font color=\"#808080\">.size_in_bytes<font color=\"#808080\">, <font color=\"#ff00ff\">SUBSTRING<font color=\"#808080\">(ST<font color=\"#808080\">.<font color=\"#0000ff\">text<font color=\"#808080\">,<font color=\"#0000ff\"> <font color=\"#808080\">(QS<font color=\"#808080\">.statement_start_offset<font color=\"#808080\">\/2<font color=\"#808080\">) <font color=\"#808080\">+ 1<font color=\"#808080\">,<font color=\"#0000ff\"> <font color=\"#808080\">((<font color=\"#0000ff\">CASE statement_end_offset <font color=\"#0000ff\">WHEN <font color=\"#808080\">-1 <font color=\"#0000ff\">THEN <font color=\"#ff00ff\">DATALENGTH<font color=\"#808080\">(st<font color=\"#808080\">.<font color=\"#0000ff\">text<font color=\"#808080\">)&nbsp;<br \/>\n\t<font color=\"#0000ff\"><font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ELSE QS<font color=\"#808080\">.statement_end_offset <font color=\"#0000ff\">END <font color=\"#808080\">&#8211; QS<font color=\"#808080\">.statement_start_offset<font color=\"#808080\">)\/2<font color=\"#808080\">) <font color=\"#808080\">+ 1<font color=\"#808080\">) <font color=\"#0000ff\">AS statement_text&nbsp;<br \/>\n\t<font color=\"#0000ff\"><font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM <font color=\"#008000\">sys<font color=\"#808080\">.<font color=\"#008000\">dm_exec_cached_plans <font color=\"#0000ff\">AS cp&nbsp;<br \/>\n\t<font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JOIN <font color=\"#008000\">sys<font color=\"#808080\">.<font color=\"#008000\">dm_exec_query_stats <font color=\"#0000ff\">AS qs <font color=\"#0000ff\">ON cp<font color=\"#808080\">.plan_handle <font color=\"#808080\">= qs<font color=\"#808080\">.plan_handle<br \/>\n\t<font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CROSS <font color=\"#808080\">APPLY <font color=\"#008000\">sys<font color=\"#808080\">.<font color=\"#008000\">dm_exec_sql_text<font color=\"#808080\">(QS<font color=\"#808080\">.<font color=\"#0000ff\">sql_handle<font color=\"#808080\">) <font color=\"#0000ff\">as ST<font color=\"#808080\">) <font color=\"#0000ff\">as qs2<br \/>\n\t<font color=\"#0000ff\">GROUP <font color=\"#0000ff\">BY qs2<font color=\"#808080\">.query_hash <font color=\"#0000ff\">ORDER <font color=\"#0000ff\">BY [Avg Duration] <font color=\"#0000ff\">DESC<font color=\"#808080\">;\n\t<\/p>\n<\/blockquote>\n<p>\nThis is incredibly important because this will give you the information to determine what SIMILAR query (or queries) are consuming the cache and which ones have the highest cumulative effect. And, if you see a statement that is consistent and can be optimized using indexes (and consistently using the same indexes) what you might try first is &quot;forced parameterization.&quot; This is a DATABASE-level option (available in 2005+) that significantly improves but still doesn&#39;t guarantee that a statement will be parameterized. See the Books Online for Forced Parameterization (<a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ms175037.aspx\" target=\"_blank\">SQL Server 2008 Forced Parameterization<\/a> and <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ms175037(SQL.90).aspx\" target=\"_blank\">SQL Server 2005 Forced Parameterization<\/a>)&nbsp;for the rules on when SQL Server parameterizes. However, there are many statements that will. If you have A LOT of plan cache bloat AND you think your plans might benefit from forced parameterization &#8211; this is definitely something you can try.\n<\/p>\n<p>\nAgain, the good news here is that the QP will try harder to parameterize but there are still many cases where it won&#39;t. In this case, SQL Server parameterizes this statement (it becomes a Prepared statement) as:\n<\/p>\n<blockquote>\n<p>\n\t(@0 varchar(8000))select * from dbo . Member where Lastname = @0\n\t<\/p>\n<\/blockquote>\n<p>\nYou can see this through the following query\/results:\n<\/p>\n<p><font color=\"#0000ff\"><\/p>\n<blockquote>\n<p>\n\tSELECT st<font color=\"#808080\">.<font color=\"#0000ff\">text<font color=\"#808080\">, cp<font color=\"#808080\">.objtype<font color=\"#808080\">, cp<font color=\"#808080\">.cacheobjtype<font color=\"#808080\">, cp<font color=\"#808080\">.size_in_bytes<font color=\"#808080\">, cp<font color=\"#808080\">.refcounts<font color=\"#808080\">, cp<font color=\"#808080\">.usecounts<font color=\"#808080\">, qp<font color=\"#808080\">.query_plan <font color=\"#008000\">&#8211;, *<br \/>\n\t<font color=\"#0000ff\">FROM <font color=\"#008000\">sys<font color=\"#808080\">.<font color=\"#008000\">dm_exec_cached_plans <font color=\"#0000ff\">AS cp<br \/>\n\t<font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CROSS <font color=\"#808080\">APPLY <font color=\"#008000\">sys<font color=\"#808080\">.<font color=\"#008000\">dm_exec_sql_text<font color=\"#808080\">(cp<font color=\"#808080\">.plan_handle<font color=\"#808080\">) <font color=\"#0000ff\">AS st<br \/>\n\t<font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CROSS <font color=\"#808080\">APPLY <font color=\"#008000\">sys<font color=\"#808080\">.<font color=\"#008000\">dm_exec_query_plan<font color=\"#808080\">(plan_handle<font color=\"#808080\">) <font color=\"#0000ff\">AS qp<br \/>\n\t<font color=\"#0000ff\">WHERE cp<font color=\"#808080\">.objtype <font color=\"#808080\">IN<font color=\"#0000ff\"> <font color=\"#808080\">(<font color=\"#ff0000\">&#39;Adhoc&#39;<font color=\"#808080\">, <font color=\"#ff0000\">&#39;Prepared&#39;<font color=\"#808080\">)<br \/>\n\t<font color=\"#808080\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND st<font color=\"#808080\">.<font color=\"#0000ff\">text <font color=\"#808080\">LIKE <font color=\"#ff0000\">&#39;%member%&#39;<br \/>\n\t<font color=\"#0000ff\">ORDER <font color=\"#0000ff\">BY cp<font color=\"#808080\">.objtype<br \/>\n\t<font color=\"#0000ff\">go\n\t<\/p>\n<\/blockquote>\n<div>\n<blockquote>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"2624\" style=\"width: 1970pt; border-collapse: collapse\">\n<tbody>\n<tr height=\"20\" style=\"height: 15pt\">\n<td class=\"xl65\" width=\"444\" height=\"20\" style=\"background-color: transparent; width: 333pt; height: 15pt; border: windowtext 0.5pt solid\"><strong><font face=\"Calibri\" size=\"3\">text<\/strong><\/td>\n<td class=\"xl65\" width=\"64\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; width: 48pt; border-top: windowtext 0.5pt solid; border-right: windowtext 0.5pt solid\"><strong><font face=\"Calibri\" size=\"3\">objtype<\/strong><\/td>\n<td class=\"xl65\" width=\"98\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; width: 74pt; border-top: windowtext 0.5pt solid; border-right: windowtext 0.5pt solid\"><strong><font face=\"Calibri\" size=\"3\">cacheobjtype<\/strong><\/td>\n<td class=\"xl65\" width=\"92\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; width: 69pt; border-top: windowtext 0.5pt solid; border-right: windowtext 0.5pt solid\"><strong><font face=\"Calibri\" size=\"3\">size_in_bytes<\/strong><\/td>\n<td class=\"xl65\" width=\"66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; width: 50pt; border-top: windowtext 0.5pt solid; border-right: windowtext 0.5pt solid\"><strong><font face=\"Calibri\" size=\"3\">refcounts<\/strong><\/td>\n<td class=\"xl65\" width=\"70\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; width: 53pt; border-top: windowtext 0.5pt solid; border-right: windowtext 0.5pt solid\"><strong><font face=\"Calibri\" size=\"3\">usecounts<\/strong><\/td>\n<td class=\"xl65\" width=\"1790\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; width: 1343pt; border-top: windowtext 0.5pt solid; border-right: windowtext 0.5pt solid\"><strong><font face=\"Calibri\" size=\"3\">query_plan<\/strong><\/td>\n<\/tr>\n<tr height=\"20\" style=\"height: 15pt\">\n<td class=\"xl66\" height=\"20\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext 0.5pt solid; background-color: transparent; height: 15pt; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\">SELECT * FROM dbo.Member WHERE Lastname = &#39;Falls&#39;;<span>&nbsp;&nbsp;<\/span><\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">Adhoc<\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">Compiled Plan<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">24576<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">2<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">1<\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">&lt;ShowPlanXML xmlns=&quot;http:\/\/schemas.microsoft.com\/sqlserver\/2004\/07\/showplan&quot; Version=&quot;1.1&quot; Build=&quot;10.0.2531.0&quot;&gt;&lt;BatchSequence&gt;&lt;Batch&gt;&lt;Statements&gt;&lt;StmtSimple StatementText=&quot;SELECT * FROM dbo.Member WHERE Lastname = &#39;Falls&#39;;&amp;#xD;&amp;#xA;&quot; StatementId=&quot;1&quot; StatementCompId=&quot;1&quot; StatementType=&quot;SELECT&quot; ParameterizedPlanHandle=&quot;0x0600050088DDF510B8C0630A000000000000000000000000&quot; ParameterizedText=&quot;(@0 varchar(8000))select * from dbo . Member where Lastname = @0&quot; \/&gt;&lt;\/Statements&gt;&lt;\/Batch&gt;&lt;\/BatchSequence&gt;&lt;\/ShowPlanXML&gt;<\/td>\n<\/tr>\n<tr height=\"20\" style=\"height: 15pt\">\n<td class=\"xl66\" height=\"20\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext 0.5pt solid; background-color: transparent; height: 15pt; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\">SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripps&#39;;<span>&nbsp;&nbsp;<\/span><\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">Adhoc<\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">Compiled Plan<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">24576<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">2<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">1<\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">&lt;ShowPlanXML xmlns=&quot;http:\/\/schemas.microsoft.com\/sqlserver\/2004\/07\/showplan&quot; Version=&quot;1.1&quot; Build=&quot;10.0.2531.0&quot;&gt;&lt;BatchSequence&gt;&lt;Batch&gt;&lt;Statements&gt;&lt;StmtSimple StatementText=&quot;SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripps&#39;;&amp;#xD;&amp;#xA;&quot; StatementId=&quot;1&quot; StatementCompId=&quot;1&quot; StatementType=&quot;SELECT&quot; ParameterizedPlanHandle=&quot;0x0600050088DDF510B8C0630A000000000000000000000000&quot; ParameterizedText=&quot;(@0 varchar(8000))select * from dbo . Member where Lastname = @0&quot; \/&gt;&lt;\/Statements&gt;&lt;\/Batch&gt;&lt;\/BatchSequence&gt;&lt;\/ShowPlanXML&gt;<\/td>\n<\/tr>\n<tr height=\"20\" style=\"height: 15pt\">\n<td class=\"xl66\" height=\"20\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext 0.5pt solid; background-color: transparent; height: 15pt; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\">SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripper&#39;;<span>&nbsp;&nbsp;<\/span><\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">Adhoc<\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">Compiled Plan<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">24576<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">2<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">1<\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">&lt;ShowPlanXML xmlns=&quot;http:\/\/schemas.microsoft.com\/sqlserver\/2004\/07\/showplan&quot; Version=&quot;1.1&quot; Build=&quot;10.0.2531.0&quot;&gt;&lt;BatchSequence&gt;&lt;Batch&gt;&lt;Statements&gt;&lt;StmtSimple StatementText=&quot;SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripper&#39;;&amp;#xD;&amp;#xA;&quot; StatementId=&quot;1&quot; StatementCompId=&quot;1&quot; StatementType=&quot;SELECT&quot; ParameterizedPlanHandle=&quot;0x0600050088DDF510B8C0630A000000000000000000000000&quot; ParameterizedText=&quot;(@0 varchar(8000))select * from dbo . Member where Lastname = @0&quot; \/&gt;&lt;\/Statements&gt;&lt;\/Batch&gt;&lt;\/BatchSequence&gt;&lt;\/ShowPlanXML&gt;<\/td>\n<\/tr>\n<tr height=\"20\" style=\"height: 15pt\">\n<td class=\"xl66\" height=\"20\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext 0.5pt solid; background-color: transparent; height: 15pt; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\">SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripped&#39;;<span>&nbsp;&nbsp;<\/span><\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">Adhoc<\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">Compiled Plan<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">24576<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">2<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">1<\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">&lt;ShowPlanXML xmlns=&quot;http:\/\/schemas.microsoft.com\/sqlserver\/2004\/07\/showplan&quot; Version=&quot;1.1&quot; Build=&quot;10.0.2531.0&quot;&gt;&lt;BatchSequence&gt;&lt;Batch&gt;&lt;Statements&gt;&lt;StmtSimple StatementText=&quot;SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripped&#39;;&amp;#xD;&amp;#xA;&quot; StatementId=&quot;1&quot; StatementCompId=&quot;1&quot; StatementType=&quot;SELECT&quot; ParameterizedPlanHandle=&quot;0x0600050088DDF510B8C0630A000000000000000000000000&quot; ParameterizedText=&quot;(@0 varchar(8000))select * from dbo . Member where Lastname = @0&quot; \/&gt;&lt;\/Statements&gt;&lt;\/Batch&gt;&lt;\/BatchSequence&gt;&lt;\/ShowPlanXML&gt;<\/td>\n<\/tr>\n<tr height=\"20\" style=\"height: 15pt\">\n<td class=\"xl66\" height=\"20\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext 0.5pt solid; background-color: transparent; height: 15pt; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\">SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripp&#39;;<span>&nbsp;&nbsp;<\/span><\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">Adhoc<\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">Compiled Plan<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">24576<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">2<\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">1<\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">&lt;ShowPlanXML xmlns=&quot;http:\/\/schemas.microsoft.com\/sqlserver\/2004\/07\/showplan&quot; Version=&quot;1.1&quot; Build=&quot;10.0.2531.0&quot;&gt;&lt;BatchSequence&gt;&lt;Batch&gt;&lt;Statements&gt;&lt;StmtSimple StatementText=&quot;SELECT * FROM dbo.Member WHERE Lastname = &#39;Tripp&#39;;&amp;#xD;&amp;#xA;&quot; StatementId=&quot;1&quot; StatementCompId=&quot;1&quot; StatementType=&quot;SELECT&quot; ParameterizedPlanHandle=&quot;0x0600050088DDF510B8C0630A000000000000000000000000&quot; ParameterizedText=&quot;(@0 varchar(8000))select * from dbo . Member where Lastname = @0&quot; \/&gt;&lt;\/Statements&gt;&lt;\/Batch&gt;&lt;\/BatchSequence&gt;&lt;\/ShowPlanXML&gt;<\/td>\n<\/tr>\n<tr height=\"20\" style=\"height: 15pt\">\n<td class=\"xl66\" height=\"20\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext 0.5pt solid; background-color: transparent; height: 15pt; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\"><strong>(@0 varchar(8000))select * from dbo . Member where Lastname = @0<\/strong><\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\"><strong>Prepared<\/strong><\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\"><strong>Compiled Plan<\/strong><\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\"><strong>40960<\/strong><\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\"><strong>7<\/strong><\/td>\n<td class=\"xl66\" align=\"right\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\"><strong>10<\/strong><\/td>\n<td class=\"xl66\" style=\"border-bottom: windowtext 0.5pt solid; border-left: windowtext; background-color: transparent; border-top: windowtext; border-right: windowtext 0.5pt solid\"><font face=\"Calibri\" size=\"3\">&lt;ShowPlanXML xmlns=&quot;http:\/\/schemas.microsoft.com\/sqlserver\/2004\/07\/showplan&quot; Version=&quot;1.1&quot; Build=&quot;10.0.2531.0&quot;&gt;&lt;BatchSequence&gt;&lt;Batch&gt;&lt;Statements&gt;&lt;StmtSimple StatementText=&quot;(@0 varchar(8000))select * from dbo . Member where Lastname = @0&quot; StatementId=&quot;1&quot; StatementCompId=&quot;1&quot; StatementType=&quot;SELECT&quot; StatementSubTreeCost=&quot;0.00657038&quot; StatementEstRows=&quot;1&quot; StatementOptmLevel=&quot;FULL&quot; QueryHash=&quot;0xDEF0805C7B74A31E&quot; QueryPlanHash=&quot;0x785C8E320D853B97&quot; StatementOptmEarlyAbortReason=&quot;GoodEnoughPlanFound&quot;&gt;&lt;StatementSetOptions QUOTED_IDENTIFIER=&quot;true&quot; ARITHABORT=&quot;true&quot; CONCAT_NULL_YIELDS_NULL=&quot;true&quot; ANSI_NULLS=&quot;true&quot; ANSI_PADDING=&quot;true&quot; ANSI_WARNINGS=&quot;true&quot; NUMERIC_ROUNDABORT=&quot;false&quot; \/&gt;&lt;QueryPlan CachedPlanSize=&quot;24&quot; CompileTime=&quot;1&quot; CompileCPU=&quot;1&quot; CompileMemory=&quot;136&quot;&gt;&lt;RelOp NodeId=&quot;0&quot; PhysicalOp=&quot;Nested Loops&quot; LogicalOp=&quot;Inner Join&quot; EstimateRows=&quot;1&quot; EstimateIO=&quot;0&quot; EstimateCPU=&quot;4.18e-006&quot; AvgRowSize=&quot;173&quot; EstimatedTotalSubtreeCost=&quot;0.00657038&quot; Parallel=&quot;0&quot; EstimateRebinds=&quot;0&quot; EstimateRewinds=&quot;0&quot;&gt;&lt;OutputList&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;member_no&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;lastname&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;firstname&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;middleinitial&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;street&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;city&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;state_prov&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;country&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;mail_code&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;phone_no&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;photograph&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;issue_dt&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;expr_dt&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;region_no&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;corp_no&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;prev_balance&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;curr_balance&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;member_code&quot; \/&gt;&lt;\/OutputList&gt;&lt;NestedLoops Optimized=&quot;0&quot;&gt;&lt;OuterReferences&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;member_no&quot; \/&gt;&lt;\/OuterReferences&gt;&lt;RelOp NodeId=&quot;1&quot; PhysicalOp=&quot;Index Seek&quot; LogicalOp=&quot;Index Seek&quot; EstimateRows=&quot;1&quot; EstimateIO=&quot;0.003125&quot; EstimateCPU=&quot;0.0001581&quot; AvgRowSize=&quot;20&quot; EstimatedTotalSubtreeCost=&quot;0.0032831&quot; TableCardinality=&quot;10000&quot; Parallel=&quot;0&quot; EstimateRebinds=&quot;0&quot; EstimateRewinds=&quot;0&quot;&gt;&lt;OutputList&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;member_no&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;lastname&quot; \/&gt;&lt;\/OutputList&gt;&lt;IndexScan Ordered=&quot;1&quot; ScanDirection=&quot;FORWARD&quot; ForcedIndex=&quot;0&quot; ForceSeek=&quot;0&quot; NoExpandHint=&quot;0&quot;&gt;&lt;DefinedValues&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;member_no&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;lastname&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;\/DefinedValues&gt;&lt;Object Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Index=&quot;[IX_Member_Lastname]&quot; IndexKind=&quot;NonClustered&quot; \/&gt;&lt;SeekPredicates&gt;&lt;SeekPredicateNew&gt;&lt;SeekKeys&gt;&lt;Prefix ScanType=&quot;EQ&quot;&gt;&lt;RangeColumns&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;lastname&quot; \/&gt;&lt;\/RangeColumns&gt;&lt;RangeExpressions&gt;&lt;ScalarOperator ScalarString=&quot;[@0]&quot;&gt;&lt;Identifier&gt;&lt;ColumnReference Column=&quot;@0&quot; \/&gt;&lt;\/Identifier&gt;&lt;\/ScalarOperator&gt;&lt;\/RangeExpressions&gt;&lt;\/Prefix&gt;&lt;\/SeekKeys&gt;&lt;\/SeekPredicateNew&gt;&lt;\/SeekPredicates&gt;&lt;\/IndexScan&gt;&lt;\/RelOp&gt;&lt;RelOp NodeId=&quot;3&quot; PhysicalOp=&quot;Clustered Index Seek&quot; LogicalOp=&quot;Clustered Index Seek&quot; EstimateRows=&quot;1&quot; EstimateIO=&quot;0.003125&quot; EstimateCPU=&quot;0.0001581&quot; AvgRowSize=&quot;161&quot; EstimatedTotalSubtreeCost=&quot;0.0032831&quot; TableCardinality=&quot;10000&quot; Parallel=&quot;0&quot; EstimateRebinds=&quot;0&quot; EstimateRewinds=&quot;0&quot;&gt;&lt;OutputList&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;firstname&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;middleinitial&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;street&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;city&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;state_prov&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;country&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;mail_code&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;phone_no&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;photograph&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;issue_dt&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;expr_dt&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;region_no&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;corp_no&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;prev_balance&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;curr_balance&quot; \/&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;member_code&quot; \/&gt;&lt;\/OutputList&gt;&lt;IndexScan Lookup=&quot;1&quot; Ordered=&quot;1&quot; ScanDirection=&quot;FORWARD&quot; ForcedIndex=&quot;0&quot; ForceSeek=&quot;0&quot; NoExpandHint=&quot;0&quot;&gt;&lt;DefinedValues&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;firstname&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;middleinitial&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;street&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;city&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;state_prov&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;country&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;mail_code&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;phone_no&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;photograph&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;issue_dt&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;expr_dt&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;region_no&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;corp_no&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;prev_balance&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;curr_balance&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;DefinedValue&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;member_code&quot; \/&gt;&lt;\/DefinedValue&gt;&lt;\/DefinedValues&gt;&lt;Object Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Index=&quot;[member_ident]&quot; TableReferenceId=&quot;-1&quot; IndexKind=&quot;Clustered&quot; \/&gt;&lt;SeekPredicates&gt;&lt;SeekPredicateNew&gt;&lt;SeekKeys&gt;&lt;Prefix ScanType=&quot;EQ&quot;&gt;&lt;RangeColumns&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;member_no&quot; \/&gt;&lt;\/RangeColumns&gt;&lt;RangeExpressions&gt;&lt;ScalarOperator ScalarString=&quot;[credit].[dbo].[member].[member_no]&quot;&gt;&lt;Identifier&gt;&lt;ColumnReference Database=&quot;[credit]&quot; Schema=&quot;[dbo]&quot; Table=&quot;[member]&quot; Column=&quot;member_no&quot; \/&gt;&lt;\/Identifier&gt;&lt;\/ScalarOperator&gt;&lt;\/RangeExpressions&gt;&lt;\/Prefix&gt;&lt;\/SeekKeys&gt;&lt;\/SeekPredicateNew&gt;&lt;\/SeekPredicates&gt;&lt;\/IndexScan&gt;&lt;\/RelOp&gt;&lt;\/NestedLoops&gt;&lt;\/RelOp&gt;&lt;ParameterList&gt;&lt;ColumnReference Column=&quot;@0&quot; ParameterCompiledValue=&quot;&#39;Tripp&#39;&quot; \/&gt;&lt;\/ParameterList&gt;&lt;\/QueryPlan&gt;&lt;\/StmtSimple&gt;&lt;\/Statements&gt;&lt;\/Batch&gt;&lt;\/BatchSequence&gt;&lt;\/ShowPlanXML&gt;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/blockquote>\n<\/div>\n<p>\nIdeally, you&#39;ll end up with less wasted cache because all adhoc statements from here &#8211; will use the Prepared version of the statement. This also means that they&#39;ll run with a compiled plan (so you&#39;ll also save compile time) and &#8211; if the&nbsp;plans are consistent then all of this is good.&nbsp;However, if other values (for example &#39;Smith&#39;) require a completely different plan &#8211; then this could actually be worse (this is the &quot;It Depends&quot; part). But, if you still have statements that do have consistent execution plans (because you know the data and\/or you know exactly how this particular data is being used) then EITHER sp_executesql or a stored procedure would effectively force the parameterization as well and it would say to me that you really understand your data\/application. But, if you&#39;re wrong&#8230; anytime you force the parameterize (and increase re-use of a plan &#8211; you could end up forcing the re-use of a bad (and possibly <strong>REALLY, REALLY, REALLY, REALLY, REALLY, REALLY, REALLY, REALLY, REALLY <\/strong>bad plan) :)).\n<\/p>\n<p>\nSo, while you solve one problem &#8211; you could create another one (see the blog post titled:&nbsp;<a href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/using-the-option-recompile-option-for-a-statement\/\">Using the OPTION (RECOMPILE) option for a statement<\/a> for more information). If the plans are NOT consistent then forcing a plan (through either forced parameterization, sp_executesql OR a regular stored procedure) can result in poor performance. When writing the stored procedure (and through good testing processes), you&#39;ll be able to create better and more effective code. This is truly my preferred method. And, I&#39;m just about to wrap up my series with roughly 3 more posts (at least that&#39;s what I currently have planned but you guys have certainly been steering me in this direction for a bit :-)).\n<\/p>\n<p>\nThanks for reading,<br \/>\nkt<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OK, I&#39;ve had a bit of a tangent going on optimizing stored procedures and its all revolved around adhoc plan caching and potentially wasted cache due to the single-use plans created (see the most recent posts titled:Plan cache and optimizing for adhoc workloads&nbsp;and Plan cache, adhoc workloads and clearing the single-use plan cache bloat). To [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,50,53,62,65,66],"tags":[],"class_list":["post-504","post","type-post","status-publish","format-standard","hentry","category-dynamic-string-execution","category-optimizing-procedural-code","category-plan-cache","category-sp_executesql","category-sql-server-2005","category-sql-server-2008"],"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/comments?post=504"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/504\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/media?parent=504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/categories?post=504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/tags?post=504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}