Discovering the Plan Cache (#SQLSat 206)
-
Upload
jason-strate -
Category
Technology
-
view
344 -
download
0
Transcript of Discovering the Plan Cache (#SQLSat 206)
![Page 1: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/1.jpg)
J a s o n S t r a t e
D a t a b a s e A r c h i t e c t
P r a g m a t i c W o r k s , I n c .
DISCOVERING
THE PLAN CACHE
![Page 2: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/2.jpg)
Session Goals
Viewing The Plan Cache
Showplan XML Structure
Scenarios and Demos
Wrap Up
SESSION AGENDA
![Page 3: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/3.jpg)
SESSION GOALS
Wrap Up Scenarios
and Demos
Showplan XML
Structure
Viewing The Plan
Cache
Session Goals
![Page 4: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/4.jpg)
1. Discuss the plan cache
2. Explore the plan cache
3. Demonstrate methods
to query plan cache
4. Demonstrate
performance tuning
concepts
SESSION GOALS
![Page 5: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/5.jpg)
SQL Server 2012
SQL Server 2008 (R2)
SQL Server 2005
CONTENT COMPATIBILITY
![Page 6: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/6.jpg)
VIEWING THE PLAN
CACHE
Wrap Up Scenarios
and Demos
Showplan XML
Structure
Viewing The Plan
Cache
Session Goals
![Page 7: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/7.jpg)
Portion of SQL Server memory that stores execution plans
that have been prepared by the Query Optimizer. Execution
plans are used to by SQL Server to execute SQL statements.
PLAN CACHE
![Page 8: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/8.jpg)
Pros No setup
Query-able
Actionable
Real World
Cons Reset with service
Activity flush
Plans miss cache
In Use
PLAN CACHE
Portion of SQL Server memory that stores execution plans
that have been prepared by the Query Optimizer. Execution
plans are used to by SQL Server to execute SQL statements.
![Page 9: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/9.jpg)
sys.dm_exec_cached_plans
• All Plans
• Size
• Use count
sys.dm_exec_query_plan
(plan_handle)
• Table Valued Function
• SHOWPLAN XML as XML
sys.dm_exec_text_query_plan
(plan_handle,0, -1)
• Table Valued Function
• SHOWPLAN XML as text
PLAN CACHE
![Page 10: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/10.jpg)
0x06000F001CF36A2640C1318A…
Varbinary(64)
Hash value
Identifies plan
PLAN HANDLE
![Page 11: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/11.jpg)
sys.dm_exec_requests
Current connection
Vaguely similar to sp_who
sys.dm_exec_query_stats
Stats on executed queries
Contains SQL_Handle
sys.dm_exec_cached_plans
Stats on cached plans
Distinct list of plans
WHERE IN THE WORLD IS PLAN_HANDLE?
http://www.flickr.com/photos/fallentomato/3918329246/
![Page 12: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/12.jpg)
SHOWPLAN XML
STRUCTURE
Wrap Up Scenarios
and Demos
Showplan XML
Structure
Viewing The Plan
Cache
Session Goals
![Page 13: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/13.jpg)
DBA-409-S
GETTING STARTED
![Page 14: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/14.jpg)
DBA-409-S
GETTING STARTED
![Page 15: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/15.jpg)
DBA-409-S
GETTING STARTED
![Page 16: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/16.jpg)
DBA-409-S
GETTING STARTED
![Page 17: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/17.jpg)
DBA-409-S
EXECUTION PLAN
![Page 18: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/18.jpg)
DBA-409-S
SHOWPLAN XML STRUCTURE
![Page 19: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/19.jpg)
What is in it?
SHOWPLAN XML STRUCTURE
http://www.flickr.com/photos/etringita/854298772/
ShowPlanXML
BatchSequence
Batch
Statements
StmtSimple
![Page 20: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/20.jpg)
GOING BELOW STMTSIMPLE
Missing Indexes
• Indexes that would improve plan performance
Parameter List
•Parameters passed into query
Warnings
•Plan Affecting Convert
•Columns with no Statistics
•Unmatched Indexes
RelOp
•EstimateRows, EstimateIO, PhysicalOp,
•Hash, IndexScan, NestedLoops
•OutputList
![Page 21: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/21.jpg)
Volatile Data
Cache In Use
Use Xquery
Be Mindful
CAUTION
![Page 22: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/22.jpg)
• Forward slashes
/ means to find the specific location
// means find any from the current location
• Important Functions
• nodes()
• value()
• exist()
• query()
• Everything is case sensitive
XQUERY FUNCTIONS
![Page 23: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/23.jpg)
SCENARIOS AND DEMOS
Wrap Up Scenarios
and Demos
Showplan XML
Structure
Viewing The Plan
Cache
Session Goals
![Page 24: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/24.jpg)
What plans would benefit from indexes?
DMVs
sys.dm_db_missing_index_columns
sys.dm_db_missing_index_details
sys.dm_db_missing_index_group_stats
sys.dm_db_missing_index_groups
DBA-409-S
MISSING INDEXES
![Page 25: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/25.jpg)
What plans would benefit from indexes?
DMVs
sys.dm_db_missing_index_columns
sys.dm_db_missing_index_details
sys.dm_db_missing_index_group_stats
sys.dm_db_missing_index_groups
MISSING INDEXES
http://www.flickr.com/photos/fallentomato/3918329246/
What Queries
Should I Test?
![Page 26: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/26.jpg)
Demo 1 MISSING INDEXES
![Page 27: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/27.jpg)
Data types matter?
varchar vs. nvarchar
scan vs. seek
What plans have this issue?
CONVERSION WARNINGS
Here lies your
transaction
![Page 28: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/28.jpg)
Columns with no
Statistics
Unmatched Indexes
Spill to Tempdb*
OTHER WARNINGS
![Page 29: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/29.jpg)
Demo 2 IMPLICIT CONVERSIONS
AND OTHER WARNINGS
![Page 30: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/30.jpg)
Think about the plans you
find example…
What has changed in the
environments?
Is the plan using the wrong
values?
What parameters is the
plan using?
POOR PARAMETER SNIFFING
![Page 31: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/31.jpg)
Demo 3 PARAMETER
SNIFFING
![Page 32: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/32.jpg)
What’s the parallelism trigger?
• Parallelism is cost based
• Query cost default 5
STATEMENT INFORMATION
![Page 33: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/33.jpg)
What does SQL Server expect?
• CPU
• IO
• Rows
• Cost
QUERY TREE ESTIMATES
![Page 34: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/34.jpg)
Demo 3 QUERY PROPERTIES
![Page 35: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/35.jpg)
LET’S GET PHYSICAL
![Page 36: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/36.jpg)
Demo 5 PHYSICAL OPERATIONS
![Page 37: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/37.jpg)
Access Methods:Full Scans/sec
INDEX OPERATIONS
![Page 38: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/38.jpg)
Access Methods:Full Scans/sec
INDEX OPERATIONS
![Page 39: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/39.jpg)
What about large tables?
Scans?
Good?
Bad?
What do you do when you
see large counts of scans
on an index?
INDEX OPERATIONS
![Page 40: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/40.jpg)
What plans are using that index?
Should you drop it?
What is the potential effect?
INDEX OPERATIONS
http://www.flickr.com/photos/s3a/4436302537/
![Page 41: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/41.jpg)
Demo 5 INDEX OPERATIONS
![Page 42: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/42.jpg)
Find value in one index
Look up more columns in clustered index
KEY LOOKUP
![Page 43: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/43.jpg)
You found them…
NOW fix them…
The information is in the plan!
• Investigate the nodes
• Pull in the data!
DEEPER FOR INCLUDED INDEXES
![Page 44: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/44.jpg)
Demo 4 KEY LOOKUPS &
INCLUDED COLUMNS
![Page 45: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/45.jpg)
WRAP UP
Wrap Up Scenarios
and Demos
Showplan XML
Structure
Viewing The Plan
Cache
Session Goals
![Page 46: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/46.jpg)
http://schemas.microsoft.com/sqlserver
/2004/07/showplan/showplanxml.xsd
FIND YOUR OWN TREASURE
![Page 47: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/47.jpg)
1. Plan Cache IS your
performance work load
2. Information is readily
available
3. Techniques to access
not as complex as
appearances
4. Scale your performance
knowledge across the
environment
SUMMARY
![Page 48: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/48.jpg)
www.operationsmile.org
www.manning.com/delaney
SQL SERVER MVP DEEP DIVES, VOLUME 2
![Page 49: Discovering the Plan Cache (#SQLSat 206)](https://reader033.fdocuments.net/reader033/viewer/2022052600/55876c61d8b42ace0b8b46bc/html5/thumbnails/49.jpg)
Jason Strate
b: www.jasonstrate.com
t: StrateSQL
Resources jasonstrate.com/go/Cache2013
RESOURCES