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
![Page 1: Before you optimize: Understanding Execution Plans](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/1.jpg)
Before You Optimize: Understanding Execution Plans
@IAmTimCorey
![Page 2: Before you optimize: Understanding Execution Plans](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/3.jpg)
Agenda
• Introduction to Execution Plans• Reading Execution Plans• Limitations• Additional Tools• Next Steps
![Page 4: Before you optimize: Understanding Execution Plans](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/4.jpg)
What Are Execution Plans?
![Page 5: Before you optimize: Understanding Execution Plans](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/5.jpg)
Basic Execution Plan
![Page 6: Before you optimize: Understanding Execution Plans](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/7.jpg)
Estimated vs. Actual Details
![Page 8: Before you optimize: Understanding Execution Plans](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/10.jpg)
How Do You Read an Execution Plan?
![Page 11: Before you optimize: Understanding Execution Plans](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/12.jpg)
Limitations
![Page 13: Before you optimize: Understanding Execution Plans](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/15.jpg)
Additional Tools
![Page 18: Before you optimize: Understanding Execution Plans](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/18.jpg)
SQL Server Profiler
![Page 19: Before you optimize: Understanding Execution Plans](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/19.jpg)
How Do I Learn More?
![Page 20: Before you optimize: Understanding Execution Plans](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061210/548d20bfb4795949118b4714/html5/thumbnails/21.jpg)
For questions, comments, and further information, catch me on Twitter at
@IAmTimCorey or email [email protected]