<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.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>Kalen Delaney : plan guides</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/plan+guides/default.aspx</link><description>Tags: plan guides</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Did You Know? I'll be at TechEd!</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/2009/05/08/teched.aspx</link><pubDate>Fri, 08 May 2009 17:23:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:13901</guid><dc:creator>Kalen Delaney</dc:creator><slash:comments>0</slash:comments><comments>http://www2.sqlblog.com/blogs/kalen_delaney/comments/13901.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=13901</wfw:commentRss><description>&lt;P&gt;I'm delivering a session on Tuesday morning (May 12) called "Controlling Your Query Plans", and for most of the rest of Tuesday, Wednesday and Thursday I'll be at the ATE (Ask the Experts) Area. Feel free to stop by!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG title=TENA_blgr2_mvps height=133 alt=TENA_blgr2_mvps src="http://blogs.msdn.com/blogfiles/mvpawardprogram/WindowsLiveWriter/RealWorldPerspectivesontheProsandConsofS_BA39/TENA_blgr2_mvps_thumb.jpg" width=115 align=right border=0&gt;&lt;FONT color=#ff00ff size=4&gt;~Kalen&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=13901" width="1" height="1"&gt;</description><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/plan+guides/default.aspx">plan guides</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/conference/default.aspx">conference</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/TechEd/default.aspx">TechEd</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/query+plans/default.aspx">query plans</category></item><item><title>Did You Know? Everything's Up to Date in Kansas City!</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/2008/02/01/kansas-city-seminar.aspx</link><pubDate>Fri, 01 Feb 2008 18:54:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:4826</guid><dc:creator>Kalen Delaney</dc:creator><slash:comments>1</slash:comments><comments>http://www2.sqlblog.com/blogs/kalen_delaney/comments/4826.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=4826</wfw:commentRss><description>&lt;P&gt;They are so up-to-date, I have chosen it as one of the handful of&amp;nbsp; cities on my user group tour... &lt;/P&gt;
&lt;P&gt;I'll be presenting a full day seminar on &lt;EM&gt;Controlling and Reusing Query Plans&lt;/EM&gt; in SQL Server 2005. You can get more information from&amp;nbsp; my schedule page at &lt;A href="http://schedule.kalendelaney.com/" target=_blank&gt;schedule.KalenDelaney.com&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Full details, including registration information, can be found here:&lt;BR&gt;&lt;A title=http://www.cleardata.biz/events/query-plans-2008.aspx href="http://www.cleardata.biz/events/query-plans-2008.aspx"&gt;http://www.cleardata.biz/events/query-plans-2008.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;If you live anywhere nearby, it would be great to see you there.&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#ff00ff&gt;~Kalen&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=4826" width="1" height="1"&gt;</description><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/training/default.aspx">training</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/index+hints/default.aspx">index hints</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/execution+plan/default.aspx">execution plan</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/plan+guides/default.aspx">plan guides</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/user+groups/default.aspx">user groups</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/seminars/default.aspx">seminars</category></item><item><title>Geek City: Q &amp; A on my Plan Cache Sizing article in SQL Server Magazine</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/2007/12/15/QA-on-my-plan-cache-sizing-article-in-sql-server-magazine.aspx</link><pubDate>Sun, 16 Dec 2007 03:00:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:4004</guid><dc:creator>Kalen Delaney</dc:creator><slash:comments>3</slash:comments><comments>http://www2.sqlblog.com/blogs/kalen_delaney/comments/4004.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=4004</wfw:commentRss><description>&lt;BLOCKQUOTE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;A href="http://www.sqlmag.com/articles/articleid/97373/Managing_PlanCache_Size.html" target=_blank&gt;My December article in SQL Server Magazine&lt;/A&gt;, called Managing Plan Cache Size, expands on the information I talked about in a &lt;A href="http://sqlblog.com/blogs/kalen_delaney/archive/2007/11/04/did-you-know-sp2-does-not-limit-the-amount-of-plan-cache-you-can-have.aspx" target=_blank&gt;post last month on the limits of plan cache&lt;/A&gt;.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;A reader of the article ask some questions on the SQL Server Magazine web site, and I have decided to use my blog to post the answers. &lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Q:&amp;nbsp; Could you give me an illustration how to calculate target memory please? Let say there is a 64 bit system (64 bit hardware, windows 2003 64 bit enterprise edition, SQL Server 2005 Enterprise edition 64 bit, memory 30 GB), max server memory (in sp_configure) is between 16 (min value) - 30 GB (max value)).&lt;/EM&gt; &lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P align=left&gt;A: Target memory refers to the maximum physical memory that can be committed to the buffer pool and ideally is the lesser of the values you’ve configured for “max server memory” and the total amount of visible physical memory available to the OS. On a 64bit system, all your memory is visible, and your max server memory is the same as your physical memory, so target ideally will be 30GB. You can see the target memory value in the metadata view &lt;EM&gt;sys.dm_os_sys_info&lt;/EM&gt; (where it's called &lt;B&gt;bpool_commit_target)&lt;/B&gt;, along with the value for visible memory (called &lt;B&gt;bpool_visible&lt;/B&gt;). 
&lt;P align=left&gt;However, there are other factors that can affect the target value. The BOL entry for &lt;EM&gt;sys.dm_os_sys_info&lt;/EM&gt; defines &lt;B&gt;bpool_commit_target&lt;/B&gt; as follows: 
&lt;P align=left&gt;&lt;EM&gt;"Number of 8-KB buffers needed by the buffer pool. The target amount is calculated using a variety of inputs such as the current state of the system, including its load, the memory requested by current processes, the amount of memory installed on the computer, and configuration parameters."&lt;/EM&gt; 
&lt;P align=left&gt;The exact formula is not published. It's best to just use &lt;EM&gt;sys.dm_os_sys_info&lt;/EM&gt; to see what your system's target memory value is. 
&lt;P align=left&gt;&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;EM&gt;Q:&amp;nbsp; In your article, you give an example how to calculate plan-cache pressure limit, for example for SQL Server 2005 RTM and SP1, it calculates for on 28 target memory as follows : (.75 x 8 GB) + (.5 x 20 GB) = 6 GB. Then you add to 10 GB so pressure limit is 16 GB. Where does 10 GB come from?&lt;/EM&gt; 
&lt;BLOCKQUOTE&gt;
&lt;P&gt;A: You have rephrased the expression incorrectly. This is what the article says: 
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (.75 x 8 GB) + (.5 x 20 GB) = 6 GB + 10 GB = 16 GB 
&lt;P&gt;If you do the math, you’ll see that the first term in parentheses (.75 x 8 GB) is equal to 6 GB and the second term (.5 x 20 GB) is equal to 10 GB, so we add the 6 and the 10 together. 
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;EM&gt;Q. You mention "plan-cache pressure limit". Does it mean that when , let's say pressure limit of 28 GB is 16 GB, 16 GB is reached then SQL Server couldn't remove plan cache to make it less than 16 GB, is my understanding right?&lt;/EM&gt; 
&lt;BLOCKQUOTE&gt;
&lt;P&gt;A: No, in the above example, we calculated the pressure limit to be 16 GB. That means when your SQL Server hits 16 GB of memory for plan cache, it will start removing plans from cache to keep cache from getting too big. 
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;EM&gt;Q: You show how to remove a prepared query by creating a plan guide with recompile hint; is it possible to remove non-prepared query plan on the following sample:&lt;/EM&gt; 
&lt;P&gt;&lt;EM&gt;EXEC sp_create_plan_guide @name = N'RemovePlan1', @stmt = N' SELECT * FROM Sales WHERE ContactID &amp;lt; 3', @type = N'SQL', @module_or_batch = NULL, @params = N', @hints = N'OPTION(RECOMPILE)'; &lt;/EM&gt;
&lt;P&gt;&lt;EM&gt;and I issue query twice as below shown SELECT * FROM Sales WHERE ContactID &amp;lt; 3 &lt;/EM&gt;
&lt;P&gt;&lt;EM&gt;When I check in syscacheobjects with the query: &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/EM&gt;&lt;EM&gt;SELECT usecounts as uses, sql FROM sys.syscacheobjects WHERE dbid = db_id('AdventureWorks'); &lt;/EM&gt;
&lt;P&gt;&lt;EM&gt;It shows that the non-prepared query plan is reused with count equal to 2.&lt;/EM&gt; 
&lt;BLOCKQUOTE&gt;
&lt;P&gt;A: Althought it is possible to create a plan guide for an adhoc (non-prepared) query, your @stmt parameter must be absolutely exactly the same as the query you will run. In your plan guide creation, you have an extra space in front of the SELECT, and your @module_or_batch parameter is incorrect; it should be NULL. A correct plan guide can be used for adhoc queries, but it will NOT remove the adhoc plan from cache when you drop the plan guide, like a plan guide for a prepared query will. You will have to wait for the next version (SQL Server 2008) to get that behavior.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I hope this helps! 
&lt;P&gt;&lt;FONT color=#ff00ff&gt;~Kalen&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=4004" width="1" height="1"&gt;</description><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/plan+cache/default.aspx">plan cache</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/plan+guides/default.aspx">plan guides</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/SQL+Server+Magazine/default.aspx">SQL Server Magazine</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/memory+management/default.aspx">memory management</category></item><item><title>Geek City: Clearing a Single Plan from Cache</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/2007/09/29/geek-city-clearing-a-single-plan-from-cache.aspx</link><pubDate>Sat, 29 Sep 2007 18:18:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:2742</guid><dc:creator>Kalen Delaney</dc:creator><slash:comments>14</slash:comments><comments>http://www2.sqlblog.com/blogs/kalen_delaney/comments/2742.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=2742</wfw:commentRss><description>&lt;P&gt;Caching and reusing query plans is both a good thing and a bad thing.&amp;nbsp;Saving costs of recompilation for a query that is run repeatedly can be a good thing. Using a cached plan that is not appropriate can be a very bad thing. &lt;/P&gt;
&lt;P&gt;What do you do if you have plans in cache that should not be there?&lt;/P&gt;
&lt;P&gt;SQL Server 2000 and 2005 provide a way to clear all the plans from plan cache with DBCC FREEPROCCACHE. There is also a way to clear all plans from a single database using DBCC FLUSHPROCINDB(&amp;lt;db_id&amp;gt;) &lt;/P&gt;
&lt;P&gt;Rumor has it that SQL Server 2008 will provide a way to clear a single plan from cache. The details of exactly how this will be accomplished, and how you can specify which plan to remove&amp;nbsp;are not available yet. However, in SQL Server 2005 there is a way to remove a single plan using the new SQL Server 2005 Plan Guide feature. This trick only works for plans where the &lt;EM&gt;objtype&lt;/EM&gt; value for the plan in&amp;nbsp;&lt;EM&gt;sys.syscacheobjects&lt;/EM&gt; is 'prepared' and&amp;nbsp;is not 'adhoc'. It is not needed for 'proc' plans, because 'proc' plans can be easily removed by simply using the ALTER PROC command. Prepared plans, whether they are created through autoparameterization, by using &lt;EM&gt;sp_executesql&lt;/EM&gt;, or by using prepare and execute methods in your application can be removed by creating a plan guide that matches the parameterized query.&amp;nbsp;You can then drop the plan guide if you don't want to continue using it.&lt;/P&gt;
&lt;P&gt;Here's an example:&lt;/P&gt;
&lt;P&gt;First create a copy of the &lt;EM&gt;Sales.SalesOrderHeader&lt;/EM&gt; table in the &lt;EM&gt;AdventureWorks&lt;/EM&gt; database, and then build a nonclustered, nonunique index on the new table.&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;USE AdventureWorks&lt;BR&gt;GO&lt;BR&gt;SELECT * INTO Sales FROM Sales. SalesOrderHeader&lt;BR&gt;GO&lt;BR&gt;CREATE INDEX Contact_indx on Sales(ContactID)&lt;BR&gt;GO&lt;/FONT&gt; 
&lt;P&gt;Next, create a prepared query which selects all rows less than a particular&amp;nbsp;very restrictive value (one that only returns a few rows.) 
&lt;P&gt;&lt;FONT face="Courier New"&gt;DBCC FREEPROCCACHE&lt;BR&gt;GO&lt;BR&gt;EXEC sp_executesql &lt;BR&gt;N'SELECT * FROM Sales WHERE ContactID &amp;lt; @ID',&lt;BR&gt;N'@ID int',&lt;BR&gt;@ID = 3&lt;BR&gt;GO&lt;/FONT&gt; 
&lt;P&gt;&lt;U&gt;&lt;/U&gt;
&lt;P&gt;A second query, which uses a much less restrictive value, will use the same plan. 
&lt;P&gt;&lt;FONT face="courier new"&gt;EXEC sp_executesql &lt;BR&gt;N'SELECT * FROM Sales WHERE ContactID &amp;lt; @ID',&lt;BR&gt;N'@ID int',&lt;BR&gt;@ID = 200&lt;BR&gt;GO&lt;/FONT&gt; 
&lt;P&gt;The following query verifies that the same plan was used for both queries: 
&lt;P&gt;&lt;FONT face="Courier New"&gt;SELECT usecounts as uses, sql FROM sys.syscacheobjects &lt;BR&gt;WHERE dbid = db_id('AdventureWorks')&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND objtype = 'Prepared'&lt;BR&gt;GO&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;I get the following results: &lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new"&gt;uses sql &lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new"&gt;---- --------------------------------------------------&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new"&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp; (@ID int)SELECT * FROM Sales WHERE ContactID &amp;lt; @ID&lt;/FONT&gt; 
&lt;P&gt;If you realize that you don’t want this plan in cache because you don’t want to always use the plan for the nonselective value, you can create the following plan guide&amp;nbsp; 
&lt;P&gt;&lt;FONT face="courier new"&gt;EXEC sp_create_plan_guide &lt;BR&gt;@name = N'RemovePlan',&lt;BR&gt;@stmt = N'SELECT * FROM Sales WHERE ContactID &amp;lt; @ID',&lt;BR&gt;@type = N'SQL',&lt;BR&gt;@module_or_batch = NULL,&lt;BR&gt;@params = N'@ID int',&lt;BR&gt;@hints = N'OPTION(RECOMPILE)';&lt;/FONT&gt; 
&lt;P&gt;As soon as the plan guide is created, it will remove the plan for this query from cache, and you can confirm that by running the query that inspects &lt;EM&gt;sys.syscacheobjects&lt;/EM&gt;. 
&lt;P&gt;If you leave the plan guide in the database, the RECOMPILE hint will force SQL Server to always come up with a new plan for queries that have the same form as the query in the &lt;EM&gt;@stmt&lt;/EM&gt; variable. If you just want to remove the existing plan, but not keep the plan guide around, you can remove the plan guide with the following statement: 
&lt;P&gt;&lt;FONT face="Courier New"&gt;EXEC sp_control_plan_guide N'drop', N'RemovePlan'&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face=ta&gt;Have fun!&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT color=#ff00ff&gt;~Kalen&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=2742" width="1" height="1"&gt;</description><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/plan+cache/default.aspx">plan cache</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/syscacheobjects/default.aspx">syscacheobjects</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/DBCC/default.aspx">DBCC</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/plan+guides/default.aspx">plan guides</category></item></channel></rss>