49
Jason Strate Database Architect Pragmatic Works, Inc. DISCOVERING THE PLAN CACHE SQL SATURDAY 175 FARGO

Discovering the plan cache (sql sat175)

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Discovering the plan cache (sql sat175)

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 SQL SATURDAY 175

FARGO

Page 2: Discovering the plan cache (sql sat175)

Session Goals

Viewing The Plan Cache

Showplan XML Structure

Scenarios and Demos

Wrap Up

SESSION AGENDA

Page 3: Discovering the plan cache (sql sat175)

SESSION GOALS

Wrap Up Scenarios

and Demos

Showplan XML

Structure

Viewing The Plan

Cache

Session Goals

Page 4: Discovering the plan cache (sql sat175)

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 (sql sat175)

SQL Server 2012

SQL Server 2008 (R2)

SQL Server 2005

CONTENT COMPATIBILITY

Page 6: Discovering the plan cache (sql sat175)

VIEWING THE PLAN

CACHE

Wrap Up Scenarios

and Demos

Showplan XML

Structure

Viewing The Plan

Cache

Session Goals

Page 7: Discovering the plan cache (sql sat175)

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 (sql sat175)

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 (sql sat175)

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 (sql sat175)

0x06000F001CF36A2640C1318A…

Varbinary(64)

Hash value

Identifies plan

PLAN HANDLE

Page 11: Discovering the plan cache (sql sat175)

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 (sql sat175)

SHOWPLAN XML

STRUCTURE

Wrap Up Scenarios

and Demos

Showplan XML

Structure

Viewing The Plan

Cache

Session Goals

Page 13: Discovering the plan cache (sql sat175)

DBA-409-S

GETTING STARTED

Page 14: Discovering the plan cache (sql sat175)

DBA-409-S

GETTING STARTED

Page 15: Discovering the plan cache (sql sat175)

DBA-409-S

GETTING STARTED

Page 16: Discovering the plan cache (sql sat175)

DBA-409-S

GETTING STARTED

Page 17: Discovering the plan cache (sql sat175)

DBA-409-S

EXECUTION PLAN

Page 18: Discovering the plan cache (sql sat175)

DBA-409-S

SHOWPLAN XML STRUCTURE

Page 19: Discovering the plan cache (sql sat175)

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 (sql sat175)

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 (sql sat175)

Volatile Data

Cache In Use

Use Xquery

Be Mindful

CAUTION

Page 22: Discovering the plan cache (sql sat175)

• 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 (sql sat175)

SCENARIOS AND DEMOS

Wrap Up Scenarios

and Demos

Showplan XML

Structure

Viewing The Plan

Cache

Session Goals

Page 24: Discovering the plan cache (sql sat175)

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 (sql sat175)

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 (sql sat175)

Demo 1 MISSING INDEXES

Page 27: Discovering the plan cache (sql sat175)

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 (sql sat175)

Columns with no

Statistics

Unmatched Indexes

Spill to Tempdb*

OTHER WARNINGS

Page 29: Discovering the plan cache (sql sat175)

Demo 2 IMPLICIT CONVERSIONS

AND OTHER WARNINGS

Page 30: Discovering the plan cache (sql sat175)

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 (sql sat175)

Demo 3 PARAMETER

SNIFFING

Page 32: Discovering the plan cache (sql sat175)

What’s the parallelism trigger?

• Parallelism is cost based

• Query cost default 5

STATEMENT INFORMATION

Page 33: Discovering the plan cache (sql sat175)

What does SQL Server expect?

• CPU

• IO

• Rows

• Cost

QUERY TREE ESTIMATES

Page 34: Discovering the plan cache (sql sat175)

Demo 3 QUERY PROPERTIES

Page 35: Discovering the plan cache (sql sat175)

LET’S GET PHYSICAL

Page 36: Discovering the plan cache (sql sat175)

Demo 5 PHYSICAL OPERATIONS

Page 37: Discovering the plan cache (sql sat175)

Access Methods:Full Scans/sec

INDEX OPERATIONS

Page 38: Discovering the plan cache (sql sat175)

Access Methods:Full Scans/sec

INDEX OPERATIONS

Page 39: Discovering the plan cache (sql sat175)

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 (sql sat175)

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 (sql sat175)

Demo 5 INDEX OPERATIONS

Page 42: Discovering the plan cache (sql sat175)

Find value in one index

Look up more columns in clustered index

KEY LOOKUP

Page 43: Discovering the plan cache (sql sat175)

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 (sql sat175)

Demo 4 KEY LOOKUPS &

INCLUDED COLUMNS

Page 45: Discovering the plan cache (sql sat175)

WRAP UP

Wrap Up Scenarios

and Demos

Showplan XML

Structure

Viewing The Plan

Cache

Session Goals

Page 46: Discovering the plan cache (sql sat175)

http://schemas.microsoft.com/sqlserver

/2004/07/showplan/showplanxml.xsd

FIND YOUR OWN TREASURE

Page 47: Discovering the plan cache (sql sat175)

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 (sql sat175)

www.operationsmile.org

www.manning.com/delaney

SQL SERVER MVP DEEP DIVES, VOLUME 2

Page 49: Discovering the plan cache (sql sat175)

Jason Strate

e: [email protected]

e: [email protected]

b: www.jasonstrate.com

t: StrateSQL

Resources jasonstrate.com/go/Cache2013

RESOURCES