![]() 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. 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. ![]() Put the OPTION (RECOMPILE) on the query that is using the function. SQL SERVER Parameter Sniffing and OPTION (RECOMPILE) 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. What happens when a statement has OPTION(RECOMPILE) 1 Answer Sorted by: 12 Inline Table Valued Functions are expanded out into the calling query so nothing is ever actually called. This new database level enhancement was introduced recently which can help you overcome any issue with the parameter sniffing. The generated query plan doesn't replace a plan stored in cache when the same query runs without the RECOMPILE hint. There are a variety of reasons the database engine may choose to recompile objects. I’ve used it before without fully understanding the impact. Instructs the SQL Server Database Engine to generate a new, temporary plan for the query and immediately discard that plan after the query completes execution. SQL Server automatically recompiles stored procedures, triggers, and user-defined functions when it is advantageous. I wish I knew that when I started query tuning. ![]()
0 Comments
Leave a Reply. |