
Parameter Sniffing is the process of looking to the first passed parameters values when compiling the stored procedure in order to create an optimal execution plan that fits these parameters values and use it for all values. What is parameter sniffing in SQL Server? This does not execute the procedure but it does mark the procedure to be recompiled so that its query plan is updated the next time that the procedure is executed. Your server is under a heavy load that is causing each of these procedures to take more time than usual. This procedure is run on multiple concurrent connections that have different sets of parameter values. Select New Query, then copy and paste the following example into the query window and click Execute. Conversely the 22 hour plan (with option recompile) estimates a higher row count of 8,659. It has a WHERE clause that contains parameters. To recompile a stored procedure by using sp_recompile
#Option recompile sql server how to#
There is no rule on how to make this decision, it depends on knowing the way the query will be used by users. To tell the Engine this is a 'Single Use Plan' and not to use a Cached Plan which likely does not apply. The oldest and most traditional technique to not cache the query plans and compile your stored procedure or queries every single time to get optimal performance. In such a situation you can tell the SQL statement to use the option OPTION (RECOMPILE) or an SP to use WITH RECOMPILE. Now, we have got the complete detailed explanation and answer for everyone, who is interested! What happens when a statement has OPTION (RECOMPILE) SQL Server will compile an execution plan specifically for the statement that the query hint is on. Im fairly comfortable with writing SQL but have never used an OPTION command in a query before and was unfamiliar with the whole. 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.This is a question our experts keep getting from time to time. 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
#Option recompile sql server update#
JOIN Users as U on Posts.OwnerUserId = U.Id The estimated recompile threshold starts an automatic recompile for the query when the estimated number of indexed column changes have been made to a table by running one of the following statements: UPDATE DELETE MERGE INSERT Specifying KEEP PLAN makes sure a query won't be recompiled as frequently when there are multiple updates to a table. 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.

But again the choice is between using the RECOMPILE option in adhoc T-SQL or, the basic rules to be followed (same structure, same columns, same tables, fully qualified names, etc) for plan reuse. I have a few reasons why this hint is dangerous. Usage of the RECOMPILE option would help reduce the utilization of plan cache in such cases. In fact, the statement with option recompile won’t be stored in cach e. If this is the kind of SQL Server stuff you love learning about, you’ll love my training. It also means that the statement itself won’t be vulnerable to parameter sniffing from other queries in cache. You can only do that by adding OPTION(RECOMPILE) to the query, like this. 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.
