Understanding query execution plans

21
Understanding Query Execution Plans Richard Douglas

description

Understanding query execution plans is something that a lot of IT professionals either don't know about or don't understand. SQL Server's programming language T-SQL is different from most other programming languages. In most languages you tell the language what you want it to do, in T-SQL you tell it what you want. This means you are at the mercy of the query optimiser which will take the logical instruction and convert it into a physical plan. The query execution plan is a map of what the optimiser thinks will be a good way of returning the data you have asked for. Being able to understand query plans and what they show is critical for both DBA's and developers in uncovering query performance problems. To view the webcast:http://dell.to/17k8zWK

Transcript of Understanding query execution plans

Page 1: Understanding query execution plans

Understanding Query Execution Plans

Richard Douglas

Page 2: Understanding query execution plans

2 Global MarketingUnderstanding Query Execution Plans

Agenda

• Introductions

• The Query Optimizer

• Commonly used operators

• Blocking and non-blocking operators

• Reading Query Execution Plans

Page 3: Understanding query execution plans

3 Global MarketingUnderstanding Query Execution Plans

Your host

• Richard Douglas

• Systems Consultant

• SQL Server MCITPro

• Maidenhead SQL User Group Leader

• Blog: http://SQL.RichardDouglas.co.uk

• Twitter: @SQLRich

• Email: [email protected]

Page 4: Understanding query execution plans

4 Global MarketingUnderstanding Query Execution Plans

Why do we need an optimizer?

The Query Optimizer

• T-SQL is a “What” not “how” language.

• We write “logical” requests.

• SQL Optimizer Engine converts logical requests into physical plans.

Page 5: Understanding query execution plans

5 Global MarketingUnderstanding Query Execution Plans

The job of the SQL Optimizer is to find “the best plan possible”.

The Query Optimizer

XWhat is the goal of the Optimizer?

Page 7: Understanding query execution plans

7 Global MarketingUnderstanding Query Execution Plans

Optimizer steps

Query Optimization (in a bit more detail)

Bind

Execute

Optimize

Parse

Page 8: Understanding query execution plans

8 Global MarketingUnderstanding Query Execution Plans

Parse

Builds a tree structure based upon the logical operators in the query.For example:

SELECT SSOD.[SalesOrderID], PP.[Name], PP.[Weight], SSOD.[UnitPrice]

FROM [Sales].[SalesOrderDetail] SSODINNER JOIN [Production].[Product] PP

ON SSOD.ProductID = PP.ProductIDWHERE PP.Weight > 100

Project

Filter

Join

ProductSalesOrderDetail

Log

ical O

pera

tion

s

Nod

es

Page 9: Understanding query execution plans

9 Global MarketingUnderstanding Query Execution Plans

Bind

• Series of validation steps

• Schema validation

• Table validation

• Attribute validation

• Permission validation

SELECT SSOD.[SalesOrderID], PP.[Name], PP.[Weight], SSOD.[UnitPrice]

FROM [Sales].[SalesOrderDetail] SSODINNER JOIN [Production].[Product] PP

ON SSOD.ProductID = PP.ProductIDWHERE PP.Weight > 100

Page 10: Understanding query execution plans

10 Global MarketingUnderstanding Query Execution Plans

Optimize

Works though many rules and heuristics.

These Include:

• Commutativity

• Substitution rules

• Exploration rules

• Implementation rules

Page 11: Understanding query execution plans

11 Global MarketingUnderstanding Query Execution Plans

Execute

Page 12: Understanding query execution plans

12 Global MarketingUnderstanding Query Execution Plans

Statistics

• SQL uses a cost based optimizer

• Costs influenced by statistics

Page 13: Understanding query execution plans

14 Global MarketingUnderstanding Query Execution Plans

Commonly used operators

SELECT UPDATE INSERT

DELETE Table scan Clustered Index Scan

NonClustered Index Scan

Clustered Index Seek

NonClustered Index Seek

Key Lookup Nested Loop Join Merge Join

Hash Join

Page 14: Understanding query execution plans

15 Global MarketingUnderstanding Query Execution Plans

Blocking and Non-blocking Operators

• Operators / Iterators can be put in two categories:1. Blocking2. Non-blocking

• Having a blocking operator in your plan means other operators further down the line are sitting idle.This will reduce the overall performance of your query

• Some examples…

Page 15: Understanding query execution plans

16 Global MarketingUnderstanding Query Execution Plans

Non-blocking example

Blocking and Non-blocking operators

• An example using a Compute Scalar function

Compute Scalar Function

Row 1Row 2Row 3Row 4Row 5

?

Page 16: Understanding query execution plans

17 Global MarketingUnderstanding Query Execution Plans

Blocking example

Blocking and Non-blocking operators

• An example using the sort operator:

Row 1Row 2Row 3Row 4Row 5

? Sort Desc

Page 17: Understanding query execution plans

Demo’s

Page 18: Understanding query execution plans

Pg. 22© 2012 Quest Software Inc. All rights reserved.

Solution Area Product DescriptionFast, flexible backup and recovery with industry-leading compression technology

Discover and resolve performance issues in production before they impact end users and service levels

Deepest possible understanding of database performance and norms

Plan and develop applications that deliver both functionality and

optimal performance

Project Lucy

Backup and Recovery

Performance & Operations

Performance Tuning

Development

Comprehensive schema, object, security and change management

Administration

Community crowdsourcing for SQL Server tracing and performance information!

Community, Knowledge, Training

Page 19: Understanding query execution plans

SQLRelay Dates 11/11/13 – Reading

12/11/13 – Southampton

13/11/13 – Cardiff 14/11/13 – Birmingham 15/11/13 – Hemel

Hempstead

25/11/13 – Newcastle 26/11/13 – Manchester 27/11/13 – Norwich 28/11/13 – Bristol 29/11/13 – London

Register at

sqlrelay.co.uk

@SQLRelay2013

Page 20: Understanding query execution plans

24 Global MarketingUnderstanding Query Execution Plans

Any questions?

Page 21: Understanding query execution plans

Thank you for attending

Richard [email protected]@SQLRich