Published on

April 28, 2013

Understanding Statement Recompilation in SQL Server

As a SQL Server enthusiast, I often come across moments where I realize there is still so much to learn about this powerful database management system. Today, I stumbled upon the sys.dm_xe_map_values dynamic management view (DMV) and discovered a wealth of information about statement recompilation.

Statement recompilation occurs when SQL Server needs to recompile a previously compiled query or stored procedure. This can happen for various reasons, and understanding these reasons can greatly enhance our understanding of SQL Server’s behavior.

To explore the different reasons for statement recompilation, we can query the sys.dm_xe_map_values DMV. This DMV provides a list of all the possible reasons for statement recompilation.

SELECT dxmv.name, dxmv.map_key, dxmv.map_value
FROM sys.dm_xe_map_values AS dxmv
WHERE dxmv.name = N'statement_recompile_cause'
ORDER BY dxmv.map_key;

When executing this query, we obtain a list of 14 different reasons for statement recompilation:

  1. Schema changed
  2. Statistics changed
  3. Deferred compile
  4. Set option change
  5. Temp table changed
  6. Remote rowset changed
  7. For browse permissions changed
  8. Query notification environment changed
  9. PartitionView changed
  10. Cursor options changed
  11. Option (recompile) requested
  12. Parameterized plan flushed
  13. Test plan linearization
  14. Plan affecting database version changed

These reasons cover a wide range of scenarios where SQL Server may need to recompile a statement. Understanding each of these reasons can provide valuable insights into SQL Server’s behavior and performance.

Building test cases for each compilation reason can be a great learning experience. By simulating these scenarios, we can observe how SQL Server handles statement recompilation and gain a deeper understanding of its inner workings.

Next time you encounter a statement recompilation scenario, refer to this list of reasons to help troubleshoot and optimize your SQL Server environment.

Happy coding!

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.