调优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.
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 时使用
3,option(recompile),在sp中进行语句级别的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
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
相关文章推荐
- Linux diff与patch的深入分析
- Linux设置默认启动命令行,而不是图形界面
- android 获取控件高度
- C++中的函数指针
- Java语言基础(三)
- JAVA的初始化的坑以及内存分配
- C#执行定时计算限制操作(计时器)
- nodejs+phantomjs+casperjs
- 文件下载
- 项目稳定回报论证—大数据进入价值变现的2.0时代
- Android项目:简易版QQ的实现
- ios开发错误日志获取以及上传邮箱
- HTTPS
- GPS 射频调试总结
- ionic实现双击返回键退出功能
- pat1054The Dominant Color (20)
- 软件开发的文档与审查
- CruiseControl.net
- Android编程实现将压缩数据库文件拷贝到安装目录的方法
- Android实现输入手机号时自动添加空格