<?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 cache</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/plan+cache/default.aspx</link><description>Tags: plan cache</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Geek City: Plan Caching Internals</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/2010/08/23/plan-caching-internals.aspx</link><pubDate>Mon, 23 Aug 2010 17:04:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:28191</guid><dc:creator>Kalen Delaney</dc:creator><slash:comments>0</slash:comments><comments>http://www2.sqlblog.com/blogs/kalen_delaney/comments/28191.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=28191</wfw:commentRss><description>This Friday is the last of my summer seminars. On August 27, I am presenting on plan caching, plan reuse and recompile and plan cache metadata. This is one of my favorite topics to talk about, because a lot of the information is not very widely known,...(&lt;a href="http://www2.sqlblog.com/blogs/kalen_delaney/archive/2010/08/23/plan-caching-internals.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=28191" 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/metadata/default.aspx">metadata</category><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/seminars/default.aspx">seminars</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? I'm doing 3 more online seminars with SSWUG!</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/2010/06/11/3-more-online-seminars-with-sswug.aspx</link><pubDate>Fri, 11 Jun 2010 22:46:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:26122</guid><dc:creator>Kalen Delaney</dc:creator><slash:comments>2</slash:comments><comments>http://www2.sqlblog.com/blogs/kalen_delaney/comments/26122.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=26122</wfw:commentRss><description>As I told you in April , I recorded two more seminars with Stephen Wynkoop, on aspects of Query Processing. The first one will be broadcast on June 30 and the second on August 27. In between, we'll broadcast my Index Internals seminar, on July 23. Workshops...(&lt;a href="http://www2.sqlblog.com/blogs/kalen_delaney/archive/2010/06/11/3-more-online-seminars-with-sswug.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=26122" 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/plan+cache/default.aspx">plan cache</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/indexes/default.aspx">indexes</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/seminars/default.aspx">seminars</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/query+plans/default.aspx">query plans</category></item><item><title>Geek City: Clearing Plans for a Single Database</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/2010/05/21/clearing-plans-for-a-single-database.aspx</link><pubDate>Sat, 22 May 2010 00:46:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:25412</guid><dc:creator>Kalen Delaney</dc:creator><slash:comments>3</slash:comments><comments>http://www2.sqlblog.com/blogs/kalen_delaney/comments/25412.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=25412</wfw:commentRss><description>I know Friday afternoon isn't the best time for blogging, as everyone is going home now, and by Monday morning, this post will be old news. But I'm not shutting down just yet, and a something came up this week that I just realized not everybody knew about,...(&lt;a href="http://www2.sqlblog.com/blogs/kalen_delaney/archive/2010/05/21/clearing-plans-for-a-single-database.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=25412" 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/alter+database/default.aspx">alter database</category></item><item><title>Did You Know? More online seminars!</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/2010/04/22/more-online-seminars.aspx</link><pubDate>Thu, 22 Apr 2010 23:20:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:24507</guid><dc:creator>Kalen Delaney</dc:creator><slash:comments>2</slash:comments><comments>http://www2.sqlblog.com/blogs/kalen_delaney/comments/24507.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=24507</wfw:commentRss><description>I am in Tucson again, having just recorded two more online workshops to be broadcast by SSWUG. We haven't set the dates yet, but we are thinking about offering a special package deal for the two of them. The topics really are related and I think they...(&lt;a href="http://www2.sqlblog.com/blogs/kalen_delaney/archive/2010/04/22/more-online-seminars.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=24507" 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/books/default.aspx">books</category><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/seminars/default.aspx">seminars</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/query+plans/default.aspx">query plans</category></item><item><title>Geek City: Clearing a Single Plan From Cache in SQL Server 2008</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/2008/10/31/clearing-a-single-plan-from-cache-in-sql-server-2008.aspx</link><pubDate>Fri, 31 Oct 2008 23:43:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9787</guid><dc:creator>Kalen Delaney</dc:creator><slash:comments>4</slash:comments><comments>http://www2.sqlblog.com/blogs/kalen_delaney/comments/9787.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=9787</wfw:commentRss><description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I just today found out about something that was in plain sight in the Books Online... but the documentation is so vast, I haven't discovered everything yet. &lt;/P&gt;
&lt;P&gt;I blogged about &lt;A href="http://sqlblog.com/blogs/kalen_delaney/archive/2007/09/29/geek-city-clearing-a-single-plan-from-cache.aspx" target=_blank&gt;clearing a single plan from cache in SQL Server 2005&lt;/A&gt; a while back, and mentioned a rumor that there would be a new way to do this in SQL Server 2008. And then just today, someone posted a comment on that blog post, asking about the new 2008 method. &lt;/P&gt;
&lt;P&gt;One of my contacts on the SQL Server team at Microsoft just sent me an email telling me about new features related to plan caching, as I am starting to work on updating that chapter for my 2008 book. He mentioned that DBCC now takes parameters, and one of the parameters is a plan handle!&lt;/P&gt;&lt;PRE&gt;DBCC FREEPROCCACHE [ ( { plan_handle | sql_handle | pool_name } ) ] [ WITH NO_INFOMSGS ]&lt;/PRE&gt;
&lt;P&gt;There is full example in the Books Online, so I won't repeat it here. If you don't have a local copy of BOL handy, you can read about the new option to DBCC FREEPROCCACHE online here:&lt;/P&gt;
&lt;P&gt;&lt;A title=http://msdn.microsoft.com/en-us/library/ms174283.aspx href="http://msdn.microsoft.com/en-us/library/ms174283.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms174283.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have fun!&lt;/P&gt;
&lt;P&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=9787" width="1" height="1"&gt;</description><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/metadata/default.aspx">metadata</category><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/SQL+Server+2008/default.aspx">SQL Server 2008</category></item><item><title>Geek City: Reducing Cache Bloat and a Metadata Bug</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/2008/10/29/reducing-cache-bloat-and-a-metadata-bug.aspx</link><pubDate>Wed, 29 Oct 2008 23:43:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9714</guid><dc:creator>Kalen Delaney</dc:creator><slash:comments>1</slash:comments><comments>http://www2.sqlblog.com/blogs/kalen_delaney/comments/9714.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=9714</wfw:commentRss><description>&lt;P&gt;I've been running some tests with the new SQL Server 2008 configuration option 'optimize for ad hoc workloads' which is intended to reduce cache bloat due to holding onto plans for single use ad hoc queries. This option will allow a new type of cached object to be stored, which isn't the only plan, but merely a stub. There will be a corresponding new &lt;EM&gt;cacheobjtype&lt;/EM&gt; value called "Compiled Plan Stub". &lt;/P&gt;
&lt;P&gt;There are several ways of looking at the contents of cache. In SQL 2000, we had a pseudo table called &lt;EM&gt;master.dbo.syscacheobjects&lt;/EM&gt;, and we can still access this object in SQL 2005 and 2008 by referencing the compatibility view &lt;EM&gt;sys.syscacheobjects&lt;/EM&gt; (from any database). However, when I look at this view, I don't see "Compiled Plan Stub", but rather "Compiled Plan Stu ". &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;A href="http://sqlblog.com/blogs/kalen_delaney/WindowsLiveWriter/GeekCityReducingCacheBloatandaMetadataBu_E2B9/stu.png"&gt;&lt;IMG style="BORDER-RIGHT:0px;BORDER-TOP:0px;BORDER-LEFT:0px;BORDER-BOTTOM:0px;" height=219 alt=stu src="http://sqlblog.com/blogs/kalen_delaney/WindowsLiveWriter/GeekCityReducingCacheBloatandaMetadataBu_E2B9/stu_thumb.png" width=403 border=0&gt;&lt;/A&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I checked the definition of &lt;EM&gt;syscacheobjects&lt;/EM&gt;, using the function &lt;EM&gt;object_definition&lt;/EM&gt;:&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Consolas size=2&gt;USE master;&lt;BR&gt;SELECT OBJECT_DEFINITION(object_id('syscacheobjects'));&lt;/FONT&gt; 
&lt;P&gt;and I saw that &lt;EM&gt;syscacheobjects&lt;/EM&gt; was derived from a column in &lt;EM&gt;sys.dm_exec_cached_plans&lt;/EM&gt;, and converted to an nvarchar(17). Unfortunately, "Compiled Plan Stub" is 18 characters long. &lt;/P&gt;
&lt;P&gt;I'll be talking about this new cached object type in my &lt;A href="http://www.sqlknowhow.com/events/insidesql3.aspx" target=_blank&gt;3-day seminar in London next week&lt;/A&gt;, and I'll post more information about using the 'optimize for ad hoc workloads' at a later time. &lt;/P&gt;
&lt;P&gt;But normally, I wouldn't have run into this problem, because I have created my own view based on the new SQL 2005 metadata, including &lt;EM&gt;sys.dm_exec_cached_plans&lt;/EM&gt;. Usually, I use my own view, which converts to a longer string, so I wouldn't have seen the string truncation. But I was testing this on a new instance, where I hadn't created my own view yet. &lt;/P&gt;
&lt;P&gt;I am including my view definition, which is what I have been using since the SQL Server 2005 came out, to inspect the plan cache. In fact, I usually add a where clause to restrict the output only to Compiled Plans. Note that the view name starting with &lt;EM&gt;sp_&lt;/EM&gt; means we can access the view from any database:&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Consolas size=1&gt;-- Create a view to show most of the same information as &lt;BR&gt;--&amp;nbsp;&amp;nbsp; SQL Server 2000's syscacheobjects &lt;BR&gt;&lt;/FONT&gt;&lt;FONT face=Consolas size=1&gt;USE master&lt;BR&gt;GO&lt;BR&gt;CREATE VIEW sp_cacheobjects(bucketid, cacheobjtype, objtype, objid, dbid, dbidexec, &lt;BR&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; uid, refcounts, usecounts, pagesused, setopts, langid, dateformat, status, lasttime, &lt;BR&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; maxexectime, avgexectime, lastreads,lastwrites, sqlbytes, sql) &lt;BR&gt;AS&lt;BR&gt;SELECT &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face=Consolas size=1&gt;pvt.bucketid, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(nvarchar(20), pvt.cacheobjtype) as cacheobjtype, pvt.objtype, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(int, pvt.objectid)as object_id, CONVERT(smallint, pvt.dbid) as dbid,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(smallint, pvt.dbid_execute) as execute_dbid, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(smallint, pvt.user_id) as user_id, pvt.refcounts, pvt.usecounts, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pvt.size_in_bytes / 8192 as size_in_bytes,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(int, pvt.set_options) as setopts, CONVERT(smallint, pvt.language_id) as langid,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(smallint, pvt.date_format) as date_format, CONVERT(int, pvt.status) as status,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(bigint, 0), CONVERT(bigint, 0), CONVERT(bigint, 0), CONVERT(bigint, 0), &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(bigint, 0),CONVERT(int, LEN(CONVERT(nvarchar(max), fgs.text)) * 2), &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(nvarchar(3900), fgs.text)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Consolas size=1&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM (SELECT ecp.*, epa.attribute, epa.value&lt;BR&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; FROM sys.dm_exec_cached_plans ecp &lt;BR&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUTER APPLY sys.dm_exec_plan_attributes(ecp.plan_handle) epa) as ecpa&lt;BR&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; PIVOT (MAX(ecpa.value) for ecpa.attribute &lt;BR&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;&amp;nbsp;&amp;nbsp;&amp;nbsp; IN ("set_options", "objectid", "dbid", "dbid_execute", &lt;BR&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "user_id", "language_id", "date_format", "status")) as pvt&lt;BR&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; OUTER APPLY sys.dm_exec_sql_text(pvt.plan_handle) fgs&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Consolas size=1&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This view shows the full cached object type:&lt;/P&gt;&lt;A href="http://sqlblog.com/blogs/kalen_delaney/WindowsLiveWriter/GeekCityReducingCacheBloatandaMetadataBu_E2B9/stub.png"&gt;&lt;IMG style="BORDER-RIGHT:0px;BORDER-TOP:0px;BORDER-LEFT:0px;BORDER-BOTTOM:0px;" height=226 alt=stub src="http://sqlblog.com/blogs/kalen_delaney/WindowsLiveWriter/GeekCityReducingCacheBloatandaMetadataBu_E2B9/stub_thumb.png" width=528 border=0&gt;&lt;/A&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have fun!&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#ff00ff size=4&gt;~Kalen&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Consolas size=1&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=9714" width="1" height="1"&gt;</description><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/metadata/default.aspx">metadata</category><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/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>Did You Know? SP2 does NOT limit the amount of plan cache you can have</title><link>http://www2.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</link><pubDate>Mon, 05 Nov 2007 02:39:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:3214</guid><dc:creator>Kalen Delaney</dc:creator><slash:comments>10</slash:comments><comments>http://www2.sqlblog.com/blogs/kalen_delaney/comments/3214.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=3214</wfw:commentRss><description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I am preparing my talk for the &lt;A title="Seminar Description: Controlling and Reusing Plans" href="http://sqlserverfaq.com/?eid=103"&gt;UK SQL Server Users Group&lt;/A&gt;&amp;nbsp;on November 16, I was going through other presentations on plan cache management. At the PASS Conference in Denver in September there was a session on management of cache, and I was surprised to find a slide that indicated there was a hard upper limit for plan cache size, and that the limit was actually decreased as of SQL Server 2005 SP2. This is not true. There is no upper limit to plan cache size, other than any upper limit you might have on total SQL Server memory. But usually we don't want to use too much memory for query plans, because it will reduce the memory available for data cache. In fact, there is a situation called 'plan cache bloat' where memory gets filled with very cheap adhoc plans that aren't reused, but take up memory space that could be much more effectively used for other purposes. It is to reduce this plan cache bloat that the caching management algorithm changed in SP2. &lt;/P&gt;
&lt;P&gt;The changes do NOT enforce an upper limit. The change actually computes a plan cache size at which SQL Server recognizes that there is memory pressure, and it will start removing plans from cache. The plans to be removed are the cheap plans that have not been reused, and this is a GOOD THING.&lt;/P&gt;
&lt;P&gt;The following explanation is based on a section in Chapter 5 of Inside &lt;EM&gt;SQL Server 2005: Query Tuning and Optimization&lt;/EM&gt;, used by permission of the author. :-)&lt;/P&gt;
&lt;P&gt;---------------------------------------------------------------------&lt;/P&gt;
&lt;P&gt;When discussing memory pressure, we refer to the term ‘visible’ memory. Visible memory is that which is directly addressable in the SQL Server process’s virtual address space. On a 32-bit SQL Server instance, the maximum value for the visible memory is either 2 GB or 3 GB, depending on whether you have the /3GB flag set in your boot.ini file. Memory with addresses greater than 2 GB or 3 GB is only available indirectly, through AWE-mapped-memory. On a 64-bit SQL Server instance, ‘visible’ memory has no special meaning, as all the memory is directly addressable. In any of the discussion below, if we refer to visible target memory greater than 3 GB, keep in mind that is only possible on a 64-bit SQL Server. The term ‘target’ memory refers to the maximum amount of memory that can be committed to the SQL Server process. Target memory is the lesser of the value you have configured for ‘max server memory’ and the total amount of physical memory available to the operating system. So ‘visible target memory’ is the visible portion of the target memory. Query plans can only be stored in the non-AWE-mapped memory, which is why the concept of ‘visible’ memory is important. You can see a value for visible memory, specified as the number of 8-KB buffers, when you run the DBCC MEMORYSTATUS command. The section called ‘Buffer Counts’ displays values for Visible memory as well as Target memory. 
&lt;P&gt;SQL Server defines a ‘plan cache pressure limit’ value which varies depending on the version you’re running, and the amount of visible target memory.&amp;nbsp; The formula for determining the plan cache pressure limit changed in SQL Server 2005, Service Pack 2.&amp;nbsp;The table below&amp;nbsp;shows how to determine the plan cache pressure limit in SQL Server 2000 and 2005, and indicates the change in Service Pack 2, which reduced the pressure limit with higher amounts of memory. Be aware that these formulas are subject to change again in future service packs. 
&lt;P&gt;&lt;A href="http://sqlblog.com/blogs/kalen_delaney/WindowsLiveWriter/DidYouKnowSP2doesNOTlimittheamountofplan_10E8C/image%7B0%7D%5B3%5D.png"&gt;&lt;IMG style="BORDER-RIGHT:0px;BORDER-TOP:0px;BORDER-LEFT:0px;BORDER-BOTTOM:0px;" height=318 src="http://sqlblog.com/blogs/kalen_delaney/WindowsLiveWriter/DidYouKnowSP2doesNOTlimittheamountofplan_10E8C/image%7B0%7D_thumb%5B1%5D.png" width=610 border=0&gt;&lt;/A&gt;&amp;nbsp; 
&lt;P&gt;As an example, assume we are on SQL Server 2005, Service Pack 1, on a 64-bit SQL Server instance with 28 GB of target memory. The plan cache pressure limit would be 75% of 8GB plus 50% of the target memory over 8 GB (or 50% of 20GB), which is 6GB + 10GB or &lt;STRONG&gt;16 GB.&lt;/STRONG&gt; 
&lt;P&gt;On SQL Server 2005, Service Pack 2, on the 64-bit SQL Server instance with 28 GB of target memory, the plan cache pressure limit would be 75% of 4GB plus 10% of the target memory over 4 GB (or 10% of 24GB), which is 3GB + 2.4GB or &lt;STRONG&gt;5.4 GB&lt;/STRONG&gt;. 
&lt;P&gt;--------------------------------------------------------------- 
&lt;P&gt;There are more details about different caches and global vs local pressure, that I'm not going to go into detail about here. But to simplify, when SQL Server detects that a cache has reached 75-80% of the cache pressure limit, it will consider the SQL Server instance to be under memory pressure. When memory pressure is detected, all zero cost plans will be removed from cache and the cost of all other plans is reduced by half. 
&lt;P&gt;For the rest of the details, you can read the &lt;EM&gt;Query Tuning and Optimization&lt;/EM&gt; book. The point I wanted to make is that there is no hard limit and having memory pressure kick in is a good thing to prevent cache bloat. I have actually heard of people not upgrading to SP2 because they thought it would mean they would have less space for plans in cache. Even under memory pressure, plans that are regularly reused will NOT be removed from cache, so all you'll be doing is getting rid of the clutter, and have more room left for the important stuff. 
&lt;P&gt;Have fun! 
&lt;P&gt;&lt;FONT color=#ff00ff&gt;~Kalen&lt;/FONT&gt; 
&lt;P&gt;&lt;STRONG&gt;&lt;FONT size=2&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=3214" width="1" height="1"&gt;</description><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/internals/default.aspx">internals</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/Inside+SQL+Server/default.aspx">Inside SQL Server</category><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/UK/default.aspx">UK</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>