Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey | Reading Execution Plans: The Deep...

36
Grant Fritchey | www.ScaryDBA.com www.ScaryDBA.com Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Transcript of Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey | Reading Execution Plans: The Deep...

Page 1: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

www.ScaryDBA.com

Reading Execution

Plans: The Deep Dive

for PASS Performance Virtual Chapter 4/26/12

Grant Fritchey

Page 2: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Grant Fritchey

Product Evangelist for Red Gate Software

Twitter: @gfritchey

Blog: scarydba.com

Email: [email protected]

Page 3: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

A Query

SELECT soh.PurchaseOrderNumber, soh.AccountNumber, p.Name, sod.OrderQty, sod.LineTotal, cc.CardNumber FROM Sales.SalesOrderHeader AS soh JOIN Sales.SalesOrderDetail AS sod ON soh.SalesOrderID = sod.SalesOrderID JOIN Production.Product AS p ON sod.ProductID = p.ProductID JOIN Sales.CreditCard AS cc ON soh.CreditCardID = cc.CreditCardID

3

Page 4: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

A plan

4

Page 5: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

The Select Operator

5

Page 6: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Clustered Index Scan

6

Page 7: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Merge Join

7

Page 8: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Hash Join

8

Page 9: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

The Pipe

9

Page 10: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Compute Scalar

10

Page 11: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

An Entirely New Query

SELECT soh.PurchaseOrderNumber, soh.AccountNumber, p.Name, sod.OrderQty, sod.LineTotal, cc.CardNumber FROM Sales.SalesOrderHeader AS soh JOIN Sales.SalesOrderDetail AS sod ON soh.SalesOrderID = sod.SalesOrderID JOIN Production.Product AS p ON sod.ProductID = p.ProductID JOIN Sales.CreditCard AS cc ON soh.CreditCardID = cc.CreditCardID WHERE soh.CustomerID = 24624

11

Page 12: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

An Entirely New Query

SELECT soh.PurchaseOrderNumber, soh.AccountNumber, p.Name, sod.OrderQty, sod.LineTotal, cc.CardNumber FROM Sales.SalesOrderHeader AS soh JOIN Sales.SalesOrderDetail AS sod ON soh.SalesOrderID = sod.SalesOrderID JOIN Production.Product AS p ON sod.ProductID = p.ProductID JOIN Sales.CreditCard AS cc ON soh.CreditCardID = cc.CreditCardID WHERE soh.CustomerID = 24624

12

Page 13: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

A New Plan

13

Page 14: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

The Select Operator 2

14

Page 15: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Index Seek

15

Page 16: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Nested Loop Join

16

Page 17: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Key Lookup

17

Page 18: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Stored Procedure

IF (SELECT OBJECT_ID('spAddressByCity')

) IS NOT NULL

DROP PROCEDURE dbo.spAddressByCity ;

GO

CREATE PROC dbo.spAddressByCity @City NVARCHAR(30)

AS

SELECT a.AddressID,

a.AddressLine1,

a.AddressLine2,

a.City,

sp.[Name] AS StateProvinceName,

a.PostalCode

FROM Person.Address AS a

JOIN Person.StateProvince AS sp

ON a.StateProvinceID = sp.StateProvinceID

WHERE a.City = @City ;

18

Page 19: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

The Plan

19

Page 20: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Sort

20

Page 21: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

The Select Operator 3

21

Page 22: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

The Plan?

22

Page 23: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

The Select Operator 4

23

Page 24: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Delete Query

BEGIN TRAN

DELETE FROM Production.Product

WHERE ProductID = 3343

ROLLBACK TRAN

24

Page 25: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

The Plan

25

Page 26: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Delete Operator

26

Page 27: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Assert

27

Page 28: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Table Spool

28

Page 29: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

A View

SELECT ve.FirstName,

ve.LastName,

ve.BusinessEntityID

FROM HumanResources.vEmployee AS ve

WHERE ve.BusinessEntityID = 221

29

Page 30: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

View Execution Plan

30

Page 31: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

The Real Plan

31

Page 32: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

A little XML

WITH XMLNAMESPACES(DEFAULT N'http://schemas.microsoft.com/sqlserver/2004/07/showplan'),

QueryPlans AS

(

SELECT RelOp.pln.value(N'@PhysicalOp', N'varchar(50)') AS OperatorName,

RelOp.pln.value(N'@NodeId',N'integer') AS NodeId,

RelOp.pln.value(N'@EstimateCPU', N'decimal(10,9)') AS CPUCost,

RelOp.pln.value(N'@EstimateIO', N'decimal(10,9)') AS IOCost,

dest.text

FROM sys.dm_exec_query_stats AS deqs

CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest

CROSS APPLY sys.dm_exec_query_plan(deqs.plan_handle) AS deqp

CROSS APPLY deqp.query_plan.nodes(N'//RelOp') RelOp (pln)

)

SELECT qp.OperatorName,

qp.NodeId,

qp.CPUCost,

qp.IOCost,

qp.CPUCost + qp.IOCost AS EstimatedCost

FROM QueryPlans AS qp

WHERE qp.text LIKE 'SELECT * FROM HumanResources.vEmployee AS ve'

ORDER BY EstimatedCost DESC

32

Page 33: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

What To Look For

First Operator

Warnings

Most Costly Operations

Fat Pipes

Extra Operations

Scans

Page 34: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Resources

Scarydba.com/Resources

34

Page 35: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Grant Fritchey

Product Evangelist for Red Gate Software

Twitter: @gfritchey

Blog: scarydba.com

Email: [email protected]

Page 36: Reading Execution Plans: The Deep Dive€¦ · Grant Fritchey |   Reading Execution Plans: The Deep Dive for PASS Performance Virtual Chapter 4/26/12 Grant Fritchey

Grant Fritchey | www.ScaryDBA.com

Questions?

36