Presentation is loading. Please wait.

Presentation is loading. Please wait.

2 Avoiding Stored Procedure Recompiles Dr Greg Low Managing Director Solid Q Australia Session Code: DAT454.

Similar presentations


Presentation on theme: "2 Avoiding Stored Procedure Recompiles Dr Greg Low Managing Director Solid Q Australia Session Code: DAT454."— Presentation transcript:

1

2 2 Avoiding Stored Procedure Recompiles Dr Greg Low Managing Director Solid Q Australia Session Code: DAT454

3 3 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

4 4 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 Acknowledgements/Sources

5 5 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 What we will cover

6 6 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) SQL Server Code Execution

7 7 Cached plan reuse is highly desirable Recompilation vs compilation SQL Server may choose not to use a stored plan - (validity & optimality) SQL Server Code Execution

8 8 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 SQL Server 2005/2008

9 9 Pop – Quiz

10 10 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) Cached vs Non-Cached

11 11 Showplan – Auto-parameterization

12 12 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 Query Plans

13 13 Execution contexts are derived from Query Plans Not re-entrant – one per user executing – data and parameters – multiples per Query Plan possible Execution Contexts

14 14 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) Execution Contexts (Continued)

15 15 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 Caching In SQL Server 2000

16 16 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. Caching In SQL Server 2005/2008

17 17 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

18 18 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 What we will cover

19 19 sys.syscacheobjects / Pop Quiz Revisited

20 20 SQL Profiler

21 21 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 What we will cover

22 22 Recompilation Reasons

23 23 Correctness Optimality Recompilation Triggers

24 24 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 SET Options (Correctness)

25 25 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 Name Resolution Issues (Correctness)

26 26 Name Resolution Issues

27 27 EXEC dbo.Proc1 EXEC anotherdb.dbo.Proc1 Database Context (Correctness)

28 28 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 Miscellaneous

29 29 Mixing DDL and DML

30 30 Add/drop columns Add/drop indexes (where used in the plan) Manual statistics changes ATTACH/DETACH ONLINE/OFFLINE Schema Changes

31 31 Trivial plans KEEPFIXED_PLAN All read-only tables No Recompile

32 32 Each table has a Recompilation Threshold (RT) Based on number of rows in table rowmodctr (2000), colmodctr (2005/2008) Permanent tables ( 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 Optimality

33 33 INSERT/DELETE count as one UPDATE counts as 2 in 2000 UPDATE counts as 1 in 2005/2008 (non-key) or 2 (key) Optimality (Continued)

34 34 Working with Statistics

35 35 Problem with atypical parameters SQL Server 2005/2008 hint for default parameter values Parameter Sniffing

36 36 Parameter Sniffing and 2005/2008

37 37 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 What we will cover

38 38 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. Best Practices

39 39 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 Best Practices (Continued)

40 40 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). Best Practices (Continued)

41 41 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/en- us/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 Additional Resources

42 42 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

43 43

44 44 Thank You! glow@solidq.com http://sqlblog.com/blogs/greg_low http://www.sqldownunder.com

45 Related Content Breakout Sessions (session codes and titles) Interactive Theater Sessions (session codes and titles) Hands-on Labs (session codes and titles)

46 Track Resources Resource 1 Resource 2 Resource 3 Resource 4

47 www.microsoft.com/teched Tech·TalksTech·Ed Bloggers Live SimulcastsVirtual Labs http://microsoft.com/technet Evaluation licenses, pre-released products, and MORE! Resources for IT Professionals

48 Complete an evaluation on CommNet and enter to win! 1 Year Subscription!

49 49 © 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.


Download ppt "2 Avoiding Stored Procedure Recompiles Dr Greg Low Managing Director Solid Q Australia Session Code: DAT454."

Similar presentations


Ads by Google