<?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 : syscacheobjects</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/syscacheobjects/default.aspx</link><description>Tags: syscacheobjects</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><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/DBCC/default.aspx">DBCC</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/plan+guides/default.aspx">plan guides</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/syscacheobjects/default.aspx">syscacheobjects</category></item></channel></rss>