Execution Plans for Mere Mortals A beginners look at execution plans. Mike Lawell, Teammate, Linchpin People.

Slides:



Advertisements
Similar presentations
new database engine component fully integrated into SQL Server 2014 optimized for OLTP workloads accessing memory resident data achive improvements.
Advertisements

Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
CS 540 Database Management Systems
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.
Tempdb Parasites Jason Hall-Dir. of Client SQL Sentry Blog-jasonhall.blogs.sqlsentry.net.
Access Path Selection in a Relation Database Management System (summarized in section 2)
Database System Architecture and Performance CSCI 6442 ©Copyright 2015, David C. Roberts, all rights reserved.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 14 – Join Processing.
Title Slide No more than two lines here Session Subtitle 18pt April , 2015 | Santa Clara, CA Speaker Name Title Company.
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.
Top Free Tools for Tuning SQL Statements Kevin Kline & Aaron Bertrand SQL Sentry, Inc.
The Self-Managing Database: Guided Application and SQL Tuning Mohamed Ziauddin Consulting Member of Technical Staff Oracle Corporation Session id:
Query Processing. Steps in Query Processing Validate and translate the query –Good syntax. –All referenced relations exist. –Translate the SQL to relational.
Online aggregation Joseph M. Hellerstein University of California, Berkley Peter J. Haas IBM Research Division Helen J. Wang University of California,
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Query Optimizer Execution Plan Cost Model Joe Chang
How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira.
CS 440 Database Management Systems Lecture 5: Query Processing 1.
CS 540 Database Management Systems
Database Systems, 8 th Edition SQL Performance Tuning Evaluated from client perspective –Most current relational DBMSs perform automatic query optimization.
October 15-18, 2013 Charlotte, NC Being the DBA of the Future A World of On-Premises and Cloud Dandy Weyn, Snr. Technical Marketing Product Manager Microsoft.
Fundamentals of Great SQL Query Performance Matt Wigdahl, ScriptPro LLC.
Query Performance Tuning in SQL Server 2014 #devconnections.
Dave LinkedIn
Inside Query Processor Sort Dmitry Pilugin SQL Server Developer, MVP (RU) (EN)
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP,MCP. SQL SERVER Database Administration.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
October 15-18, 2013 Charlotte, NC SQL Server Index Internals Tim Chapman Premier Field Engineer.
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.
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.
A deep dive into SQL Server Plan Cache Management.
This document is provided for informational purposes only and Microsoft makes no warranties, either express or implied, in this document. Information.
Execution Plans for Mere Mortals Contact your Local or Virtual Chapter for their unique discount to SAVE $150! I’M SPEAKING JOIN ME AGAIN OCTOBER 27-30:
Execution Plans for Mere Mortals
CS 540 Database Management Systems
Execution Planning for Success
Execution Plans for Mere Mortals
CS 440 Database Management Systems
Solving the Hard Problems
Reading execution plans successfully
Reading Execution Plans Successfully
Introduction to Query Optimization
Introduction to Execution Plans
The Key to the Database Engine
SQL Server 2017 has more cool features than just running on Linux
Dave LinkedIn SQL Server Execution Plans: How to use them to find performance bottlenecks Dave LinkedIn.
Physical Database Design
Execution Plans Demystified
Statistics: What are they and How do I use them
SQL Server 2016 Execution Plan Analysis Liviu Ieran
Reading Execution Plans Successfully
SQL Server Query Plans Journeyman and Beyond
SQL Server Security For Everyone
Dave Bland LinkedIn SQL Server Execution Plans: How to use them to find performance bottlenecks Dave Bland LinkedIn
Introduction to Execution Plans
Chapter 8 Advanced SQL.
Diving into Query Execution Plans
Introduction to Execution Plans
From adaptive to intelligent: query processing in SQL Server 2019
Reading execution plans successfully
Introduction to Execution Plans
Presentation transcript:

Execution Plans for Mere Mortals A beginners look at execution plans. Mike Lawell, Teammate, Linchpin People

2 Please silence cell phones 2

3 Explore Everything PASS Has to Offer FREE SQL SERVER AND BI WEB EVENTS FREE 1-DAY TRAINING EVENTS REGIONAL EVENT LOCAL USER GROUPS AROUND THE WORLD FREE ONLINE TECHNICAL TRAINING THIS IS COMMUNITYBUSINESS ANALYTICS TRAINING SESSION RECORDINGSPASS NEWSLETTER 3

