
This issue can occur when multiple connections concurrently run this query by using different parameters. I converted it to an ITVF using cascading CTEs and achieved a 93.8% reduction in run time over a 1M row test table. Fixes a rare problem in which incorrect results are returned when you run a query that uses Option(Recompile) in SQL Server 2014 or SQL Server 2012. There was a loop over the characters and several checks involved. For third party vendors who have somehow developed software that uses SQL Server for decades without running into a single best practice even by accident, it’s often harder to get those changes through. In a recent redesign of an ETL system, the previous author had an MTVF to calculate a custom checksum for a given string of numbers. But all that assumes that those options are immediately available. Piling on, ITVFs are definitely the way to go.

It's used differently that a Scalar Function but it's worth it for reduced CPU and increased performance. It does not have to be valid for any other value of the parameter.

SELECT FROM H h JOIN D d ON d.idH h.id WHERE h.id 1 The generated plan has to be valid for this specific value of the parameter. write it as an "iSF" (Inline Scalar Function). With OPTION(RECOMPILE) optimiser knows the value of the hid variable and essentially generates the plan, as if you wrote. What do you do if you need a Scalar Function? Simple. The iTVF works more like a parameterized view and (depending on how the code is written, of course) is as fast as if the code were written "inline". The first two are terrible for both performance and resource usage. Scalar Function, Multi-statement Table Valued Function (mTVF), and Inline Table Valued Functions (iTVFs). If you look in "Books Online", you'll find that there are 3 types of functions. 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.Kevin is correct. SET STATISTICS TIME ON Įvery time that statement runs, it will be compiled again, costing those 5 ms of CPU time.
syntaxsql Copy OPTION (Transact-SQL Syntax Conventions Syntax Syntax for SQL Server and Azure SQL Database. This clause can be specified in the SELECT, DELETE, UPDATE and MERGE statements. JOIN Users as U2 on Posts.LastEditorUserId = U2.Id Only one OPTION clause can be specified with the statement. 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. Im fairly comfortable with writing SQL but have never used an OPTION command in a query before and was unfamiliar with the whole. 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.
