Understanding Query Execution Plans - Richard Douglas

21
Understanding Query Execution Plans Richard Douglas

description

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

Transcript of Understanding Query Execution Plans - Richard Douglas

Page 1: Understanding Query Execution Plans - Richard Douglas

Understanding Query Execution Plans

Richard Douglas

Page 2: 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

Page 3: Understanding Query Execution Plans - Richard Douglas

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]

Page 4: Understanding Query Execution Plans - Richard Douglas

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.

Page 5: Understanding Query Execution Plans - Richard Douglas

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?

Page 7: Understanding Query Execution Plans - Richard Douglas

7 Global Marketing Understanding Query Execution Plans

Optimizer steps

Query Optimization (in a bit more detail)

Bind

Execute

Optimize

Parse

Page 8: Understanding Query Execution Plans - Richard Douglas

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

Page 9: Understanding Query Execution Plans - Richard Douglas

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

Page 10: Understanding Query Execution Plans - Richard Douglas

10 Global Marketing Understanding 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 - Richard Douglas

11 Global Marketing Understanding Query Execution Plans

Execute

Page 12: Understanding Query Execution Plans - Richard Douglas

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

Page 13: Understanding Query Execution Plans - Richard Douglas

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

Page 14: Understanding Query Execution Plans - Richard Douglas

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…

Page 15: Understanding Query Execution Plans - Richard Douglas

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

?

Page 16: Understanding Query Execution Plans - Richard Douglas

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

Page 17: Understanding Query Execution Plans - Richard Douglas

Demo’s

Page 18: Understanding Query Execution Plans - Richard Douglas

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

Page 19: Understanding Query Execution Plans - Richard Douglas

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

Page 20: Understanding Query Execution Plans - Richard Douglas

21 Global Marketing Understanding Query Execution Plans

Any questions?

Page 21: Understanding Query Execution Plans - Richard Douglas

Thank you for attending

Richard Douglas [email protected] @SQLRich