Agenda We will be taking a look at Graphical Execution Plans from a beginners perspective. The ride will include the following: Execution Steps Execution Plan Basics Basic Operators Join Operators Other Operators 4

Execution Step by Step

6 On The Inside Relational Engine Optimizer Command Parser Query Executor SNI* User Storage Engine Buffer Manager Access Methods Transaction Manager SQL OS Buffer Pool Borrowed from Bradley Ball’s “SQL Internals, Recovery Models, & Backups” presentation Plan Cache Data Cache Data *SQL Server Network Interface (SNI)

Relation Engine 1.Is the query syntactically correct? SELECT [Name], [Number] FORM [Production].[Product] Incorrect syntax near 'Production'. SELCT [Name], [Number] FROM [Production].[Product] Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword 'FROM'. 2.No Errors Output as Parse Tree to Algebrizer Query Parsing 7

8 SELECT [Name], [Number] FORM [Production].[Product] Incorrect syntax near 'Production'.

9 Query Parsing SELECT [Name], [Number] FROM [Production].[Product]

10 Execute Parallel/ Not Parallel Store in Plan Cache (*) Find Plan Hash Checked in Plan Cache Aggregate Binding Resolve Data Types Name Resolution Plan Creation Algebrizer Query Optimizer Recompile? Storage Engine No Results Returned 7 Cost > Cost for Parallelism 8 YES Does Plan Exist? Create Plan NO Yes

11 Execute Parallel/ Not Parallel Store in Plan Cache (*) Find/Create Plan Hash Checked in Plan Cache Aggregate Binding Resolve Data Types Name Resolution Plan Creation Does Plan Exist? Algebrizer Query Optimizer Yes Recompile? No Storage Engine No Results Returned 7 Cost > Cost for Parallelism 8 When a plan is found, good enough or optimal, then it is stored in cache (*). *If optimize for adhoc workloads is enabled, a stub is saved the first execution. If a recompile has been triggered, the process starts over. Is cost > cost for threshold? If it is, parallelize query. The algebrizer finds aggregate operations and performs aggregate binding. The algebrizer resolves the data types for the objects being accessed. The algebrizer resolves the names for objects (tables, columns, etc). Check the query hash against the plan cache to see if it exists If it doesn’t exist, use the query processor tree and statistics to determine execution plan generating multiple plans assigning cost (CPU, I/O) to each step and picks the best one.

12 Cardinality Estimation The optimization process depends on a cost estimation of each physical operator and the estimated number of records (cardinality estimation) to be processed. DBCC SHOW_STATISTICS ("[Sales].[SalesOrderDetailEnlarged]", [_WA_Sys_ _3D7E1B63]); The accuracy of the cardinality estimation depends upon the distribution of values in one or more columns of a table (statistics). NameUpdatedRowsRows SampledStepsAverage key lengthString IndexFilter ExpressionUnfiltered Rows _WA_Sys_ _3D7E1B63Nov :26AM YESNULL1000 RANGE_HI_KEYRANGE_ROWSEQ_ROWSDISTINCT_RANGE_ROWSAVG_RANGE_ROWS 002EE045-E E-8EC B3-B F840-4C C4FEAC-7C13131 Create Plan 5

13 Cardinality Estimation There are multiple factors that can negatively impact the cardinality estimation process: 1. 1.Out of date Statistics SELECT object_name(object_id) as TableName, name AS stats_name, STATS_DATE(object_id, stats_id) AS statistics_update_date FROM sys.stats WHERE object_id = OBJECT_ID('[Sales].[SalesOrderDetailEnlarged]'); 2. 2.Cardinality Estimation Errors

14 Which cardinality version am I using?

Execution Plan Basics Getting Started

16 Required Permissions Server or database roles with permissions: sysadmin, dbcreator or db_owner or showplan To give showplan rights to the database: GRANT SHOWPLAN TO [username];

How to Read

Basic Operators Start Here

Basic Operators DML StatementsAccess Methods 19 Other Operators

Join Operators Making it Happen

21 Nested Loop Non-blocking Description For each row in the top (outer) input, scan the bottom (inner) input, and output matching rows. Where does it happen: Joins with indexes on join columns.

