Session Code: DAT454 Avoiding Stored Procedure Recompiles Dr Greg Low Managing Director Solid Q Australia 2 Who am I? Managing Director Solid Q Australia Host of SQL Down Under Podcast Microsoft Regional Director Microsoft MVP for SQL Server Organizer for SDU Code Camp Co-organizer for CodeCampOz PASS Board Member 3 Acknowledgements/Sources Material for this session has been distilled from: Batch Compilation, Recompilation and Plan Caching Issues in SQL Server 2005 (Arun Marathe – MSDN Library) Query Recompilation in SQL Server 2000 (Thomas Davidson – MSDN Library) Statistical maintenance functionality (autostats) in SQL Server (KB 195565) Troubleshooting stored procedure recompilation (KB 243586) How to identify the cause of recompilation in an SP:Recompile event (KB 308737) Tibor Karaszi’s article on SET options and Recompilation 4 What we will cover Query compilation and plan caching in SQL Server Using tools to view caching and recompiles Recompilation triggers in SQL Server 2000 and 2005/8 Best practices 5 SQL Server Code Execution SQL Server selects an execution plan for a stored procedure (and other query batches) and then compiles it, prior to execution. Concept of a batch After compilation, plans are stored in the procedure cache (Arun calls it a plan cache) 6 SQL Server Code Execution Cached plan reuse is highly desirable Recompilation vs compilation SQL Server may choose not to use a stored plan - (validity & optimality) 7 SQL Server 2005/2008 SQL Server 2000 always recompiled entire batches SQL Server 2005/8 may use statement-level recompilation Faster Less memory Fewer compile locks Avoids the need to break up large stored procedures 8 Pop – Quiz 9 Cached vs Non-Cached Ad-hoc queries (exact match required – case and space sensitivity) Auto-parameterized queries (unless the value of a constant literal can influence the plan – if so, treated as ad-hoc) sp_executesql Stored Procedures Batches (exact match) EXEC () (checks resultant string) 10 Showplan – Auto-parameterization 11 Query Plans Query plans are re-entrant At most, one sequential plan and one parallel plan (no matter how many processors) In SQL Server 2000, serialized action, in 2005 and 2008, multiples may temporarily exist but only one cached 12 Execution Contexts Execution contexts are derived from Query Plans Not re-entrant – one per user executing – data and parameters – multiples per Query Plan possible 13 Execution Contexts (Continued) Not all execution contexts for a single Query Plan are identical (eg branching) Errors of severity 11 or above cause the Execution Context to be destroyed Execution contexts from parallel plans are not cached (but parallel query plans are) 14 Caching In SQL Server 2000 A cost is held for each query plan and execution context SQL Server 2000 – measure of server resources to optimize the batch (note not from executions). Based on: CPU time generating the plan Read page count Write page count Memory pages occupied Lazy writer divides costs by 4 each pass 0 entries (+ ad-hoc queries) are deletion targets Cost gets reset to original value on reuse 15 Caching In SQL Server 2005/2008 Ad-hoc still zero Cost is calculated in ticks (max 31) Two I/O’s cost 1 tick (max 19 ticks) Two context switches cost 1 tick (max 8 ticks) Sixteen memory pages cost 1 tick (max 4 ticks) When procedure cache 50% of buffer pool size, next cache access decrements all ticks by one. If over 75%, a separate resource monitor takes over decrementing. 16 New Option In SQL Server 2008 sp_configure option to: optimize for adhoc workloads Cache only parameterized stubs for adhoc queries Should help for applications with large numbers of non-parameterized batches 17 What we will cover Query compilation and plan caching in SQL Server Using tools to view caching and recompiles Recompilation triggers in SQL Server 2000 and 2005/2008 Best practices 18 sys.syscacheobjects / Pop Quiz Revisited 19 SQL Profiler 20 What we will cover Query compilation and plan caching in SQL Server Using tools to view caching and recompiles Recompilation triggers in SQL Server 2000 and 2005/2008 Best practices 21 Recompilation Reasons 22 Recompilation Triggers Correctness Optimality 23 SET Options (Correctness) Various SET options trigger recompilations due to constant folding Tibor mentions ANSI_DEFAULTS, ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, DATEFORMAT, FORCEPLAN, LANGUAGE, NUMERIC_ROUNDABOUT Arun also adds DATEFIRST, NO_BROWSETABLE, QUOTED_IDENTIFIER 24 Name Resolution Issues (Correctness) Objects with single-part names cannot be resolved until execution time. SQL Server 2000 (owners) Products -> Mary.Products vs Tom.Products SQL Server 2005 and 2008 (schemas) Products -> Production.Products vs Advertising.Products 25 Name Resolution Issues 26 Database Context (Correctness) EXEC dbo.Proc1 EXEC anotherdb.dbo.Proc1 27 Miscellaneous Batch inside vs outside CLR CREATE PROCEDURE … WITH RECOMPILE EXEC … WITH RECOMPILE (but preferred to the CREATE PROC version) Mixing DDL and DML sp_recompile DBCC FREEPROCCACHE/DBCC FLUSHPROCINDB RECONFIGURE Autoclose 28 Mixing DDL and DML 29 Schema Changes Add/drop columns Add/drop indexes (where used in the plan) Manual statistics changes ATTACH/DETACH ONLINE/OFFLINE 30 No Recompile Trivial plans KEEPFIXED_PLAN All read-only tables 31 Optimality Each table has a Recompilation Threshold (RT) Based on number of rows in table rowmodctr (2000), colmodctr (2005/2008) Permanent tables ( <500 = 500, >500 = 500 + 0.2 x n) Temporary tables ( < 6 = 6, else same) Table variables (no RT) rowmodctr, colmodctr not transactional rowmodctr value present but not useful in 2005/2008, use colmodctr 32 Optimality (Continued) INSERT/DELETE count as one UPDATE counts as 2 in 2000 UPDATE counts as 1 in 2005/2008 (non-key) or 2 (key) 33 Working with Statistics 34 Parameter Sniffing Problem with atypical parameters SQL Server 2005/2008 hint for default parameter values 35 Parameter Sniffing and 2005/2008 36 What we will cover Query compilation and plan caching in SQL Server Using tools to view caching and recompiles Recompilation triggers in SQL Server 2000 and 2005/2008 Best practices 37 Best Practices In SQL Server 2000, it may be advantageous to break up large procedures. Avoid SET options from the list of those that cause recompilations. In general, try to work with connection default settings. Keep server, database & connection settings as stable as possible. Avoid name resolution issues by two-part object names. 38 Best Practices (Continued) Avoid WITH RECOMPILE option on CREATE PROCEDURE and EXEC Consider using new SQL Server 2005 hints to minimize the need for these Or execute the query using sp_executesql Consider table variables rather than temporary tables where recompiles are an issue but consider the impact of the lack of statistics 39 Best Practices (Continued) Consider KEEPFIXED_PLAN Avoid manually controlling statistics updates. Avoid mixing DDL and DML Consider cost of recompilations, not just the number (2005/2008 may appear to have more) Avoid literals longer than 8K (never cached). 40 Additional Resources Batch Compilation, Recompilation and Plan Caching Issues in SQL Server 2005 (Arun Marathe – MSDN Library) http://www.microsoft.com/technet/prodtechnol/sql/2005/recomp.mspx Query Recompilation in SQL Server 2000 (Thomas Davidson – MSDN Library) http://msdn.microsoft.com/library/default.asp?url=/library/enus/dnsql2k/html/sql_queryrecompilation.asp Statistical maintenance functionality (autostats) in SQL Server (KB 195565) http://support.microsoft.com/default.aspx?scid=kb;EN-US;195565 41 Additional Resources (Continued) Troubleshooting stored procedure recompilation (KB 243586) http://support.microsoft.com/default.aspx?scid=kb;en-us;243586 How to identify the cause of recompilation in an SP:Recompile event (KB 308737) http://support.microsoft.com/?id=308737 Tibor Karaszi’s article on SET options and Recompilation http://www.karaszi.com/SQLServer/info_sp_recompile_set.asp 42 43 Thank You! [email protected] http://sqlblog.com/blogs/greg_low http://www.sqldownunder.com 44 Related Content Breakout Sessions (session codes and titles) Interactive Theater Sessions (session codes and titles) Hands-on Labs (session codes and titles) Hands-on Labs (session codes and titles) Track Resources Resource 1 Resource 2 Resource 3 Resource 4 Resources for IT Professionals www.microsoft.com/teched Tech·Talks Live Simulcasts Tech·Ed Bloggers Virtual Labs http://microsoft.com/technet Evaluation licenses, pre-released products, and MORE! 1 Year Subscription! Complete an evaluation on CommNet and enter to win! © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. 49
© Copyright 2026 Paperzz