Diving into Query Execution Plans

Slides:



Advertisements
Similar presentations
Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
Advertisements

Understanding SQL Server Query Execution Plans
Module 13: Performance Tuning. Overview Performance tuning methodologies Instance level Database level Application level Overview of tools and techniques.
EXECUTION PLANS By Nimesh Shah, Amit Bhawnani. Outline  What is execution plan  How are execution plans created  How to get an execution plan  Graphical.
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 11 Database Performance Tuning and Query Optimization.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
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.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Sorting and Joining.
Eugene Meidinger Execution Plans
SQL Server Deep Dive Denis Reznik Data Architect at Intapp.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Execution Plans Detail From Zero to Hero İsmail Adar.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Database Design: Solving Problems Before they Start! Ed Pollack Database Administrator CommerceHub.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
CHAPTER 19 Query Optimization. CHAPTER 19 Query Optimization.
SQL Server Statistics and its relationship with Query Optimizer
Chris Index Feng Shui Chris
An Introductory Look at Execution Plans
Query Optimization Techniques
Module 11: File Structure
Dynamic SQL Writing Efficient Queries on the Fly
MySQL Subquery Source: Dev.MySql.com
Informatica PowerCenter Performance Tuning Tips
Dynamic SQL: Writing Efficient Queries on the Fly
Efficiently Searching Schema in SQL Server
Database Performance Tuning &
Query Tuning without Production Data
Database Performance Tuning and Query Optimization
Hustle and Bustle of SQL Pages
Introduction to Execution Plans
Chapter 15 QUERY EXECUTION.
The Key to the Database Engine
Physical Join Operators
Session #, Speaker Name Indexing Chapter 8 11/19/2018.
Statistics What are the chances
Query Optimization Techniques
Dave LinkedIn SQL Server Execution Plans: How to use them to find performance bottlenecks Dave LinkedIn.
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Cse 344 APRIL 23RD – Indexing.
Execution Plans Demystified
Dynamic SQL: Writing Efficient Queries on the Fly
SQL Server Query Plans Journeyman and Beyond
Introduction to reading execution plans
Dave Bland LinkedIn SQL Server Execution Plans: How to use them to find performance bottlenecks Dave Bland LinkedIn
Query Processing CSD305 Advanced Databases.
Introduction to Execution Plans
Lecture 13: Query Execution
Chapter 11 Database Performance Tuning and Query Optimization
Insight into the SQL Server Buffer Cache
EXECUTION PLANS Quick Dive.
Execution plans Eugene
Tracking Index Usage Like a Pro
A – Pre Join Indexes.
Introduction to Execution Plans
Query Optimization Techniques
Database SQL.
SQL Server Execution Plan Primer
Reading execution plans successfully
Introduction to Execution Plans
All about Indexes Gail Shaw.
Presentation transcript:

Diving into Query Execution Plans Ed Pollack CommerceHub Database Administrator

How is a Query Processed? PARSING: The SQL statement is broken down into logical pieces: keywords, objects, operators, etc…and is checked for syntax errors. BINDING: Object names are checked against system views. A query tree is built with this data, creating an ordered list of steps that are needed to get from input to output. AKA: algebrizer. OPTIMIZATION: The query optimizer analyzes different ways to process the query and chooses the best one found. The result is an execution plan. EXECUTION: The query is executed by the relational engine based on the results of the optimizer.

What are Query Execution Plans? Graphical step-by-step depiction of how a query is executed (right-to-left). Lots of useful detail! Combine with IO & time statistics to get a thorough look at the resources consumed by a query. Steps are illustrated with subtree cost (what’s this!?)

What Does the Query Optimizer Do? Creates a list of many possible execution plans. Analyzes resources required and estimates cost. Follows a set structure of search paths. Uses transformation rules to generate plans. Statistics are key to this process. Chooses the best plan found in the time allotted. May not be the best plan possible! SQL Example: Turning on & using execution plans.

