Understanding Query Execution Plans - Richard Douglas
-
Upload
sqldbapros -
Category
Technology
-
view
652 -
download
0
description
Transcript of Understanding Query Execution Plans - Richard Douglas
Understanding Query Execution Plans
Richard Douglas
2 Global Marketing Understanding Query Execution Plans
Agenda
• Introductions
• The Query Optimizer
• Commonly used operators
• Blocking and non-blocking operators
• Reading Query Execution Plans
3 Global Marketing Understanding 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]
4 Global Marketing Understanding 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 Marketing Understanding Query Execution Plans
The job of the SQL Optimizer is to find “the best plan possible”.
The Query Optimizer What is the goal of the Optimizer?
6 Global Marketing Understanding Query Execution Plans
Query optimization explained simply
1. Query submitted
2. Magic happens
3. Shedload of data returned
7 Global Marketing Understanding Query Execution Plans
Optimizer steps
Query Optimization (in a bit more detail)
Bind
Execute
Optimize
Parse
8 Global Marketing Understanding 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] SSOD INNER JOIN [Production].[Product] PP ON SSOD.ProductID = PP.ProductID WHERE PP.Weight > 100
Project
Filter
Join
Product Sales Order Detail
Lo
gic
al O
pe
rati
on
s
No
de
s
9 Global Marketing Understanding 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] SSOD INNER JOIN [Production].[Product] PP ON SSOD.ProductID = PP.ProductID WHERE PP.Weight > 100
10 Global Marketing Understanding Query Execution Plans
Optimize
Works though many rules and heuristics.
These Include:
• Commutativity
• Substitution rules
• Exploration rules
• Implementation rules
11 Global Marketing Understanding Query Execution Plans
Execute
12 Global Marketing Understanding Query Execution Plans
Statistics
• SQL uses a cost based optimizer
• Costs influenced by statistics
*Trace flag 2371 can change these thresholds.
It is undocumented and was included in 2008 R2 SP1. http://blogs.msdn.com/b/saponsqlserver/archive/2011/09/07/changes-to-automatic-update-statistics-in-sql-server-traceflag-2371.aspx
13 Global Marketing Understanding 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
14 Global Marketing Understanding Query Execution Plans
Blocking and Non-blocking Operators
• Operators / Iterators can be put in two categories: 1. Blocking 2. 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…
15 Global Marketing Understanding Query Execution Plans
Non-blocking example
Blocking and Non-blocking operators
• An example using a Compute Scalar function
Compute Scalar Function
Row 1
Row 2
Row 3
Row 4 Row 5
?
16 Global Marketing Understanding Query Execution Plans
Blocking example
Blocking and Non-blocking operators
• An example using the sort operator:
Row 1
Row 2
Row 3
Row 4 Row 5
? Sort Desc
Demo’s
SQL Server Tools Monitoring and
Diagnostics
Spotlight on SQL server
Enterprise
FREE TOOLS
Healthcheck,
Performance
Analytics,
Freemium
Monitoring
Free Tools at
www.SpotlightEssentials.com
Backup NetVault LiteSpeed for SQL
Server
Administration
and Development Toad for SQL Server
Business
Intelligence Toad BI Suite
Application
Performance
Monitoring
Foglight for SQL Server
Leadership
Innovation
Experience
SQL Server
Free SQL Server Tools Download at SpotlightEssentials.com
You can now monitor all your SQL Server Connections for FREE within SQL Server Management Studio. Monitoring features include a Heatmap, Alarms List and detailed analytics for each connection
SpotlightEssentials.com
Alarms Real Time Notifications
21 Global Marketing Understanding Query Execution Plans
Any questions?