What are the main differences between OPTION(OPTIMIZE FOR UNKNOWN) and OPTION(RECOMPILE)?

前端 未结 3 1847
鱼传尺愫
鱼传尺愫 2020-12-30 04:13

I run into the classic Parameter Sniffing issues in SQL Server 2012. Based on some research I found multiple options around this problem. The two options that I need to unde

3条回答
  •  被撕碎了的回忆
    2020-12-30 04:42

    Will OPTION(OPTIMIZE FOR UNKNOWN) reuse cache instead of recompiling each time?

    Yes. Optimize for unknown will influence how the plan is generated (i.e. explicitly prevent it from sniffing parameters and compare it with column data histogram), but once generated the plan stays in cache and is reused.

    OPTION(RECOMPILE) will force a recompile on every execution and is a rather heavy handed approach. It makes sense only in an analytical DW/BI environments where each query may be different, complex and probably with a significant run time.

    You also have other options at your disposal:

    • Plan Guides
    • Query Store (only in SQL 2016) as it allows you to 'pin' a favoured execution plan

    Both of these allow you to obtain the same effect as in your post, but in a non-invasive way (no app code/query changes).

提交回复
热议问题