Exploring Execution Plans, Learning to Read SQL Server Execution Plans
GrantFritchey
81 views
27 slides
Oct 11, 2024
Slide 1 of 27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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...
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 what the plan is telling you crystal clear. The information presented here will better empower you to traverse the execution plans you’ll see on your own servers. That knowledge will make it possible to more efficiently and accurately tune and troubleshoot your queries.
Size: 1.86 MB
Language: en
Added: Oct 11, 2024
Slides: 27 pages
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
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