Nested Loop SELECT [sod].[CarrierTrackingNumber], [sod].[OrderQty], [soh].[RevisionNumber], [soh].[OrderDate] FROM [Sales].[SalesOrderHeader] soh JOIN [Sales].[SalesOrderDetail] sod ON sod.[SalesOrderID] = soh.[SalesOrderID] WHERE soh.[OrderDate] = ' :00:00.000' Outer Loop Inner Loop Non-blocking 22

23 Nested Loop

24 Non-blocking Nested Loop SalesOrderIDRevisionNumberOrderDate :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: SalesOrderIDProductIDCarrierTrackingNumberOrderQty E E E E E E E E E E E E E E E E E E E E-951

25 Description Match rows from two suitably sorted input tables exploiting their sort order. Where does it happen: Joins with indexes on join columns sorted in an appropriate sort order. Merge Non-blocking

Merge SELECT [sod].[CarrierTrackingNumber], [sod].[OrderQty], [soh].[RevisionNumber], [soh].[OrderDate] FROM [Sales].[SalesOrderHeader] soh JOIN [Sales].[SalesOrderDetail] sod ON sod.[SalesOrderID] = soh.[SalesOrderID] Non-blocking 26

27 Merge SalesOrderIDRevisionNumberOrderDate :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: :00: SalesOrderIDProductIDCarrierTrackingNumberOrderQty E E E E E E E E E E E E E E E E E E E E-951 Non-blocking

28 Hash Match Description Use each row from the top input to build a hash table, and each row from the bottom input to probe into the hash table, outputting all matching rows. Where does it happen: Missing Index, Missing Where Clause, Where Clause that is non-sargable.

Hash Match Blocking SELECT [sod1].[ProductID], [sod1].[CarrierTrackingNumber], [sod1].[UnitPrice], [sod1].[OrderQty] FROM [Sales].[SalesOrderDetail] sod1 JOIN [Sales].[SalesOrderDetail] sod2 ON [sod1].[ProductID] = [sod2].[ProductID] AND [sod1].[CarrierTrackingNumber] = [sod2].[CarrierTrackingNumber] AND [sod1].[OrderQty] > 5 29

Hash Match Hash Table Blocking 30 Column A Column B Hashed Value 8Ax94Dd343S 8Ax94Dd343S

Hash Match Hash Table 1 Blocking 31 8Ax94Dd343S 1) From the first input (table) the column data is hashed and put in buckets in a “hash” table (in tempdb). 2) Then it reads rows from input 2, hashes the join columns and compares the hash and values to output matches. 8Ax94Dd343S 8Bx40XdF43D 8CxXDVa91f If a Match… “Send” row data to next operator and go to the next row.

32 Fixed Hash Match

Other Operators Fun Stuff

34 Parallelism SELECT [sod1].[ProductID], [sod1].[CarrierTrackingNumber], [sod1].[UnitPrice], [sod1].[OrderQty] FROM [Sales].[SalesOrderDetail] sod1 JOIN [Sales].[SalesOrderDetail] sod2 ON [sod1].[ProductID] = [sod2].[ProductID] AND [sod1].[CarrierTrackingNumber] = [sod2].[CarrierTrackingNumber]

35 Parallelism SET STATISTICS IO ON SET STATISTICS TIME ON SELECT [sod1].[ProductID], [sod1].[CarrierTrackingNumber], [sod1].[UnitPrice], [sod1].[OrderQty] FROM [Sales].[SalesOrderDetail] sod1 JOIN [Sales].[SalesOrderDetail] sod2 ON [sod1].[ProductID] = [sod2].[ProductID] AND [sod1].[CarrierTrackingNumber] = [sod2].[CarrierTrackingNumber] SELECT [sod1].[ProductID], [sod1].[CarrierTrackingNumber], [sod1].[UnitPrice], [sod1].[OrderQty] FROM [Sales].[SalesOrderDetail] sod1 JOIN [Sales].[SalesOrderDetail] sod2 ON [sod1].[ProductID] = [sod2].[ProductID] AND [sod1].[CarrierTrackingNumber] = [sod2].[CarrierTrackingNumber] OPTION(MAXDOP 1)

36 Parallelism

37 Parallelism

38 Hash Aggregate SELECT [sod1].[CarrierTrackingNumber], sum([sod1].[UnitPrice]) UnitPriceTotal, sum([sod1].[OrderQty]) as OrderQtyTotal FROM [Sales].[SalesOrderDetail] sod1 JOIN [Sales].[SalesOrderDetail] sod2 ON [sod1].[ProductID] = [sod2].[ProductID] AND [sod1].[CarrierTrackingNumber] = [sod2].[CarrierTrackingNumber] AND [sod1].[OrderQty] > 5 GROUP BY [sod1].[CarrierTrackingNumber]

