Download - Before you optimize: Understanding Execution Plans

Transcript
Page 1: Before you optimize: Understanding Execution Plans

Before You Optimize: Understanding Execution Plans

@IAmTimCorey

Page 2: Before you optimize: Understanding Execution Plans

About Me

• Worked with Microsoft SQL since 6.5• Started my career in VB6• Moved to .NET when it came out• Consultant, Adjunct Professor, Trainer, and

Speaker

Page 3: Before you optimize: Understanding Execution Plans

Agenda

• Introduction to Execution Plans• Reading Execution Plans• Limitations• Additional Tools• Next Steps

Page 4: Before you optimize: Understanding Execution Plans

What Are Execution Plans?

Page 5: Before you optimize: Understanding Execution Plans

Basic Execution Plan

Page 6: Before you optimize: Understanding Execution Plans

Types of Execution Plans

Estimated ActualGraphical Ctrl + L Ctrl + MText SHOWPLAN_ALL

SHOWPLAN_TEXTSTATISTICS PROFILE

XML SHOWPLAN_XML STATISTICS_XML

Page 7: Before you optimize: Understanding Execution Plans

Estimated vs. Actual Details

Page 8: Before you optimize: Understanding Execution Plans

List of Existing Plans

SELECT [cp].[refcounts] ,[cp].[usecounts] ,[cp].[objtype] ,[st].[dbid] ,[st].[objectid] ,[st].[text] ,[qp].[query_plan]FROM sys.dm_exec_cached_plans cpCROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) stCROSS APPLY sys.dm_exec_query_plan(cp.plan_handle)qp;

* From SQL Server Execution Plans by Grant Fritchey – p. 23

Page 9: Before you optimize: Understanding Execution Plans

Execution Plan Aging

Formula: Cost to compile * # of callsLazywriter: scans cache and decrements by one each scanClear Cache: DBCC FREEPROCCACHE

Items are removed when:• Memory is needed• The age of the item is zero• The plan is not currently in use

Page 10: Before you optimize: Understanding Execution Plans

How Do You Read an Execution Plan?

Page 11: Before you optimize: Understanding Execution Plans

Rebind and Rewind

Occur on certain loop joins. They represent each time the system has to look through the inner table again for more data.

Rebind – when the outer data changesRewind – when the outer data remains the same

More info: http://bit.ly/P7EUv3

Page 12: Before you optimize: Understanding Execution Plans

Limitations

Page 13: Before you optimize: Understanding Execution Plans

Ad Hoc Query Plans

Ad hoc plans will use the cache as long as:• The schema is specified throughout• The text doesn’t change (at all*)

* For queries with one table only, simple parameterization will allow the parameters to change while still reusing the cached plan. To expand this to more complex queries, see this reference: http://bit.ly/Oi3FDH

Page 14: Before you optimize: Understanding Execution Plans

Parameter Sniffing

SQL Server designs execution plans based upon the first set of parameters passed in. This can have adverse effects. To get around it, you can:• Use WITH RECOMPILE• Use tailored stored procs• Set a trace flag to disable parameter sniffing• Use the OPTIMIZE FOR query hint• Use sp_recompile to clear cache for stored proc or those

that use a particular table

Read more here: http://bit.ly/MqNVOG

Page 15: Before you optimize: Understanding Execution Plans

Additional Tools

Page 16: Before you optimize: Understanding Execution Plans

SQL Sentry Plan Explorer

http://bit.ly/NDwBqh

Page 17: Before you optimize: Understanding Execution Plans

AutoEPLoader

http://bit.ly/StcFVu

Page 18: Before you optimize: Understanding Execution Plans

SQL Server Profiler

Page 19: Before you optimize: Understanding Execution Plans

How Do I Learn More?

Page 20: Before you optimize: Understanding Execution Plans

Further Resources

• Book by Grant Fritchey - http://bit.ly/MtL0a2• Index Statistics - http://bit.ly/NTArHo• Query Parallelism - http://bit.ly/NlcS0s• Query Hints - http://bit.ly/N6YMKo

Page 21: Before you optimize: Understanding Execution Plans

For questions, comments, and further information, catch me on Twitter at

@IAmTimCorey or email [email protected]