Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Server 2016 Execution Plan Analysis Liviu Ieran

Similar presentations


Presentation on theme: "SQL Server 2016 Execution Plan Analysis Liviu Ieran"— Presentation transcript:

1 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.

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

3 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.

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

5 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.

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

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

8 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.

9 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.

10 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.

11 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.

12 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.

13 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.

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

15 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.

16 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.

17 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.

18 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.

19 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.

20 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.

21 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.

22 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.

23 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.

24 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.


Download ppt "SQL Server 2016 Execution Plan Analysis Liviu Ieran"

Similar presentations


Ads by Google