Understanding query execution plans is something many IT professionals either don’t know about or don’t fully 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, but in T-SQL, you tell it what you want. This means you are at the mercy of the query optimizer, 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 DBAs and developers in uncovering query performance problems.
To view the webcast go to:
http://software-dell-com/webcast-ondemand/understanding-sql-server-query-execution-plans-826629
2. Agenda
• Introductions
• The Query Optimizer
• Commonly used operators
• Blocking and non-blocking operators
• Reading Query Execution Plans
2
Understanding Query Execution Plans
Global Marketing
3. 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
3
Understanding Query Execution Plans
Global Marketing
4. The Query Optimizer
Why do we need an optimizer?
• T-SQL is a “What” not “how” language.
• We write “logical” requests.
• SQL Optimizer Engine converts
logical requests into physical
plans.
4
Understanding Query Execution Plans
Global Marketing
5. The Query Optimizer
What is the goal of the Optimizer?
The job of the
SQL Optimizer is
to find “the best
plan possible”.
5
Understanding Query Execution Plans
Global Marketing
6. Query optimization explained simply
1.
Query submitted
2. Magic happens
3. Shedload of data returned
6
Understanding Query Execution Plans
Global Marketing
7. Query Optimization (in a bit more detail)
Optimizer steps
Parse
Bind
Optimize
Execute
7
Understanding Query Execution Plans
Global Marketing
8. Parse
Builds a tree structure based upon the logical operators in the query.
For example:
Logical Operations
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
Nodes
Project
Filter
Join
8
Understanding Query Execution Plans
Sales
Order
Detail
Product
Global Marketing
9. 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
9
Understanding Query Execution Plans
Global Marketing
10. Optimize
Works though many rules and heuristics.
These Include:
• Commutativity
• Substitution rules
• Exploration rules
• Implementation rules
10
Understanding Query Execution Plans
Global Marketing
12. 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/c
hanges-to-automatic-update-statistics-in-sql-server-traceflag2371.aspx
12
Understanding Query Execution Plans
Global Marketing
13. 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
13
Understanding Query Execution Plans
Global Marketing
14. 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…
14
Understanding Query Execution Plans
Global Marketing
15. Blocking and Non-blocking operators
Non-blocking example
• An example using a Compute Scalar function
?
15
Understanding Query Execution Plans
Compute
Scalar
Function
Row 1
Row 2
Row 3
Row 4
Row 5
Global Marketing
16. Blocking and Non-blocking operators
Blocking example
• An example using the sort operator:
?
16
Understanding Query Execution Plans
Sort
Desc
Row 1
Row 2
Row 3
Row 4
Row 5
Global Marketing
18. SQL Server Tools
SQL Server
Experience
Monitoring and
Diagnostics
Spotlight on SQL server
Enterprise
FREE TOOLS
Healthcheck,
Performance
Analytics,
Freemium
Monitoring
Free Tools at
www.SpotlightEssentials.com
NetVault LiteSpeed for SQL
Server
Innovation
Backup
Leadership
Administration
Toad for SQL Server
and Development
Business
Intelligence
Toad BI Suite
Application
Performance
Monitoring
Foglight for SQL Server
19. SpotlightEssentials.com
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
Alarms
Real Time
Notifications