简体   繁体   English

SQL Server:在proc定义中使用'WITH RECOMPILE'的效果?

[英]SQL Server: Effects of using 'WITH RECOMPILE' in proc definition?

My understanding of the WITH RECOMPILE option with stored procedures is generally limited to using the clause with a single stored proc call as a trailing parameter: 我对存储过程的WITH RECOMPILE选项的理解通常仅限于使用带有单个存储过程调用的子句作为尾随参数:

exec sp_mystoredproc 'Parameter1', 2, '1/28/2011' with recompile

What are the effects of including WITH RECOMPILE in the actual proc definition? 在实际的proc定义中包含WITH RECOMPILE有什么影响? Does this recompile the proc every time it's executed? 这会在每次执行时重新编译proc吗? Or just the next time the proc is altered? 或者只是下次更改过程?

Example: 例:

CREATE PROCEDURE [dbo].[sp_mystoredproc]
    (@string1           varchar(8000)
    ,@int2              int = 2
    ,@dt_begin          DATETIME
    with recompile
AS
... proc code ...

This makes the proc rebuild the plans of all queries every time it's run. 这使得proc在每次运行时都会重建所有查询的计划。

Useful it the values of the proc parameters affect the filter selectivity. 有用的是,proc参数的值会影响过滤器的选择性。

Say, the optimal plan for this query: 比如说,这个查询的最佳计划:

SELECT  *
FROM    orders
WHERE   order_date BETWEEN @begin_report AND @from_report

will be a full scan if the date range is large or an index scan if it's small. 将是一个完整的扫描,如果日期范围较大索引扫描,如果是小的。

Created using WITH RECOMPILE , the proc will build the plan on each execution; 使用WITH RECOMPILE创建,proc将在每次执行时构建计划; without one, it will stick to a single plan (but will save time on recompilation itself). 没有一个,它将坚持一个计划(但会节省重新编译本身的时间)。

This hint is usually used in procs processing large volumes of data and doing complex reports, when the overall query time is large and time for rebuilding the plan is negligible compared with the time saved by a better plan. 当整体查询时间很长并且重建计划的时间与更好的计划所节省的时间相比可以忽略不计时,此提示通常用于处理大量数据和执行复杂报告的过程中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM