Execution Plans Demystified

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
Overview of Query Evaluation (contd.) Chapter 12 Ramakrishnan and Gehrke (Sections )
Query Optimization Reserves Sailors sid=sid bid=100 rating > 5 sname (Simple Nested Loops) Imperative query execution plan: SELECT S.sname FROM Reserves.
EXECUTION PLANS By Nimesh Shah, Amit Bhawnani. Outline  What is execution plan  How are execution plans created  How to get an execution plan  Graphical.
Query Optimization Goal: Declarative SQL query
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
The query processor does what the query plan tells it to do A “good” query plan is essential for a well- performing.
Query Optimization, part 2 CS634 Lecture 13, Mar Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Query Evaluation Chapter 12: Overview.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Database Management 9. course. Execution of queries.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
Around the world (of query plan operators) in 50 minutes David Morrison BI Consultant.
Indexes and Views Unit 7.
Introduction to Query Optimization, R. Ramakrishnan and J. Gehrke 1 Introduction to Query Optimization Chapter 13.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Introduction to Query Optimization Chapter 13.
Sorting and Joining.
Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP,MCP. SQL SERVER Database Administration.
APRIL 13 th Introduction About me Duško Mirković 7 years of experience.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Execution Plans Detail From Zero to Hero İsmail Adar.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP SQL SERVER Database Administration.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
How is data stored? ● Table and index Data are stored in blocks(aka Page). ● All IO is done at least one block at a time. ● Typical block size is 8Kb.
SQL Server Statistics and its relationship with Query Optimizer
Chris Index Feng Shui Chris
Record Storage, File Organization, and Indexes
Database Management System
Prepared by : Ankit Patel (226)
Parameter Sniffing in SQL Server Stored Procedures
Query Tuning without Production Data
Reading execution plans successfully
CS222P: Principles of Data Management Lecture #15 Query Optimization (System-R) Instructor: Chen Li.
Reading Execution Plans Successfully
Introduction to Query Optimization
Overview of Query Optimization
Introduction to Execution Plans
Chapter 15 QUERY EXECUTION.
Database Management Systems (CS 564)
Examples of Physical Query Plan Alternatives
The Key to the Database Engine
Now where does THAT estimate come from?
Cardinality Estimator 2014/2016
Session #, Speaker Name Indexing Chapter 8 11/19/2018.
Statistics What are the chances
Lecture 12 Lecture 12: Indexing.
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Cse 344 APRIL 23RD – Indexing.
Transactions, Locking and Query Optimisation
SQL Server 2016 Execution Plan Analysis Liviu Ieran
Reading Execution Plans Successfully
SQL Server Query Plans Journeyman and Beyond
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
Execution plans Eugene
Diving into Query Execution Plans
Introduction to Execution Plans
Introduction to the Optimizer
Query Optimization Techniques
CS222: Principles of Data Management Lecture #15 Query Optimization (System-R) Instructor: Chen Li.
SQL Server Execution Plan Primer
Reading execution plans successfully
T-SQL Basics: Coding for performance
Introduction to Execution Plans
Welcome!.
All about Indexes Gail Shaw.
Presentation transcript:

Execution Plans Demystified Ernest Libertucci Execution Plans Demystified

Presenter Info Senior Database Engineer 17+ Years Using SQL Server Database administration, SSIS, Hadoop, etc Thank you to our sponsers!

Execution Plans – What are they? An execution plan (or query plan) is a description of the operations the SQL Server engine will use to perform a given task, along with the metrics involved in this decision making process.

Why should we care? Can answer questions including: Why is my query so slow? Is that index I created being used? Why is TempDB growing? Where was I last night? (Okay, maybe not that one)

Goal – Avoid becoming this:

Permissions Required SHOWPLAN permission is required (database-level) for all databases involved in the query.

How do we see them? SSMS Menu. CTRL + L (Show Estimated) CTRL + M (Include Actual) SET SHOWPLAN_XML ON

Indexes

Types of Indexes Clustered – Logical ordering of data (Page splits can change physical order). Data pages are the leaf nodes, so the table becomes this index. Tables without a clustered index are called a heap. Non-clustered – One or more columns. Nodes contain pointer to the row data stored in the clustered index (or heap). Columnstore. Filtered.

Statistics

Statistics Details Cardinality: Uniqueness of Data values (Example: Countries would have 193 distinct values). Cardinality Estimates refer to the engines ‘belief’ about a column’s distribution. Contributes greatly to the choices of operators used in an execution plan.

A day in the life of a Query – Part 1

A day in the life of a Query – Part 2

A day in the life of a Query – Part 3

Query Optimizer Software that analyzes potential execution plans for a given query and chooses which one to execute. Cost-based, but will choose the best plan it can within a quantum. Parses the query, creating a logical tree of operations. Using this tree, the QO examines some potential ways to satisfy a query and then picks the lowest cost one.

Operators Table/Clustered Index Scan Index Seek Sort Key/Bookmark Lookup Aggregates Scalar Data Modification

Join Operators – Nested Loop In general, better for smaller data inputs. Cost A * B

Join Operators – Merge Join Join two sorted inputs (Sort operator or Index) Ideal for large range scans where both sides are indexed. Cost A + B

Join Operators – Hash Match Build a hash table for both inputs, then look for matching. Typically chosen when at least 1 input is large. Image: By Jorge Stolfi - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=6471238

Suspicious Things Seek (go to value) vs Scan (read entire table/index) SARgable predicates (Search ARGumentable) { f(col1) = value vs col1 = f(value) } Implicit conversion { ex: column1 = ‘5’ when column1 is integer } Statistics {Estimated widely different from Actual }

Considerations Give the Query Optimizer Information Constraints, Keys (Primary/Foreign), Nullability Avoid Implicit Conversion Database Compatibility Level Querytraceon hint (9481 or 2312) 9481: Use 2012 QO on a later version of sql server 2312: Use 2014 QO on a 2012 or lower set database (SQL 2014+) https://support.microsoft.com/en-us/help/2801413/enable-plan-affecting-sql-server-query-optimizer-behavior-that-can-be

Query Store (2016+) Plan, Runtime stats, wait stores.

Automatic Plan Correction (SQL 2017) Apply previous plan instead of a regressed one. Note: “current” keyword syntax since 2012.

Other Things of Note ™ Live statistics Query Optimizer (2014) QO Fixes

References/Further Information SQL Server Execution Plans (3rd Ed) – Grant Fritchey Execution Plan Reference https://sqlserverfast.com/epr Sp_WhoIsActive – Adam Machanic SentryOne Plan Explorer

Thank you!