您的位置:首页 > 其它

调优Stored Procedure,强制sp recompile

2015-10-23 14:24 423 查看
在调优Stored Proedure时,如果在Product Server上,清空plan cache,代价太大,可以使用以下三种方式,使指定的sp重新编译。

1, sys.sp_recompile

Causes stored procedures, triggers, and user-defined functions to be recompiled the next time that they are run. It does this by dropping the existing plan from the procedure cache forcing a new plan to be created the next time that the procedure or trigger is run. In a SQL Server Profiler collection, the event SP:CacheInsert is logged instead of the event SP:Recompile.

sp_recompile [ @objname = ] 'object'


Arguments

[ @objname = ] 'object'
The qualified or unqualified name of a stored procedure, trigger, table, view, or user-defined function in the current database. object is nvarchar(776), with no default. If object is the name of a stored procedure, trigger, or user-defined function, the stored procedure, trigger, or function will be recompiled the next time that it is run. If object is the name of a table or view, all the stored procedures, triggers, or user-defined functions that reference the table or view will be recompiled the next time that they are run.

sp_recompile looks for an object in the current database only.

The queries used by stored procedures, or triggers, and user-defined functions are optimized only when they are compiled. As indexes or other changes that affect statistics are made to the database, compiled stored procedures, triggers, and user-defined functions may lose efficiency. By recompiling stored procedures and triggers that act on a table, you can reoptimize the queries.

2,WITH RECOMPILE 选项

有两种方式,第一种是Create Procedure 时使用,一种是在exec sp 时使用

CREATE PROCEDURE dbo.usp_procname
@Parameter_Name varchar(30) = 'Parameter_default_value'
WITH RECOMPILE
as
begin

--sp body

end


exec dbo.usp_procname @Parameter_name='Parameter_value' WITH RECOMPILE


3,option(recompile),在sp中进行语句级别的recompile

select column_name_list
from dbo.tablename
option(recompile)


RECOMPILE Hint

Instructs the SQL Server Database Engine to discard the plan generated for the query after it executes, forcing the query optimizer to recompile a query plan the next time the same query is executed. Without specifying RECOMPILE, the Database Engine caches query plans and reuses them. When compiling query plans, the RECOMPILE query hint uses the current values of any local variables in the query and, if the query is inside a stored procedure, the current values passed to any parameters.

RECOMPILE is a useful alternative to creating a stored procedure that uses the WITH RECOMPILE clause when only a subset of queries inside the stored procedure, instead of the whole stored procedure, must be recompiled. For more information, see Recompile a Stored Procedure. RECOMPILE is also useful when you create plan guides.

参考文档:
https://msdn.microsoft.com/en-us/library/ms181647.aspx https://msdn.microsoft.com/zh-cn/library/ms190439.aspx https://msdn.microsoft.com/zh-cn/library/ms181714.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: