Module 7 Reading SQL Server® 2008 R2 Execution Plans.

Slides:



Advertisements
Similar presentations
Tuning Oracle SQL The Basics of Efficient SQLThe Basics of Efficient SQL Common Sense Indexing The Optimizer –Making SQL Efficient Finding Problem Queries.
Advertisements

Understanding SQL Server Query Execution Plans
SQL Server performance tuning basics
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Algorithms for SELECT and JOIN Operations (8) Implementing the JOIN Operation: Join (EQUIJOIN, NATURAL.
EXECUTION PLANS By Nimesh Shah, Amit Bhawnani. Outline  What is execution plan  How are execution plans created  How to get an execution plan  Graphical.
Module 17 Tracing Access to SQL Server 2008 R2. Module Overview Capturing Activity using SQL Server Profiler Improving Performance with the Database Engine.
Module 6 Implementing Table Structures in SQL Server ®2008 R2.
Paper by: A. Balmin, T. Eliaz, J. Hornibrook, L. Lim, G. M. Lohman, D. Simmen, M. Wang, C. Zhang Slides and Presentation By: Justin Weaver.
Module 13: Optimizing Query Performance. Overview Introduction to the Query Optimizer Obtaining Execution Plan Information Using an Index to Cover a Query.
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
Module 9 Designing an XML Strategy. Module 9: Designing an XML Strategy Designing XML Storage Designing a Data Conversion Strategy Designing an XML Query.
Module 17 Storing XML Data in SQL Server® 2008 R2.
Module 18 Monitoring SQL Server 2008 R2. Module Overview Monitoring Activity Capturing and Managing Performance Data Analyzing Collected Performance Data.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
Module 8 Improving Performance through Nonclustered Indexes.
Module 19 Managing Multiple Servers. Module Overview Working with Multiple Servers Virtualizing SQL Server Deploying and Upgrading Data-Tier Applications.
Module 12: Optimizing Query Performance. Overview Introducing the Query Optimizer Tuning Performance Using SQL Utilities Using an Index to Cover a Query.
Denny Cherry Manager of Information Systems MVP, MCSA, MCDBA, MCTS, MCITP.
Database Management 9. course. Execution of queries.
Module 9 Designing and Implementing Stored Procedures.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
Module 5 Planning for SQL Server® 2008 R2 Indexing.
Primary Key, Cluster Key & Identity Loop, Hash & Merge Joins Joe Chang
Module 14 Configuring Security for SQL Server Agent.
Parallel Execution Plans Joe Chang
Parallel Execution Plans Joe Chang
Module 4 Designing and Implementing Views. Module Overview Introduction to Views Creating and Managing Views Performance Considerations for Views.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Module 4 Database SQL Tuning Section 3 Application Performance.
Query Optimizer Execution Plan Cost Model Joe Chang
Indexes and Views Unit 7.
© IBM Corporation 2005 Informix User Forum 2005 John F. Miller III Explaining SQLEXPLAIN ®
(SQL - Structured Query Language)
Query Execution. Where are we? File organizations: sorted, hashed, heaps. Indexes: hash index, B+-tree Indexes can be clustered or not. Data can be stored.
Thinking in Sets and SQL Query Logical Processing.
Module 10 Merging Data and Passing Tables. Module Overview Using the MERGE Statement Implementing Table Types Using Table Types As Parameters.
Module 9: Using Advanced Techniques. Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query.
Eugene Meidinger Execution Plans
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Execution Plans Detail From Zero to Hero İsmail Adar.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
SQL Server Statistics and its relationship with Query Optimizer
Database Management System
Query Tuning without Production Data
UFC #1433 In-Memory tables 2014 vs 2016
Query Tuning without Production Data
Database Performance Tuning and Query Optimization
Introduction to Execution Plans
Chapter 15 QUERY EXECUTION.
The Key to the Database Engine
Query Optimization Techniques
SQL Fundamentals in Three Hours
Introduction to reading execution plans
Introduction to Execution Plans
Chapter 8 Advanced SQL.
Chapter 11 Database Performance Tuning and Query Optimization
CS222P: Principles of Data Management Notes #13 Set operations, Aggregation, Query Plans Instructor: Chen Li.
EXECUTION PLANS Quick Dive.
Execution plans Eugene
Diving into Query Execution Plans
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Execution Plans
Query Optimization Techniques
Introduction to Execution Plans
All about Indexes Gail Shaw.
Presentation transcript:

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? 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 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

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

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.

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