1 / 28

Module 7 Reading SQL Server ® 2008 R2 Execution Plans

Module 7 Reading SQL Server ® 2008 R2 Execution Plans. Module Overview. Execution Plan Core Concepts Common Execution Plan Elements Working with Execution Plans. Lesson 1: Execution Plan Core Concepts. Why Execution Plans Matter Query Execution Phases What is an Execution Plan?

lois
Download Presentation

Module 7 Reading SQL Server ® 2008 R2 Execution Plans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Module 7 Reading SQL Server® 2008 R2 Execution Plans

  2. Module Overview • Execution Plan Core Concepts • Common Execution Plan Elements • Working with Execution Plans

  3. Lesson 1: Execution Plan Core Concepts • Why Execution Plans Matter • Query Execution Phases • What is an Execution Plan? • Actual vs. Estimated Execution Plans • What is an Execution Context? • Execution Plan Formats • Demonstration 1A: Viewing Execution Plans in SSMS

  4. Why Execution Plans Matter • Common questions • Why does my query take so long to execute? • Why does this query take so much longer than a very similar query? • Why is SQL Server ignoring this perfectly fine index that I created? • Execution plans can help • Best use is in verifying expected plans

  5. Query Execution Phases • SQL Server parses the T-SQL • Object references are resolved • Query optimizer finds a sufficiently good plan • Not necessary for DDL statements • Some statements lead to trivial plans • Aim is to quickly find a plan that is good enough • Storage and Execution engines execute the plan • Plan may be stored in the Plan Cache

  6. What is an Execution Plan? • Execution plans detail choices that SQL Server makes on: • Types of operations • Order of operations • Choice of indexes • Rowcount estimates based on available statistics • SQL Server performs cost-based optimization • A cost is assigned to each element An execution plan shows how a query was executed or how it would be executed.

  7. Actual vs. Estimated Execution Plans • An option exists to ask SQL Server how it would execute a query • Known as an estimated execution plan • SQL Server does not execute the query, it just works out how it would execute it • Not always possible to get an estimated plan • An actual plan may differ from an estimated plan • Out of date statistics • Missing statistics • Actual degree of parallelism • Estimated plan returns estimated counts based on statistics • Actual plan returns actual counts returned

  8. What is an Execution Context? • Multiple users may execute a plan concurrently • Execution plan details how the query would be executed • Does not hold data for a specific execution • Execution context holds the data associated with a specific execution of the plan • One execution context is needed for each concurrent execution • Execution contexts are also cached • Not all execution contexts are identical

  9. Execution Plan Formats • Text-based plans • SET SHOWPLAN_TEXT ON • SET SHOWPLAN_ALL ON • Now deprecated • XML-based plans • SSMS saves these as .sqlplan files • Portable format for execution plans • SSMS is associated with this filetype • Graphical plans • Renders XML plans in an easier to read format • Does not contain all information from the XML plan

  10. Demonstration 1A: Viewing Execution Plans in SSMS • In this demonstration, you will see how to: • Show an estimated execution plan • Compare execution costs between two queries in a batch • Show an actual execution plan • Save an execution plan

  11. Lesson 2: Common Execution Plan Elements • Table and Clustered Index Scans and Seeks • Nested Loops and Lookups • Merge and Hash Joins • Aggregations • Filter and Sort • Data Modification • Demonstration 2A: Common Execution Plan Elements

  12. Table and Clustered Index Scans and Seeks • Table scan • Reading from a heap • Clustered index scan • Reading from a table with a clustered index • Clustered index seek • Following a clustered index to a specific location within the table

  13. Nested Loops and Lookups • Nested Loops • Commonly used for inner join operations • Can be used for left outer join, left semi join and left anti semi join operations • For each row of top data path, perform a lookup to the bottom data path • RID Lookup • Lookup on a heap using a row ID • Key Lookup • Lookup to a clustered index

  14. Merge and Hash Joins • Merge Join • Commonly used for inner joins • Can be used for left outer join, left semi join, left anti semi join, right outer join, right semi join, right anti semi join and union operations • Requires two inputs to be in the same sorted order • Hash Match • More difficult joins where a hash table is built by computing a hash value for each row from one input • Other input is used to lookup into the hash table

  15. Aggregations • Stream Aggregate • Highly efficient • Data already in correct order for processing the aggregate • Hash Match Aggregate • Hash table used to form aggregate as data not in the necessary order

  16. Filter and Sort • Filter • Low cost operation • Typically used for WHERE clause predicates or HAVING clause predicates • Only pass through rows that match the required filter criteria • Sort • Used whenever a sort operation is necessary • Often used for ORDER BY clauses • Can be used for other operations such as sorting inputs for merge join operations or performing DISTINCT operations • Can be very expensive

  17. Data Modification • INSERT • Used in INSERT operations • UPDATE • Used in UPDATE operations • DELETE • Used in DELETE operations • T-SQL MERGE statement can use combinations of inserts, updates and deletes

  18. Demonstration 2A: Common Execution Plan Elements • In this demonstration, you will see queries that demonstrate the most common execution plan elements

  19. Lesson 3: Working with Execution Plans • Methods for Capturing Plans • Demonstration 3A: Capturing Plans in Activity Monitor • Re-executing Queries • Execution Plan Related DMVs • Demonstration 3B: Viewing Cached Plans

  20. Methods for Capturing Plans • SQL Server Management Studio • Estimated and actual • Also available in Visual Studio 2010 • SQL Server Profiler • Can capture query execution details • Options for all query plan types • Can include an XML column with query plan • Dynamic Management Views (DMVs) • Activity Monitor • SQL Server Data Collection

  21. Demonstration 3A: Capturing Plans in Activity Monitor • In this demonstration, you will see how to use Activity Monitor to view recent expensive queries

  22. Re-Executing Queries • Plan re-use is generally desirable • Parameter sniffing issues are an exception to this • Plans can become unusable or suboptimal • Correctness • Optimality • Plans evicted on a cost algorithm basis • Cost reduced over time • Reuse re-establishes cost • Cost of zero indicates a candidate for eviction • Options are available to force compilation behavior

  23. Execution Plan Related DMVs • Dynamic Management Views (and Dynamic Management Functions) • Show current server state • Not persisted internally • Many useful views/functions related to execution: • sys.dm_exec_connections • sys.dm_exec_sessions • sys.dm_exec_query_stats • sys.dm_exec_requests • sys.dm_exec_sql_text() • sys.dm_exec_query_plan() • sys.dm_exec_cached_plans • sys.dm_exec_cached_plan_dependent_objects()

  24. Demonstration 3B: Viewing Cached Plans • In this demonstration you will see how to view cached execution plans

  25. Lab 7: Reading SQL Server Execution Plans • Exercise 1: Actual vs. Estimated Plans • Exercise 2: Identify Common Plan Elements • Challenge Exercise 3: Query Cost Comparison (Only if time permits) Logon information Estimated time: 45minutes

  26. Lab Scenario You have been learning about the design of indexes. To take this learning further, you need to have a way to view how these indexes are used. In the first exercise, you will learn to view both estimated and actual execution plans. Execution plans can contain many types of elements. In the second exercise, you will learn to identify the most common plan elements and see how statements lead to these elements being used. You regularly find yourself trying to decide between different ways of structuring SQL queries. You are concerned that you aren’t always choosing the highest-performing options. If time permits, you will learn to use execution plans to compare the cost of statements in multi-statement batches.

  27. Lab Review • Can two different queries end up with the same execution plan? • Question: If so, how can that occur? If not, why not?

  28. Module Review and Takeaways • Review Questions • Best Practices

More Related