
Note: Use and implement method 1 because this method fully tested our system.Īll methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4. You can read on in the post to learn about this optimization, or read the whole thing to learn more about parameter sniffing and the different ways you can change SQL Server’s behavior.
#SQL SERVER RECOMPILE UPDATE#
Using the query hint instead means that a compiled plan can be cached, and performance information is available in the DMVs (though it is limited to the most recent execution, for the affected statement only).įor instances running at least SQL Server 2008 build 2746 (Service Pack 1 with Cumulative Update 5), using OPTION (RECOMPILE) has another significant advantage over WITH RECOMPILE: only OPTION (RECOMPILE) enables the Parameter Embedding Optimization. When a stored procedure is created using WITH RECOMPILE option, the execution plan for the procedure is not reused. Explain the difference between RECOMPILE query hint and WITH RECOMPILE option. As a result, no performance information is maintained in DMVs such as sys.dm_exec_query_stats. SQL Server - RECOMPILE query hint and WITH RECOMPILE option - at 22:00 PM by Rajmeet Ghai. Using WITH RECOMPILE also means the compiled plan for the stored procedure is not cached. This query hint results in a recompilation of the problematic statement only execution plans for other statements within the stored procedure are cached and reused as normal. A better alternative, on SQL Server 2005 and later, is to use the OPTION (RECOMPILE) query hint on just the statement that suffers from the parameter-sniffing problem. Using WITH RECOMPILE effectively returns us to SQL Server 2000 behaviour, where the entire stored procedure is recompiled on every execution. Unfortunately, that advice is often misinterpreted to mean adding WITH RECOMPILE option to the stored procedure. When a parameter-sensitivity problem is encountered, a common piece of advice on forums and Q&A sites is to “use recompile” (assuming the other tuning options presented earlier are unsuitable). I’ll quote the relevant bit from Paul White’s blog post, Parameter Sniffing, Embedding, and the RECOMPILE Options: I would opt for statement-level OPTION (RECOMPILE) every time. I don’t think it ever is (unless you are stuck in SQL Server 2000 and are looking for pity – don’t worry, you’ll have it). We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you. I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. So that, when it is preferable to use With Recompile over OPTION(RECOMPILE)? Sometimes there are cases that it may help, but OPTION(RECOMPILE) may help too.

I know what With Recompile means, and I know that it is not suggested at most of the times due to performance issues, no dmv’s, etc. All we need is an easy explanation of the problem, so here it is.
