When Does SQL Server Compile a New Query Plan?
SQL Server is clever. It does not compile a new plan for every execution — it does so when compilation is needed. Before executing a query, the engine checks the plan cache. If a suitable cached plan exists, it may reuse it.
🔍 When is a new plan created?
Compilation or recompilation may occur when:
- no suitable plan is available in the cache,
- statistics change and a relevant recompile threshold is reached,
- a recompilation option is used (
RECOMPILE,OPTION (RECOMPILE)), - Parameter Sensitive Plan Optimization selects among previously compiled variants for different parameter ranges; selection itself does not necessarily mean a new compilation,
- or a plan is evicted from the cache, for example after
DBCC FREEPROCCACHE.
🧠 How can you inspect this?
| |
🧩 Why does it matter?
Frequent recompilation can consume unnecessary CPU. On the other hand, overly aggressive reuse can expose parameter sniffing problems. Balancing these behaviors is one of the keys to SQL Server performance.