SQL Server Query Plans Journeyman and Beyond

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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Query Optimization Chapters 14.
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.
Session 1 Module 1: Introduction to Data Integrity
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.
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.
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.
SQL Server Statistics and its relationship with Query Optimizer
Chris Index Feng Shui Chris
Database Management System
Indexing ? Why ? Need to locate the actual records on disk without having to read the entire table into memory.
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
COST ESTIMATION FOR THE RELATIONAL ALGEBRA OPERATIONS MIT 813 GROUP 15 PRESENTATION.
Introduction to Execution Plans
Chapter 15 QUERY EXECUTION.
Examples of Physical Query Plan Alternatives
The Key to the Database Engine
Cardinality Estimator 2014/2016
Physical Join Operators
Session #, Speaker Name Indexing Chapter 8 11/19/2018.
Lecture 12 Lecture 12: Indexing.
Physical Database Design
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Execution Plans Demystified
Transactions, Locking and Query Optimisation
SQL Server 2016 Execution Plan Analysis Liviu Ieran
Reading Execution Plans Successfully
Database Applications (15-415) DBMS Internals- Part IX Lecture 21, April 1, 2018 Mohammad Hammoud.
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
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:

SQL Server Query Plans Journeyman and Beyond Ernest Libertucci SQL Server Query Plans Journeyman and Beyond

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 permissions 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. 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).

Statistics

Statistics Details Cardinality: Uniquenes of Data values (Example: Countries would have 193 distinct values). Cardinality Estimates refer to the engines ‘belief’ about a column’s distribution.

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) NOT NULL Avoid Impicit Conversion Stay alert! Trust no-one! (Okay, 50% of this)

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 (2nd Ed) – Grant Fritchey Execution Plan Reference https://sqlserverfast.com/epr Sp_WhoIsActive – Adam Machanic SentryOne Plan Explorer

Thank you!