Post on 23-Feb-2016
description
transcript
Module 7Reading 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?• Actual vs. Estimated Execution Plans• What is an Execution Context?• Execution Plan Formats• Demonstration 1A: Viewing Execution Plans in SSMS
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
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
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.
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
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
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
Demonstration 1A: Viewing Execution Plans in SSMSIn 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
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
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
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
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
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
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
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
Demonstration 2A: Common Execution Plan Elements• In this demonstration, you will see queries that
demonstrate the most common execution plan elements
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
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
Demonstration 3A: Capturing Plans in Activity Monitor• In this demonstration, you will see how to use Activity
Monitor to view recent expensive queries
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
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()
Demonstration 3B: Viewing Cached Plans• In this demonstration you will see how to view cached
execution plans
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: 45 minutes
Virtual machine 623XB-MIA-SQLUser name AdventureWorks\AdministratorPassword Pa$$w0rd
Lab ScenarioYou 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.
Lab Review• Can two different queries end up with the same execution
plan?• Question: If so, how can that occur? If not, why not?
Module Review and Takeaways• Review Questions• Best Practices