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

Slides:



Advertisements
Similar presentations
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Advertisements

© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Identity Management - Login © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Reprint Outstanding Transactions Report © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
Understanding Parameter Sniffing Benjamin Nevarez Blog: benjaminnevarez.com 1.
Feature: Purchase Requisitions - Requester © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
MIX 09 4/15/ :14 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Co- location Mass Market Managed Hosting ISV Hosting.
Dual Partitioning for improved performance in VLDBs Ashwin Rao Karavadi, Rakesh Parida Microsoft IT.
Windows 7 Training Microsoft Confidential. Windows ® 7 Compatibility Version Checking.
Feature: Purchase Order Prepayments II © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: OLE Notes Migration Utility
Session 1.
Built by Developers for Developers…. © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
 Rico Mariani Architect Microsoft Corporation.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Assign an Item to Multiple Sites © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Atlanta SQL Server Users Group April 10, 2006 Stored Procedure Best Practices Kevin Kline Director of Technology Quest Software.
Connect with life Connect with life
Windows Azure Connect Name Title Microsoft Corporation.
NEXT: Overview – Sharing skills & code.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Customer Combiner and Modifier © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
Feature: Employee Self Service Timecard Entry © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
Mark Johnston Development Lead - MSDN Microsoft Corporation TL42.
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.

customer.
demo © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
demo Demo.
Connect with life Nauzad Kapadia Quartz Systems
Feature: Void Historical/Open Transaction Updates © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
demo QueryForeign KeyInstance /sm:body()/x:Order/x:Delivery/y:TrackingId1Z
DAT410 SQL Server 2005 Optimizing Procedural Code Kimberly L. Tripp President/Founder, SQLskills.com.
projekt202 © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
The CLR CoreCLRCoreCLR © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks.
Gail Shaw XpertEase DAT 305 Topics Background Information Query Hints Plan Cache Metadata Plan Guides Plan Freezing Monitoring Plan Guide Use.
Pinal Dave Mentor | Solid Quality India |
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
Maciej Pilecki Consultant Project Botticelli Ltd. DAT404.
2 Common ASP.NET production issues and how to troubleshoot them with WinDbg Tess Ferrandez - Norlander Support Escalation Engineer Microsoft Session Code:
Connect with life Praveen Srivatsa Founder and CEO – AstraSoft.NET Vinod Kumar Technology Evangelist – Databases and BI.

Stored Procedures – Facts and Myths
Tech·Ed North America /31/2018 4:35 PM
Возможности Excel 2010, о которых следует знать
Optimizing Microsoft SQL Server 2008 Applications Using Table Valued Parameters, XML, and MERGE
Peeking into the Plan Cache with SQL Server 2008
Title of Presentation 11/22/2018 3:34 PM
Baseline: How Are We Doing Now?
Title of Presentation 12/2/2018 3:48 PM
Optimizing SQL Server Performance in a Virtual Environment
8/04/2019 9:13 PM © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Виктор Хаджийски Катедра “Металургия на желязото и металолеене”
Title of Presentation 5/12/ :53 PM
Шитманов Дархан Қаражанұлы Тарих пәнінің
Title of Presentation 5/24/2019 1:26 PM
日本初公開!? Vista の新機能を実演 とっちゃん わんくま同盟 7/23/2019 9:09 AM
Title of Presentation 7/24/2019 8:53 PM
Presentation transcript:

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

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 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 ) Troubleshooting stored procedure recompilation (KB ) How to identify the cause of recompilation in an SP:Recompile event (KB ) Tibor Karaszi’s article on SET options and Recompilation Acknowledgements/Sources

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 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 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 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 Pop – Quiz

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 Showplan – Auto-parameterization

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 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 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 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 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 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 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 sys.syscacheobjects / Pop Quiz Revisited

20 SQL Profiler

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 Recompilation Reasons

23 Correctness Optimality Recompilation Triggers

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 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 Name Resolution Issues

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

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 Mixing DDL and DML

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

31 Trivial plans KEEPFIXED_PLAN All read-only tables No Recompile

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

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

36 Parameter Sniffing and 2005/2008

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 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 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 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 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) us/dnsql2k/html/sql_queryrecompilation.asp Statistical maintenance functionality (autostats) in SQL Server (KB ) Additional Resources

42 Additional Resources (Continued) Troubleshooting stored procedure recompilation (KB ) How to identify the cause of recompilation in an SP:Recompile event (KB ) Tibor Karaszi’s article on SET options and Recompilation

43

44 Thank You!

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

Track Resources Resource 1 Resource 2 Resource 3 Resource 4

Tech·TalksTech·Ed Bloggers Live SimulcastsVirtual Labs Evaluation licenses, pre-released products, and MORE! Resources for IT Professionals

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

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.