Post on 22-Jun-2015
transcript
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
Session Goals
Viewing The Plan Cache
Showplan XML Structure
Scenarios and Demos
Wrap Up
SESSION AGENDA
SESSION GOALS
Wrap Up Scenarios
and Demos
Showplan XML
Structure
Viewing The Plan
Cache
Session Goals
1. Discuss the plan cache
2. Explore the plan cache
3. Demonstrate methods
to query plan cache
4. Demonstrate
performance tuning
concepts
SESSION GOALS
SQL Server 2012
SQL Server 2008 (R2)
SQL Server 2005
CONTENT COMPATIBILITY
VIEWING THE PLAN
CACHE
Wrap Up Scenarios
and Demos
Showplan XML
Structure
Viewing The Plan
Cache
Session Goals
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
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.
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
0x06000F001CF36A2640C1318A…
Varbinary(64)
Hash value
Identifies plan
PLAN HANDLE
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/
SHOWPLAN XML
STRUCTURE
Wrap Up Scenarios
and Demos
Showplan XML
Structure
Viewing The Plan
Cache
Session Goals
DBA-409-S
GETTING STARTED
DBA-409-S
GETTING STARTED
DBA-409-S
GETTING STARTED
DBA-409-S
GETTING STARTED
DBA-409-S
EXECUTION PLAN
DBA-409-S
SHOWPLAN XML STRUCTURE
What is in it?
SHOWPLAN XML STRUCTURE
http://www.flickr.com/photos/etringita/854298772/
ShowPlanXML
BatchSequence
Batch
Statements
StmtSimple
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
Volatile Data
Cache In Use
Use Xquery
Be Mindful
CAUTION
• 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
SCENARIOS AND DEMOS
Wrap Up Scenarios
and Demos
Showplan XML
Structure
Viewing The Plan
Cache
Session Goals
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
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?
Demo 1 MISSING INDEXES
Data types matter?
varchar vs. nvarchar
scan vs. seek
What plans have this issue?
CONVERSION WARNINGS
Here lies your
transaction
Columns with no
Statistics
Unmatched Indexes
Spill to Tempdb*
OTHER WARNINGS
Demo 2 IMPLICIT CONVERSIONS
AND OTHER WARNINGS
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
Demo 3 PARAMETER
SNIFFING
What’s the parallelism trigger?
• Parallelism is cost based
• Query cost default 5
STATEMENT INFORMATION
What does SQL Server expect?
• CPU
• IO
• Rows
• Cost
QUERY TREE ESTIMATES
Demo 3 QUERY PROPERTIES
LET’S GET PHYSICAL
Demo 5 PHYSICAL OPERATIONS
Access Methods:Full Scans/sec
INDEX OPERATIONS
Access Methods:Full Scans/sec
INDEX OPERATIONS
What about large tables?
Scans?
Good?
Bad?
What do you do when you
see large counts of scans
on an index?
INDEX OPERATIONS
What plans are using that index?
Should you drop it?
What is the potential effect?
INDEX OPERATIONS
http://www.flickr.com/photos/s3a/4436302537/
Demo 5 INDEX OPERATIONS
Find value in one index
Look up more columns in clustered index
KEY LOOKUP
You found them…
NOW fix them…
The information is in the plan!
• Investigate the nodes
• Pull in the data!
DEEPER FOR INCLUDED INDEXES
Demo 4 KEY LOOKUPS &
INCLUDED COLUMNS
WRAP UP
Wrap Up Scenarios
and Demos
Showplan XML
Structure
Viewing The Plan
Cache
Session Goals
http://schemas.microsoft.com/sqlserver
/2004/07/showplan/showplanxml.xsd
FIND YOUR OWN TREASURE
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
www.operationsmile.org
www.manning.com/delaney
SQL SERVER MVP DEEP DIVES, VOLUME 2
Jason Strate
e: jstrate@pragmaticworks.com
e: jasonstrate@gmail.com
b: www.jasonstrate.com
t: StrateSQL
Resources jasonstrate.com/go/Cache2013
RESOURCES