Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 understand the difference between are OPTION(OPTIMIZE FOR UNKNOWN) vs OPTION(RECOMPILE).

I am hesitating to use OPTION(RECOMPILE) at the end of my queries that are having this issue because it will force the server to generate a new execution plan each time. If I call this query often this will spike up the CPU of that machine.

So that I use he best available solution, what are the real differences between the two options?

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

like image 739
Junior Avatar asked Nov 04 '16 22:11

Junior


People also ask

What is optimize for unknown?

The 'Optimize for Unknown' feature follows the premise that trying to get a consistent execution time for a given set of parameters and re-using a stable execution plan is better than spending CPU to compile a special, unique flower of an execution plan every time a query runs.

What is option recompile?

What is the RECOMPILE option? The compilation is the process when a query execution plan of a stored procedure is optimized based on the current database objects state. This query execution plan is often stored in the cache to be quickly accessed. Recompilation is the same process as a compilation, just executed again.

What is the use of with recompile statement?

RECOMPILE – specifies that after the query is executed, its query execution plan stored in cache is removed from cache. When the same query is executed again, there will be no existing plan in cache, so the query will have to be recompiled.

What is the use of $Hint option in a wrapped query?

The FAST 'N' query hint allows the optimizer to return a specified number of rows as quickly as possible for SQL Queries. Imagine you have a custom application where users put a specific condition and wait for data to appear.


2 Answers

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

Yes, it will.


There are two main differences between OPTION(OPTIMIZE FOR UNKNOWN) and OPTION(RECOMPILE) as can be seen from this quote from MSDN:

OPTIMIZE FOR UNKNOWN

Instructs the query optimizer to use statistical data instead of the initial values for all local variables when the query is compiled and optimized, including parameters created with forced parameterization.

RECOMPILE

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.

So, the two main differences are:

  1. Caching (or not) of the query plan.

Usually the generated query plan is cached and reused. OPTIMIZE FOR UNKNOWN doesn't affect this feature of the engine. RECOMPILE suppresses this feature and tells the engine to discard the plan and not put it into the cache.

  1. Using (or not) actual parameter values during plan generation.

Usually optimizer "sniffs" the parameter values and uses these values when generating the plan. OPTIMIZE FOR UNKNOWN suppresses this feature and tells the engine to treat all parameters as if their values were unknown. Optimizer has built-in rules and heuristics how to use available statistics for various filtering criteria. See Optimize for… Mediocre? for more details. Normally parameter sniffing is used on the first run of the query/stored procedure and uses the values of parameters during the first run. The generated plan is cached and later can be reused.

One non-obvious thing to remember here is that in both cases (normal without any query hints and with OPTIMIZE FOR UNKNOWN hint) the generated plan has to be valid and produce correct result for any possible parameter value. It is tailored to the sniffed values that were used during the first run in the normal/no-hint case; it is not tailored to any specific value in the OPTIMIZE FOR UNKNOWN case, but it is still valid if parameter changes later in any way.

This is significant and it prevents optimizer from performing certain transformations and simplifications of the plan.

OPTION(RECOMPILE) allows optimizer to inline the actual values of parameters during each run and optimizer uses actual values of parameters to generate a better plan. It doesn't have to worry that the generated plan may not work with some other value of parameter, because the plan will not be cached and reused.

This effect is mostly visible for the Dynamic Search Conditions queries. For example:

SELECT ... FROM T WHERE     (@ParamSomeID = 0)     OR     (         @ParamSomeID = -1         AND         T.SomeID NOT IN         (             SELECT OtherTable.SomeID             FROM OtherTable         )     )     OR     (         T.SomeID IN         (             SELECT OtherTable.SomeID             FROM OtherTable             WHERE OtherTable.SomeID = @ParamSomeID         )     ) OPTION(RECOMPILE) 

If @ParamSomeID is 0 optimizer would treat the query as if it didn't have any WHERE clause at all. The plan would not mention OtherTable at all.

If @ParamSomeID is -1, the plan would join T to OtherTable using Left Anti Semi Join and would scan the whole OtherTable.

If @ParamSomeID is, say, 5, the plan would do an index seek in unique index on OtherTable and read only one row from OtherTable.

Without OPTION(RECOMPILE) this kind of simplification and transformation would not happen.

Another reason to use OPTION(RECOMPILE) is when your data distribution is very skewed. For example, you have a table with 1M rows. One column has value 0 in 990K rows and values from 1 to 10 in 1K rows. The queries that filter on this column should have different plans depending on the actual value of the filter.

In both examples above OPTIMIZE FOR UNKNOWN would generate a mediocre plan.

like image 124
Vladimir Baranov Avatar answered Oct 04 '22 00:10

Vladimir Baranov


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

like image 35
Remus Rusanu Avatar answered Oct 04 '22 00:10

Remus Rusanu