How to remove a specific statement execution plan cache

SQL server to run a certain time, cached execution plan can be quite large, and some can be the size of several GB. This time assume a more complex statements and SQL server generates less than optimal execution plan, the execution plan you want to clear the cache so that the SQL server can recompile the statement. How?

 

If the stored procedure is well run, sp_recompile directly on it, as shown below. If the argument is a table, then use the table of all stored procedure or trigger will be recompiled in order to replace the original plan:

 

USE AdventureWorks;

GO

EXECsp_recompileN'Sales.Customer';

GO

 

If it is a general statement? For example, the following statement:

 

use AdventureWorks

go

 

 

http://blogs.msdn.com/b/apgcdsd/archive/2013/03/13/10401823.aspx

SELECT*FROM Sales.SalesOrderHeader h, Sales.Customer c,Sales.SalesTerritory t

WHERE h.CustomerID = c.CustomerID

AND c.TerritoryID = t.TerritoryID

AND CountryRegionCode = N'CA';

 

I executed the above statement several times to observe the cache under the implementation of the plan:

 

SELECT usecounts,text,plan_handle,*FROMsys.dm_exec_cached_plans cp

CROSSAPPLY sys.dm_exec_query_plan(cp.plan_handle)

CROSSAPPLY sys.dm_exec_sql_text (cp.plan_handle)

wheretextlike

'%SELECT * FROM Sales.SalesOrderHeader h, Sales.Customer c,Sales.SalesTerritory t

WHERE h.CustomerID = c.CustomerID

AND c.TerritoryID = t.TerritoryID

AND CountryRegionCode%';

 

The results obtained are as follows:

 

 

Usecounts indicates that the statement is executed seven times. If the execution plan of this statement is not good, how to delete it it?

 

If SQL server 2008 R2 is very easy to handle, and then passed directly DBCC FREEPROCCACHE plan handle as you can, such as:

 

DBCC FREEPROCCACHE(0x060001002903DC0B4001B887000000000000000000000000)

 

But SQL server FREEPROCCACHE 2005 and no such usage. If you run inside SQL 2005 DBCC FREEPROCCACHE then all of the cache will be cleared. This relatively large impact on performance, because SQL server to recompile all of the statements and then re-generate the cache. SQL server 2005 there have no other way to clear the cache only specific statement of it?

 

Yes, the answer using the plan guide on the use of the following:

 

sp_create_plan_guide

@name =  N'recompile_Guide',

@stmt =

N'SELECT * FROM Sales.SalesOrderHeader h, Sales.Customer c,Sales.SalesTerritory t

WHERE h.CustomerID = c.CustomerID

AND c.TerritoryID = t.TerritoryID

AND CountryRegionCode = N''CA'';',

@type = N'SQL',

@module_or_batch =NULL,

@params =NULL,

@hints = N'OPTION (RECOMPILE)'

go

exec sp_control_plan_guide N'drop',N'recompile_Guide'

 

Sp_create_plan_guide use RECOMPILE above parameters, meaning that each encounter the statement must be recompiled. After sp_create_plan_guide run, execute the plan cache for the statement was deleted, the next statement is executed again will be recompiled. So why do I immediately delete this plan guide it? Because after the statement of the buffer is cleared, I do not want the statement to recompile each execution, so I deleted it, after all, I perform purpose is to remove the statement sp_create_plan_guide execution plan cache only. So if you use the same means, be sure to remember to immediately remove the guide sp_create_plan_guide established.

Reproduced in: https: //www.cnblogs.com/flysun0311/p/3140118.html

Guess you like

Origin blog.csdn.net/weixin_34221036/article/details/93444266