

Batch mode over row store allows adaptive joins to be applied over row mode queries.Adaptive joins can solve problems with join selection caused by parameter sniffing.Memory Grant Feedback can solve some memory allocation problems caused by parameter sniffing.Query Store can identify queries suffering with parameter sniffing.Microsoft is aware of this and SQL Server created many features to help us solve the parameter sniffing problem. However, the solution has a price: We are losing the power of the query plan cache, forcing a recompilation on every execution, what affects performance. We can apply the clause ‘Option (Recompile)’ to a query, even if the query is inside a stored procedure.This will cause the procedure to be recompiled before the execution. We can apply the ‘With Recompile’ clause to the ‘Execute’ statement when executing the stored procedure.It will make the procedure be recompiled on every execution. We can apply the clause ‘With Recompile’ to a stored procedure.In order to ensure the plan will be recompiled on every execution, we have some different options: The memory allocated can become too much, or to low, and if it’s too low, it will cause spills to tempdb and performance problems, and this will be terrible for performance.A join can be made using nested join or hash join, according to the number of rows returned.A key lookup can be applied if the number of rows returned is small or an index scan will be applied if the number of rows returned is too big.Here are some examples about how the plan can be affected: Applying a recompile option solves the parameter sniffing problem because the query will be recompiled for each set of parameters, ensuring the best query plan possible. As a result, the perfect plan for the query will be different according to the value used on the predicate. The parameter sniffing problem appears when the data is uneven distributed on one column used as predicate. If the development team gets used to this, suddenly you will see recompile options all around. The usual solution we recommend is to apply a recompile option, rather to a stored procedure or a query. Parameter sniffing is a common challenge for query tuning. Summary of the downsidesĮach statement with option recompile consumes extra cpu and memory and doesn’t store the execution plan in cache, preventing performance tuners from seeing metrics like total execution count, or total worker time in dm_exec_query_stats.Parameter Sniffing or not sniffing: The Mindset change for new technologies - Simple Talk Skip to content

SET STATISTICS TIME ON Įvery time that statement runs, it will be compiled again, costing those 5 ms of CPU time. JOIN Users as U2 on Posts.LastEditorUserId = U2.Id

JOIN Users as U on Posts.OwnerUserId = U.Id To see the cost of compilation, just use statistics time. Okay, but what about the cost of compilation? To prove that, I’ll right-click on the top SELECT INTO and view the properties. exec 4Įach “query” in this example is a separate statement. Then, I’ll get the actual execution plan for post type 4. I’ll run the stored procedure with the value of 3, first. This stored procedure has OPTION(RECOMPILE) on only one statement INT) asįROM Posts where PostTypeId = PostTypeId, Tags, Body Except for this example, I can’t use the estimated execution plan because there’s a temp table. I’m going to re-use the stored procedure from this post on parameter sniffing and using the estimated execution plan. There’s also the fact that the hint applies strictly to the statement level, not the entire query. Using option recompile will use extra cpu and memory every time the statement compiles. I have a few reasons why this hint is dangerous. In fact, the statement with option recompile won’t be stored in cach e. It also means that the statement itself won’t be vulnerable to parameter sniffing from other queries in cache. There’s some benefits, like something called “ constant folding.” To us, that just means that the execution plan might be better than a normal execution plan compiled for the current statement. SQL Server will compile an execution plan specifically for the statement that the query hint is on. What happens when a statement has OPTION(RECOMPILE)

I’ve used it before without fully understanding the impact. I wish I knew that when I started query tuning.
