before you optimize: understanding execution plans
DESCRIPTION
You know what your query does, but do you know how it does it? Do you know what type of resources your query uses? This session covered these questions and more as we walked through reading execution plans. We saw how SQL breaks down the execution of your query and what each step tells us about the overall query. These slides provide the additional resources that go into the depth we couldn't get into in the session.TRANSCRIPT
Before You Optimize: Understanding Execution Plans
@IAmTimCorey
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
Agenda
• Introduction to Execution Plans• Reading Execution Plans• Limitations• Additional Tools• Next Steps
What Are Execution Plans?
Basic Execution Plan
Types of Execution Plans
Estimated ActualGraphical Ctrl + L Ctrl + MText SHOWPLAN_ALL
SHOWPLAN_TEXTSTATISTICS PROFILE
XML SHOWPLAN_XML STATISTICS_XML
Estimated vs. Actual Details
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
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
How Do You Read an Execution Plan?
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
Limitations
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
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
Additional Tools
SQL Server Profiler
How Do I Learn More?
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
For questions, comments, and further information, catch me on Twitter at
@IAmTimCorey or email [email protected]