SQL Server 2016 Execution Plan Analysis Liviu Ieran

Slides:



Advertisements
Similar presentations
Understanding SQL Server Query Execution Plans
Advertisements

SQL Server performance tuning basics
EXECUTION PLANS By Nimesh Shah, Amit Bhawnani. Outline  What is execution plan  How are execution plans created  How to get an execution plan  Graphical.
Dos and don’ts of Columnstore indexes The basis of xVelocity in-memory technology What’s it all about The compression methods (RLE / Dictionary encoding)
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
02 | Advanced SELECT Statements Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
The query processor does what the query plan tells it to do A “good” query plan is essential for a well- performing.
Module 8 Improving Performance through Nonclustered Indexes.
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.
Primary Key, Cluster Key & Identity Loop, Hash & Merge Joins Joe Chang
Applications hitting a wall today with SQL Server Locking/Latching Scale-up Throughput or latency SLA Applications which do not use SQL Server.
Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team Feb, SQL Server 2000.
Around the world (of query plan operators) in 50 minutes David Morrison BI Consultant.
Query Optimizer Execution Plan Cost Model Joe Chang
Maciej Pilecki | Project Botticelli Ltd.. SELECT Bio FROM Speakers WHERE FullName=‘Maciej Pilecki’;  Microsoft Certified Trainer since 2001  SQL Server.
Chapter 4 Indexes. Index Architecture  By default data is inserted on a first-come, first-serve basis  Indexes bring order to this chaos  Once you.
Eugene Meidinger Execution Plans
How to kill SQL Server Performance Håkan Winther.
Execution Plans for Mere Mortals A beginners look at execution plans. Mike Lawell, Teammate, Linchpin People.
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.
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
SQL Server Statistics and its relationship with Query Optimizer
An Introductory Look at Execution Plans
Execution Planning for Success
Execution Plans for Mere Mortals
02 | Advanced SELECT Statements
Query Tuning without Production Data
Query Tuning without Production Data
Query Tuning without Production Data
Reading execution plans successfully
Module 4: Creating and Tuning Indexes
Columnstore Index - is it the DW "Faster" switch you are looking for?
Introduction to SQL Server Management for the Non-DBA
Reading Execution Plans Successfully
Statistics for beginners
Introduction to Execution Plans
The Key to the Database Engine
Physical Join Operators
Back to Indexes, the Original Culprit !
Statistics What are the chances
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Execution Plans Demystified
Statistics: What are they and How do I use them
Reading Execution Plans Successfully
SQL Server Query Plans Journeyman and Beyond
Microsoft SQL Server 2014 for Oracle DBAs Module 7
Database systems Lecture 6 – Indexes
Introduction to reading execution plans
Statistics for beginners – In-Memory OLTP
Sunil Agarwal | Principal Program Manager
Dave Bland LinkedIn SQL Server Execution Plans: How to use them to find performance bottlenecks Dave Bland LinkedIn
Introduction to Execution Plans
“Magic numbers”, local variable and performance
EXECUTION PLANS Quick Dive.
Execution plans Eugene
Diving into Query Execution Plans
Introduction to Execution Plans
View ایحاد وتغییر Viewایجاد ایندکس روی
SQL Server Execution Plan Primer
Reading execution plans successfully
Introduction to Execution Plans
Welcome!.
All about Indexes Gail Shaw.
Presentation transcript:

SQL Server 2016 Execution Plan Analysis Liviu Ieran 12/2/2018 4:43 AM SQL Server 2016 Execution Plan Analysis Liviu Ieran © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Objectives Short Introduction to Execution Plans Most Encountered Operators Useful Information in a Plan Patterns to look out for

Short Introduction to Execution Plans 12/2/2018 4:43 AM Short Introduction to Execution Plans © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

What is an Execution Plan? What is it good for?

Graphical Showplan Flow 12/2/2018 4:43 AM Graphical Showplan Flow Outer Table 5 3 1 2 Inner table 4 6 Resultset 1 and 2 are joined using a nested loops join, creating resultset 3 Resultset 3 and 4 are joined using a hash match join, creating resultset 5 Resultset 5 and 6 are joined using a nested loops join, creating a resultset for the Select clause © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

How to capture an execution plan XEvents SQL Server Management Studio (Query Menu) sys.dm_exec_cached_plans SQL Profiler

Most Encountered Operators 12/2/2018 4:43 AM Most Encountered Operators © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Operators – Physical Joins 12/2/2018 4:43 AM Operators – Physical Joins © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Nested Loops Join 12/2/2018 4:43 AM © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

12/2/2018 4:43 AM Merge Join © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

