discovering the plan cache (#sqlsat 206)

49
Jason Strate Database Architect Pragmatic Works, Inc. DISCOVERING THE PLAN CACHE

Upload: jason-strate

Post on 22-Jun-2015

344 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Discovering the Plan Cache (#SQLSat 206)

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)

Session Goals

Viewing The Plan Cache

Showplan XML Structure

Scenarios and Demos

Wrap Up

SESSION AGENDA

Page 3: Discovering the Plan Cache (#SQLSat 206)

SESSION GOALS

Wrap Up Scenarios

and Demos

Showplan XML

Structure

Viewing The Plan

Cache

Session Goals

Page 4: Discovering the Plan Cache (#SQLSat 206)

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)

SQL Server 2012

SQL Server 2008 (R2)

SQL Server 2005

CONTENT COMPATIBILITY

Page 6: Discovering the Plan Cache (#SQLSat 206)

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)

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)

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)

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)

0x06000F001CF36A2640C1318A…

Varbinary(64)

Hash value

Identifies plan

PLAN HANDLE

Page 11: Discovering the Plan Cache (#SQLSat 206)

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)

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)

DBA-409-S

GETTING STARTED

Page 14: Discovering the Plan Cache (#SQLSat 206)

DBA-409-S

GETTING STARTED

Page 15: Discovering the Plan Cache (#SQLSat 206)

DBA-409-S

GETTING STARTED

Page 16: Discovering the Plan Cache (#SQLSat 206)

DBA-409-S

GETTING STARTED

Page 17: Discovering the Plan Cache (#SQLSat 206)

DBA-409-S

EXECUTION PLAN

Page 18: Discovering the Plan Cache (#SQLSat 206)

DBA-409-S

SHOWPLAN XML STRUCTURE

Page 19: Discovering the Plan Cache (#SQLSat 206)

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)

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)

Volatile Data

Cache In Use

Use Xquery

Be Mindful

CAUTION

Page 22: Discovering the Plan Cache (#SQLSat 206)

• 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)

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)

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)

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)

Demo 1 MISSING INDEXES

Page 27: Discovering the Plan Cache (#SQLSat 206)

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)

Columns with no

Statistics

Unmatched Indexes

Spill to Tempdb*

OTHER WARNINGS

Page 29: Discovering the Plan Cache (#SQLSat 206)

Demo 2 IMPLICIT CONVERSIONS

AND OTHER WARNINGS

Page 30: Discovering the Plan Cache (#SQLSat 206)

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)

Demo 3 PARAMETER

SNIFFING

Page 32: Discovering the Plan Cache (#SQLSat 206)

What’s the parallelism trigger?

• Parallelism is cost based

• Query cost default 5

STATEMENT INFORMATION

Page 33: Discovering the Plan Cache (#SQLSat 206)

What does SQL Server expect?

• CPU

• IO

• Rows

• Cost

QUERY TREE ESTIMATES

Page 34: Discovering the Plan Cache (#SQLSat 206)

Demo 3 QUERY PROPERTIES

Page 35: Discovering the Plan Cache (#SQLSat 206)

LET’S GET PHYSICAL

Page 36: Discovering the Plan Cache (#SQLSat 206)

Demo 5 PHYSICAL OPERATIONS

Page 37: Discovering the Plan Cache (#SQLSat 206)

Access Methods:Full Scans/sec

INDEX OPERATIONS

Page 38: Discovering the Plan Cache (#SQLSat 206)

Access Methods:Full Scans/sec

INDEX OPERATIONS

Page 39: Discovering the Plan Cache (#SQLSat 206)

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)

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)

Demo 5 INDEX OPERATIONS

Page 42: Discovering the Plan Cache (#SQLSat 206)

Find value in one index

Look up more columns in clustered index

KEY LOOKUP

Page 43: Discovering the Plan Cache (#SQLSat 206)

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)

Demo 4 KEY LOOKUPS &

INCLUDED COLUMNS

Page 45: Discovering the Plan Cache (#SQLSat 206)

WRAP UP

Wrap Up Scenarios

and Demos

Showplan XML

Structure

Viewing The Plan

Cache

Session Goals

Page 46: Discovering the Plan Cache (#SQLSat 206)

http://schemas.microsoft.com/sqlserver

/2004/07/showplan/showplanxml.xsd

FIND YOUR OWN TREASURE

Page 47: Discovering the Plan Cache (#SQLSat 206)

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)

www.operationsmile.org

www.manning.com/delaney

SQL SERVER MVP DEEP DIVES, VOLUME 2

Page 49: Discovering the Plan Cache (#SQLSat 206)

Jason Strate

e: [email protected]

e: [email protected]

b: www.jasonstrate.com

t: StrateSQL

Resources jasonstrate.com/go/Cache2013

RESOURCES