2 avoiding stored procedure recompiles dr greg low managing director solid q australia session code:...

49

Upload: sherman-moody

Post on 27-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

2

Avoiding Stored Procedure Recompiles

Dr Greg Low Managing DirectorSolid Q Australia

Session Code: DAT454

3

Who am I?

Managing Director Solid Q AustraliaHost of SQL Down Under PodcastMicrosoft Regional DirectorMicrosoft MVP for SQL ServerOrganizer for SDU Code CampCo-organizer for CodeCampOzPASS 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 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

Query compilation and plan caching in SQL ServerUsing tools to view caching and recompilesRecompilation triggers in SQL Server 2000 and 2005/8Best 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

FasterLess memoryFewer compile locksAvoids the need to break up large stored procedures

SQL Server 2005/2008

9

demoPop – 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_executesqlStored ProceduresBatches (exact match)EXEC () (checks resultant string)

Cached vs Non-Cached

11

demoShowplan – 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 contextSQL Server 2000 – measure of server resources to optimize the batch (note not from executions). Based on:

CPU time generating the planRead page countWrite page countMemory pages occupied

Lazy writer divides costs by 4 each pass0 entries (+ ad-hoc queries) are deletion targetsCost gets reset to original value on reuse

Caching In SQL Server 2000

16

Ad-hoc still zeroCost 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 queriesShould help for applications with large numbers of non-parameterized batches

18

Query compilation and plan caching in SQL ServerUsing tools to view caching and recompilesRecompilation triggers in SQL Server 2000 and 2005/2008Best practices

What we will cover

19

demosys.syscacheobjects / Pop Quiz Revisited

20

demoSQL Profiler

21

Query compilation and plan caching in SQL ServerUsing tools to view caching and recompilesRecompilation triggers in SQL Server 2000 and 2005/2008Best practices

What we will cover

22

demoRecompilation Reasons

23

CorrectnessOptimality

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

demoName Resolution Issues

27

EXEC dbo.Proc1

EXEC anotherdb.dbo.Proc1

Database Context (Correctness)

28

Batch inside vs outside CLRCREATE PROCEDURE … WITH RECOMPILEEXEC … WITH RECOMPILE (but preferred to the CREATE PROC version)Mixing DDL and DMLsp_recompileDBCC FREEPROCCACHE/DBCC FLUSHPROCINDBRECONFIGUREAutoclose

Miscellaneous

29

demoMixing DDL and DML

30

Add/drop columnsAdd/drop indexes (where used in the plan)Manual statistics changesATTACH/DETACHONLINE/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 tablerowmodctr (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 transactionalrowmodctr 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

demoWorking with Statistics

35

Problem with atypical parameters

SQL Server 2005/2008 hint for default parameter values

Parameter Sniffing

36

demoParameter Sniffing and 2005/2008

37

Query compilation and plan caching in SQL ServerUsing tools to view caching and recompilesRecompilation triggers in SQL Server 2000 and 2005/2008Best 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 EXECConsider using new SQL Server 2005 hints to minimize the need for theseOr execute the query using sp_executesqlConsider 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_PLANAvoid manually controlling statistics updates. Avoid mixing DDL and DMLConsider 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) 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

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

Q & A

44

Thank You!

[email protected]://sqlblog.com/blogs/greg_lowhttp://www.sqldownunder.com

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

claireh
Place Holder

www.microsoft.com/teched Tech·Talks Tech·Ed BloggersLive Simulcasts Virtual Labs

http://microsoft.com/technet

Evaluation licenses, pre-released products, and MORE!

Resources for IT Professionals

Complete anevaluation onCommNet andenter 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.