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

后端 未结 1 974
傲寒
傲寒 2020-12-15 23:05

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 param

相关标签:
1条回答
  • 2020-12-15 23:35

    This makes the proc rebuild the plans of all queries every time it's run.

    Useful it the values of the proc parameters affect the filter selectivity.

    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; 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.

    0 讨论(0)
提交回复
热议问题