0
Understanding Query
Execution Plans
Richard Douglas
Agenda
• Introductions
• The Query Optimizer
• Commonly used operators

• Blocking and non-blocking operators
• Reading Qu...
Your host
• Richard Douglas
• Systems Consultant

• SQL Server MCITPro
• Maidenhead SQL User Group Leader
• Blog: http://S...
The Query Optimizer
Why do we need an optimizer?
• T-SQL is a “What” not “how” language.
• We write “logical” requests.
• ...
The Query Optimizer
What is the goal of the Optimizer?

The job of the
SQL Optimizer is
to find “the best
plan possible”.
...
Query optimization explained simply

1.

Query submitted

2. Magic happens
3. Shedload of data returned

6

Understanding ...
Query Optimization (in a bit more detail)
Optimizer steps
Parse

Bind

Optimize

Execute
7

Understanding Query Execution ...
Parse
Builds a tree structure based upon the logical operators in the query.
For example:

Logical Operations

SELECT
SSOD...
Bind
• Series of validation steps
• Schema validation
• Table validation

• Attribute validation
• Permission validation
S...
Optimize
Works though many rules and heuristics.
These Include:
• Commutativity

• Substitution rules
• Exploration rules
...
Execute

11

Understanding Query Execution Plans

Global Marketing
Statistics
• SQL uses a cost based optimizer
• Costs influenced by statistics

*Trace flag 2371 can change these threshold...
Commonly used operators

SELECT

UPDATE

INSERT

DELETE

Table scan

Clustered
Index Scan

NonClustered
Index Scan

Cluste...
Blocking and Non-blocking Operators
• Operators / Iterators can be put in two categories:
1. Blocking
2. Non-blocking

• H...
Blocking and Non-blocking operators
Non-blocking example
• An example using a Compute Scalar function

?

15

Understandin...
Blocking and Non-blocking operators
Blocking example
• An example using the sort operator:

?
16

Understanding Query Exec...
Demo’s
SQL Server Tools

SQL Server
Experience

Monitoring and
Diagnostics

Spotlight on SQL server
Enterprise

FREE TOOLS

Healt...
SpotlightEssentials.com

Free SQL Server Tools
Download at SpotlightEssentials.com

You can now monitor all your SQL Serve...
Any questions?

21

Understanding Query Execution Plans

Global Marketing
Thank you for attending
Richard Douglas
Richard.Douglas@Software.Dell.com
@SQLRich
Upcoming SlideShare
Loading in...5
×

Understanding Query Execution Plans - Richard Douglas

680

Published on

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

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
680
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Understanding Query Execution Plans - Richard Douglas"

  1. 1. Understanding Query Execution Plans Richard Douglas
  2. 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. 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. 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. 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. 6. Query optimization explained simply 1. Query submitted 2. Magic happens 3. Shedload of data returned 6 Understanding Query Execution Plans Global Marketing
  7. 7. Query Optimization (in a bit more detail) Optimizer steps Parse Bind Optimize Execute 7 Understanding Query Execution Plans Global Marketing
  8. 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. 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. 10. Optimize Works though many rules and heuristics. These Include: • Commutativity • Substitution rules • Exploration rules • Implementation rules 10 Understanding Query Execution Plans Global Marketing
  11. 11. Execute 11 Understanding Query Execution Plans Global Marketing
  12. 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. 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. 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. 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. 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
  17. 17. Demo’s
  18. 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. 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
  20. 20. Any questions? 21 Understanding Query Execution Plans Global Marketing
  21. 21. Thank you for attending Richard Douglas Richard.Douglas@Software.Dell.com @SQLRich
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×