Post on 14-Dec-2014
description
Understanding Query Execution Plans
Richard Douglas
2 Global MarketingUnderstanding Query Execution Plans
Agenda
• Introductions
• The Query Optimizer
• Commonly used operators
• Blocking and non-blocking operators
• Reading 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: Richard.Douglas@Software.Dell.com
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.
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?
6 Global MarketingUnderstanding Query Execution Plans
Query optimization explained simply
1. Query submitted
2. Magic happens
3. Shedload of data returned
7 Global MarketingUnderstanding Query Execution Plans
Optimizer steps
Query Optimization (in a bit more detail)
Bind
Execute
Optimize
Parse
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
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
10 Global MarketingUnderstanding Query Execution Plans
Optimize
Works though many rules and heuristics.
These Include:
• Commutativity
• Substitution rules
• Exploration rules
• Implementation rules
11 Global MarketingUnderstanding Query Execution Plans
Execute
12 Global MarketingUnderstanding Query Execution Plans
Statistics
• SQL uses a cost based optimizer
• Costs influenced by statistics
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
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…
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
?
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
Demo’s
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
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
24 Global MarketingUnderstanding Query Execution Plans
Any questions?
Thank you for attending
Richard DouglasRichard.Douglas@Software.Dell.com@SQLRich