Table Access Table Scan Clustered Index Scan This is a complete scan through a heap. Tables should always have a clustered index - if you see this, beware! Clustered Index Scan This is a complete scan of a table with a clustered index. While inefficient, this can be good for a return of a large % of the table, or in the case of a very small table. Clustered Index Seek/Non Clustered Index Seek Data is read by using an index on the target table. Indexes are stored as a b-tree, so the effort of a seek vs. a scan is significantly less. Key Lookup /RID Lookup When columns are selected that are not included in the index, a lookup to the table is performed to get that data. SQL Example: Table Access

Joins Hash Match Nested Loops Merge Join SQL Example: Types of joins Often used where a small table is joined to a larger table. A hash table is created using the smaller table, and the larger table is matched via the hash table row-by-row. This temporary hash table is stored in TempDB. Works well on unsorted/unindexed inputs. Nested Loops One set of data is compared to the other, row-by-row, until all rows have been matched. Efficient for small data sets or when one input is indexed and the other isn’t (and isn’t too large). Merge Join Only very works well on sorted data. Since both sets are sorted, they can be matched directly with each other quickly. This can be inefficient if somehow used on unsorted data. SQL Example: Types of joins

Other Basic Elements (part 1) Compute Scalar Performs a basic scalar operation on data, such as DATEPART, +, ROUND, or LEFT Sort Sorts input, either by an ORDER BY, or to complete a join Filter Scans input and checks a condition, such as via a HAVING or WHERE clause. Concatenation Used to combine 2 data sets, usually via UNION ALL. Top Returns the Top N rows from a data set. Constant Scan Introduces a fixed number of scalar rows into the query so that future operations can be completed. Can also replace trivial query sections.

Other Basic Elements (part 2) Stream Aggregate Used when we GROUP or PARTITION data to group by a specific column or columns. Spools: Eager & Lazy Temporary data storage in TempDB for rows that may be needed again or to separate sensitive data from the underlying data storage. Eager: spools all data at the start; Lazy: spools only rows of data as needed. Insert, Update, Delete, Select Self-explanatory : )

Execution Plan & IO Display Options SHOWPLAN_ALL SHOWPLAN_TEXT STATISTICS PROFILE STATISTICS IO STATISTICS TIME SHOWPLAN_XML (in trace) STATISTICS XML (in trace) SQL Example: Display Options

Additional Optimizer Notes Plan Cache Execution plans are saved in memory for reuse in the future. Allow frequently executed queries to be executed without the need for optimization each time. Plans are removed due to age, memory, lack of use, or changes in statistics or table structure. Query hash & plan reuse Execution plans can change at runtime (Recompile). Actual vs. estimated execution plans. SQL Example: Insight into query optimization.

Hints Query hints can be used in writing queries to tell the optimizer to modify joins, join orders, locking mechanisms, query plans, default data, etc… Examples include: WITH (NOLOCK) OPTIMIZE FOR @color = ‘blue’ OPTION (MAXDOP 1) OPTION (FAST 10) OPTION (HASH JOIN) Hints should be used rarely as they constrain the query optimizer. Frequent usage of hints can be indicative of a much more severe problem Very bad hints can render a query unexecutable!

Questions ?????

Conclusion & Thank You! For additional reading, please see: SQL Server Execution Plans: Grant Fritchey: http://download.red-gate.com/ebooks/SQL/sql-server- execution-plans.pdf Inside the SQL Server Query Optimizer: Benjamin Nevarez: https://www.simple-talk.com/books/sql-books/inside-the- sql-server-query-optimizer/ SQL Server Central: http://www.sqlservercentral.com/Contributions/Home SQL Shack: http://www.sqlshack.com Come to SQL Saturday Albany on July 30, 2016: http://www.sqlsaturday.com/513 Contact info: ed7@alum.rpi.edu @EdwardPollack