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

前端 未结 3 1967
鱼传尺愫
鱼传尺愫 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:40

    I have used both. OPTION(OPTIMIZE FOR UNKNOWN) was used for a heavy search stored procedure that took in a variety of parameters. There were certain conditions, unknown to me (statistics and what not), that would throw the optimization off, the query was mundane, however, it would cause serious delays (and even time out). OPTION(OPTIMIZE FOR UNKNOWN) solved this issue but was not ideal.

    The same heavy search procedure would have intermittent issues, meaning after a few months, the search would time out. The immediate solution would be to call sp_recompile , which is synonymous to adding a OPTION(RECOMPILE) clause to the stored procedure.

    The guts of the stored procedure propelled a "result as you type" solution in which every three keystrokes would trigger a DB search and the results would populate in a dropdown.

    In the end, I removed the OPTION(OPTIMIZE FOR UNKNOWN) and just simply added a EXEC sp_recompile to my nightly maintenance job and that resolved all issues.

提交回复
热议问题