Exploring Execution Plans, Learning to Read SQL Server Execution Plans

GrantFritchey 81 views 27 slides Oct 11, 2024
Slide 1
Slide 1 of 27
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27

About This Presentation

Getting started reading execution plans is very straight forward. The real issue is understanding the plans as they grow in size and complexity. This session will show you how to explore the nooks and crannies of an execution plan in order to more easily find the necessary information needed to make...


Slide Content

Exploring Execution Plans
Grant Fritchey

DevOps Advocate
Microsoft PostgreSQL MVP
AWS Community Builder
@gfritchey
[email protected]
scarydba.com
/in/gfritchey

Grant Fritchey | www.ScaryDBA.com
THE OPTIMIZER

Grant Fritchey | www.ScaryDBA.com
Where the Query Goes
QUERY
OPTIMIZER
QUERY
PROCESSOR
QUERY
WRITER
Q
u
e
r y
R
e
s
u
l
t
s
ExecutionPlan

Grant Fritchey | www.ScaryDBA.com
Relational Engine
QUERY

Grant Fritchey | www.ScaryDBA.com
QUERY
Relational
Engine
QUERY OPTIMIZER
Relational Engine

Grant Fritchey | www.ScaryDBA.com
QUERY
Relational
Engine
Query
Parsor
Syntax
CheckParse
Tree
QUERY OPTIMIZER
Relational Engine

Grant Fritchey | www.ScaryDBA.com
QUERY
Relational
Engine
Query
Parsor
Syntax
CheckParse
Tree
Algebrizer
Resolves
Objects
Query
Processor
Tree
QUERY OPTIMIZER
Relational Engine

Grant Fritchey | www.ScaryDBA.com
QUERY
Relational
Engine
Query
Parsor
Syntax
CheckParse
Tree
Algebrizer
Resolves
Objects
Query
Processor
Tree
Optimizer
Execution
Plan
QUERY OPTIMIZER
Relational Engine

Grant Fritchey | www.ScaryDBA.com
Relational Engine
QUERY
RESULT
Relational
Engine
Query
Parsor
Syntax
CheckParse
Tree
Algebrizer
Resolves
Objects
Query
Processor
Tree
Optimizer
Execution
Plan
Storage
Engine
QUERY OPTIMIZER

Grant Fritchey | www.ScaryDBA.com
What’s All This Mean?
SELECT ID FROM TableA WHERE ID = 42
Table A
SCAN
SEEK
OR

Grant Fritchey | www.ScaryDBA.com
What’s All This Mean?
SELECT a.ID, b.Name, c.Value
FROM TableA as a
JOIN TableB as b
On a.ID = B.ID
JOIN TableC as c
ON b.OtherID = c.OtherID
WHERE a.ID = 42
324 Possible Plans
LOOP HASH MERGE

Grant Fritchey | www.ScaryDBA.com
Flowchart
Plan
Look-up
Correctness
and Performance
Checks
Compile
New Plan
Execute
Query
Fetch Plan
From Cache
Recompile
Pass
Plan
Found
Plan
Not
Found

Grant Fritchey | www.ScaryDBA.com
Flowchart
Plan
Look-up
Correctness
and Performance
Checks
Compile
New Plan
Execute
Query
Fetch Plan
From Cache
Recompile
Pass
Plan
Found
Plan
Not
Found
EXECPT!
Query Store

Grant Fritchey | www.ScaryDBA.com
Types of Execution Plan
Estimated plan
and Actual plan
What really is the
difference?
Does the optimiser do a
quick job with an
estimated plan?
Does the optimiser do
less work for an
estimated plan?
No! None of those
Alternative names
Plan without runtime
information
Plan with runtime
information
But that’s longer to
write and say
Only real
difference is the
run-time
information
Actual rows affected
Actual executions
Data sizes
All costs are still
estimations
Session Code | All About Execution
Plans
15

Grant Fritchey | www.ScaryDBA.com
Optimization Levels
•Trivial
•Full
•Transactional Processing
•Quick Plan
•Full Optimization
Optimization
Level
•Timeout
•Good Enough Plan Found
•Memory Limit Exceeded
•Error!
Reason For
Early
Termination

Grant Fritchey | www.ScaryDBA.com
Where To Start?

Grant Fritchey | www.ScaryDBA.com
Where To Start?

Grant Fritchey | www.ScaryDBA.com
First Operator
Plan size
Compile time
Memory grant
Missing Indexes
Optimization level
Parameter
•Compiled value
•Runtime Value
Query hash
Reason for early termination
ANSI settings

Grant Fritchey | www.ScaryDBA.com
Right to Left or Left to Right?
A clue:
English
Another clue:
These things

Grant Fritchey | www.ScaryDBA.com
Left to Right or Right to Left
Answer: Both
Logical processing order:
•Represents how the optimizer sees the query
•Reading it from Left to Right
Physical processing order
•Represents the flow of data
•Follow the arrows/pipes from Right to Left
Both are necessary to understand certain plans

Grant Fritchey | www.ScaryDBA.com
What Else to Look For
Warnings
Most Costly Operations
Fat Pipes
Extra Operations
Scans
Estimated vs. Actual
Missing Indexes

Grant Fritchey | www.ScaryDBA.com
Where to Look
Tool tips
•Eh
Properties
•Follow the details

Grant Fritchey | www.ScaryDBA.com
DEMOS
24

Grant Fritchey | www.ScaryDBA.com
Questions?
How would you… ?
Why does… ?

26

Grant Fritchey | www.ScaryDBA.com
DevOps Advocate
Microsoft PostgreSQL MVP
AWS Community Builder
@gfritchey
[email protected]
scarydba.com
/in/gfritchey