39 Stream Aggregate SELECT [sod1].[CarrierTrackingNumber], sum([sod1].[UnitPrice]) UnitPriceTotal, sum([sod1].[OrderQty]) as OrderQtyTotal FROM [Sales].[SalesOrderDetail] sod1 JOIN [Sales].[SalesOrderDetail] sod2 ON [sod1].[ProductID] = [sod2].[ProductID] AND [sod1].[CarrierTrackingNumber] = [sod2].[CarrierTrackingNumber] AND [sod1].[OrderQty] > 5 GROUP BY [sod1].[CarrierTrackingNumber]

40 Sort Spill Operator used tempdb to spill data during execution with spill level 2 SELECT [sod].[CarrierTrackingNumber], [sod].[OrderQty], [soh].[RevisionNumber], [soh].[OrderDate] FROM [Sales].[SalesOrderHeader] soh JOIN [Sales].[SalesOrderDetailEnlarged] sod ON sod.[SalesOrderID] = soh.[SalesOrderID] WHERE [sod].[CarrierTrackingNumber] > '41D0-42A8-A5' ORDER BY [soh].[RevisionNumber], [soh].[OrderDate]

41 Sort Spill SELECT TOP 1 [sod].[CarrierTrackingNumber], [sod].[OrderQty], [soh].[RevisionNumber], [soh].[OrderDate] FROM [Sales].[SalesOrderHeader] soh JOIN [Sales].[SalesOrderDetailEnlarged] sod ON sod.[SalesOrderID] = soh.[SalesOrderID] WHERE [sod].[CarrierTrackingNumber] > '41D0-42A8-A5' ORDER BY [soh].[RevisionNumber], [soh].[OrderDate]

42 Implicit Conversion SELECT [sod].[CarrierTrackingNumber], [sod].[OrderQty], [soh].[RevisionNumber], [soh].[OrderDate] FROM [Sales].[SalesOrderHeaderEnlarged] soh JOIN [Sales].[SalesOrderDetailEnlarged] sod ON sod.[SalesOrderID] = soh.[SalesOrderID] WHERE CONVERT(varchar(10),[soh].[OrderDate],110) = ' '

43 Cool Tools/Products SQL Server Query Plan Analysis by Joe Sack

44 Resources

45 Session Evaluations ways to access Go to passsummit.com/evals Download the GuideBook App and search: PASS Summit 2015 Follow the QR code link displayed on session signage throughout the conference venue and in the program guide Submit by 5pm Friday November 6 th to WIN prizes Your feedback is important and valuable. 45

46 Mike Lawell Principal Consultant Blog:SQLServerAssociates.com LinkedIn:LinkedIn.com/in/MikeLawell

47 Properties

50 Palette PRIMARY PALETTESECONDARY PALETTE 50

Titles are set 36 Segoe UI, maximum 2 lines, ideally 1 line Heading One Style, 28pt Body content, 18pt Segoe UI (gray) Heading Two Style, 24pt Body content, 18pt Segoe UI (gray) HEADING THREE STYLE, 16pt, BOLD, ALL CAPS Body content, 18pt Segoe UI (gray) 51

1.Resolve the Names of the objects (tables, columns, etc.) 2.Resolve the data types for the objects being accessed. 3.Finds aggregate operations (min, max, group by) and performs an operation called aggregate binding. If the names of the objects, columns aren’t found an error is output and the process halts. If it is successful the algebrizer outputs the query processor tree to the query optimizer. The query hash is generated and passed with the query processor tree. Algebrizer

The query optimizer checks the hash against the plan cache. If it exists use it and the optimization step is skipped and plan is passed to query execution. The query optimizer uses the query processor tree and the statistics to determine an estimated execution plan. The optimizer goes through a process of using different types of joins and indexes and assigns a cost to each step by CPU and I/O and determines a cost for each possible estimated plan it generates. Once an acceptable estimated plan is found it is stored in the plan cache then sent to the query execution. Query Optimizer

Storage Engine The storage engine determines if a recompile was triggered causing a change in the estimated execution plan. SQL Server determines based upon cost whether it exceeds the threshold for parallelism changing the estimated plan for parallelism. Statistics that have changed causing a change in the estimated plan. SQL Server returns the results. Query Execution