12/2/2018 4:43 AM Hash Join © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Seek Operators Index Seek Clustered Index Seek Range Scans appear as either clustered or non- clustered index seeks with a RANGE predicate 12/2/2018 4:43 AM Seek Operators Index Seek Clustered Index Seek © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Scan Operators Table Scan Clustered Index Scan Index Scan 12/2/2018 4:43 AM Scan Operators Table Scan Clustered Index Scan Index Scan Columnstore Index Scan © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Notable Operators Table Spool Index Spool Sort Stream Aggregation Hash Match (Aggregation) References For more information, see: Showplan Logical and Physical Operators Reference -http://msdn.microsoft.com/en-us/library/ms191158 © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Demonstration: Join Algorithms 12/2/2018 4:43 AM Demonstration: Join Algorithms SELECT sod.OrderQty, od.EventDetails FROM Sales.OrderTracking od INNER JOIN Sales.SalesOrderDetail sod ON od.SalesOrderID = sod.SalesOrderID SELECT TOP 1000 sod.OrderQty, od.EventDetails FROM Sales.OrderTracking od SELECT TOP 10 sod.OrderQty, od.EventDetails FROM Sales.OrderTracking od © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Useful Information in a Plan 12/2/2018 4:43 AM Useful Information in a Plan © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Contents of a Query Plan 12/2/2018 4:43 AM Contents of a Query Plan How data is accessed and joined Sequence of operations Use of sorts Estimated vs. Actual row counts and iterations How data is aggregated Use of parallelism Query execution warnings ….and many more © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Demonstration: Graphical Query Plans 12/2/2018 4:43 AM Demonstration: Graphical Query Plans --demo 2a SET CONCAT_NULL_YIELDS_NULL OFF SET STATISTICS TIME ON SELECT p.FirstName + ' ' + p.MiddleName + ' ' + p.LastName as FullName, a.City, a.AddressLine1 as Adress, at.Name as AdressType FROM Person.Person p INNER JOIN Person.BusinessEntityAddress bea ON p.BusinessEntityID = bea.BusinessEntityID INNER JOIN Person.Address a ON bea.AddressID = a.AddressID INNER JOIN Person.AddressType at ON at.AddressTypeID = bea.AddressTypeID --demo 2b EXEC uspGetEmployeeManagers 30 GO EXEC uspGetEmployeeManagers 40 --dbcc freeproccache © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Patterns to look out for 12/2/2018 4:43 AM Patterns to look out for © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Patterns to look out for 12/2/2018 4:43 AM Patterns to look out for Very thick arrows <–> large amount of data Table Scans (do I have a where clause?) Huge difference between estimated and actual rows High numbers of RID or Key lookups Query execution warnings: Spills to tempdb Implicit conversions © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Demonstration: Look for patterns 12/2/2018 4:43 AM Demonstration: Look for patterns --demo 3a USE [AdventureWorks2016CTP3] GO DROP TABLE IF EXISTS [dbo].[SalesOrderHeader_SQLSA] DROP TABLE IF EXISTS [dbo].[SalesOrderdetail_SQLSA] DROP TABLE IF EXISTS [dbo].[customer_SQLSA] SELECT * INTO SalesOrderHeader_SQLSA FROM Sales.SalesOrderHeader SELECT * INTO SalesOrderdetail_SQLSA FROM Sales.SalesOrderDetail SELECT * INTO Customer_SQLSA FROM Sales.Customer SET STATISTICS TIME ON SET STATISTICS IO ON SELECT c.CustomerID, so.OrderDate, so.ShipDate, sod.ProductID, sod.OrderQty, sod.UnitPrice, sod.UnitPriceDiscount FROM dbo.Customer_SQLSA c JOIN dbo.SalesOrderHeader_SQLSA so ON c.CustomerID=so.CustomerID JOIN dbo.SalesOrderdetail_SQLSA sod ON so.SalesOrderID= sod.SalesOrderID WHERE ShipDate between '07/01/2012' AND '07/31/2012' USE [master] ALTER DATABASE [AdventureWorks2016CTP3] SET AUTO_CREATE_STATISTICS ON WITH NO_WAIT ALTER DATABASE [AdventureWorks2016CTP3] SET AUTO_CREATE_STATISTICS OFF WITH NO_WAIT FROM Sales.Customer c JOIN Sales.SalesOrderHeader so ON c.CustomerID=so.CustomerID JOIN Sales.SalesOrderdetail sod ON so.SalesOrderID= sod.SalesOrderID WHERE ShipDate between '07/01/2012' AND '07/31/2012‘ --demo 3b DROP TABLE IF EXISTS [dbo].[Person_SQLSA] SELECT * INTO Person_SQLSA FROM Person.Person ALTER TABLE [dbo].[Person_SQLSA] ALTER COLUMN FirstName varchar (50) ; CREATE NONCLUSTERED INDEX [IX_Person_Firstname] ON [dbo].[Person_SQLSA] ( [FirstName] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] select * from dbo.Person_SQLSA WHERE FirstName = N'Clay' WHERE FirstName = 'Clay‘ --demo 3c SELECT * FROM Sales.OrderTracking ot INNER JOIN Sales.SalesOrderDetail sod ON ot.SalesOrderID = sod.SalesOrderID WHERE EventDetails LIKE 'ORDER%' Order by ot.EventDetails CREATE STATISTICS [TempStat] ON [Sales].[OrderTracking]([EventDetails]) dbcc freeproccache DROP STATISTICS [Sales].[OrderTracking].[TempStat] © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

A few short tips Keep stats up to date (fullscan) 12/2/2018 4:43 AM A few short tips Keep stats up to date (fullscan) Are my where clauses indexed? Are my join conditions indexed? OldCE vs NewCE (since SQL 2014) TF4199 (pre SQL 2016, comp level 130) Matching data types Defragment indexes © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

12/2/2018 4:43 AM Questions? © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

12/2/2018 4:43 AM Note: This should be the final slide for each presentation © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.