performance tuning with execution plans · performance tuning with execution plans. goal •show...

60
Performance Tuning with Execution Plans

Upload: others

Post on 19-Jul-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Performance Tuning with

Execution Plans

Page 2: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

GOAL

• Show where, how and why

performance issues appear within

execution plans in order to better

understand how to use execution

plans to troubleshoot SQL Server

query performance

Page 3: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Let’s Talk

scarydba.com

[email protected]

@gfritchey

Grant Fritchey

Page 4: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Today’s Agenda

• Introduction to Execution Plans

• Common T-SQL Code Smells

• Worked Examples

• Querying the Plan Cache

• More Worked Examples

• Parameter Sniffing

• Additional Tools

Page 5: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Introduction to Execution

Plans

Page 6: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better
Page 7: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Execution Plans

• Execution plans are a representation

of the processes used by the query

engine to perform the query

submitted to SQL Server.

Page 8: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Relational Engine

QUERY

RESULT

Relational

Engine

Query

Parsor

Syntax Check

Parse

Tree

Algebrizer

Resolves Objects

Query

Processor

Tree

Optimizer

Execution

Plan

Query

Processor

QUERY OPTIMIZER

Page 9: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Optimizer

• Cost-Based– Just an estimate

– Not based on your computer

• Statistics– Defined in indexes and tables

– Must be maintained to ensure a good execution plan

• Cache– Every query goes to cache (almost)

Page 10: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Generating a Plan

• SQL Server Management Studio– Estimated

– Actual

• Procedure Cache– Estimated (sort of)

• Extended Events– Estimated

– Actual

• Trace Events (not recommended)– Estimated

– Actual

Page 11: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Tune the QuerySmall to medium, look at the query first

Medium to large, go straight to the execution plan

Very large and insane, query the execution plan

Watch for low-hanging fruit

Fix syntax over statsStats over indexing

Indexing over restructuringRestructuring

Read the execution plan

Understand the business needs

Page 12: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Where To Start?

Page 13: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Where To Start?

Page 14: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

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

Page 15: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Right to Left or Left to Right?

• A clue: English

• Another clue: These things

Page 16: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

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

Page 17: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

What Else to Look For

• Warnings

• Most Costly Operations

• Fat Pipes

• Extra Operations

• Scans

• Estimated vs. Actual

Page 18: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Demo

Page 19: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Summary

• Execution plans are your view into the optimizer

• You can capture plans multiple ways

• You start with the first operator

• Additional things to look for include:– Warnings

– Most costly operations

– Fat pipes

– Extra operations

– Scans

– Estimated vs. Actual

• Remember that these are just representations

Page 20: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Common T-SQL Code

Smells

Page 21: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better
Page 22: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Code Smells

• A code smell is a piece of code that

functions, but doesn’t function in the

best possible way within a given set

of circumstances

Page 23: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

T-SQL Code Smells

• Functions on Predicates

• Data Conversion (Implicit & Explicit)

• Cursors

• Nested Views

• IF Logic

• Multi-Statement Table-Valued User Defined Functions

Page 24: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Demo

Page 25: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Worked Examples

Page 26: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Demo

Page 27: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Querying the Plan

Page 28: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better
Page 29: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Execution Plans From

CacheSELECT TOP 10

SUBSTRING(dest.text, (deqs.statement_start_offset / 2) + 1,

(CASE deqs.statement_end_offset

WHEN -1 THEN DATALENGTH(dest.text)

ELSE deqs.statement_end_offset

- deqs.statement_start_offset

END) / 2 + 1) AS querystatement,

deqp.query_plan,

deqs.query_hash,

deqs.execution_count

FROM sys.dm_exec_query_stats AS deqs

CROSS APPLY sys.dm_exec_query_plan(deqs.plan_handle) AS deqp

CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest

ORDER BY deqs.total_elapsed_time DESC;

Page 30: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Inside Execution PlansSELECT DB_NAME(deqp.dbid),

SUBSTRING(dest.text, (deqs.statement_start_offset / 2) + 1,

(CASE deqs.statement_end_offset

WHEN -1 THEN DATALENGTH(dest.text)

ELSE deqs.statement_end_offset

END - deqs.statement_start_offset) / 2 + 1) AS StatementText,

deqs.statement_end_offset,

deqs.statement_start_offset,

deqp.query_plan,

deqs.execution_count,

deqs.total_elapsed_time,

deqs.total_logical_reads,

deqs.total_logical_writes

FROM sys.dm_exec_query_stats AS deqs

CROSS APPLY sys.dm_exec_query_plan(deqs.plan_handle) AS deqp

CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest

WHERE CAST(deqp.query_plan AS NVARCHAR(MAX)) LIKE '%StatementOptmEarlyAbortReason="TimeOut"%';

Page 31: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Interesting Dynamic

Management Objects• Sys.dm_exec_query_plan

• sys.dm_exec_query_profiles

• Sys.dm_exec_text_query_plan

Page 32: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Demo

Page 33: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Additional Resources

• Sp_whoisactive – Adam Machanic

• Diagnostic Queries – Glen Berry

• Performance Tuning with SQL Server

Dynamic Management Views – Louis

Davidson and Tim Ford

Page 34: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

More Worked Examples

Page 35: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Demo

Page 36: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Parameter Sniffing

Page 37: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

http://download.red-

gate.com/ebooks/SQL/eBook_Perform

ance_Tuning_Davidson_Ford.pdf

Page 38: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Parameter Sniffing

• It’s a good thing… except when it

isn’t

• Automatic

• Only works on parameters (with an

exception)

• It’s all about statistics

– Average vs. Specific

Parameter list in execution plan

Page 39: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Bad Parameter Sniffing

• Differentiate from parameter sniffing

• Still about statistics

• Intermittent

• Different plans

• Focus on the compiled value

• Compare to runtime

• When it’s bad, it’s very bad

Page 40: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Local Variables

• Eliminate parameters

• Turn parameters into local variables

• Produces “generic” plan

Page 41: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Variable Sniffing

• The exception to parameters

• Same process

• Only works in a recompile situation

• Invisible killer or guardian angel

Page 42: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

OPTIMIZE FOR <value>

• Specific and accurate

• Changes over time

• Produces “precise” plan

Page 43: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

OPTIMIZE FOR

UNKNOWN• For when you’re not sure

• Changes over time

• Produces “generic” plan

Page 44: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

WITH RECOMPILE

• Specific every time

• Increases overhead

• May be more costly than

Page 45: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Statistics

• After all, it’s all about the statistics

• Stats can age w/o updating

• You may have auto-update turned

off

• Sampled updates may be

inadequate

• Filtered statistics may help

Page 46: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Plan Guides

• Just a different way to use hints

• Produces whatever plan you define

Page 47: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Turn Sniffing Off

• Dangerous choice

• Last for a reason

• Very dangerous

• Turns it all off

• Everywhere

• Did I mention it’s dangerous?

Page 48: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Demo

Page 49: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Additional Tools

Page 50: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better
Page 51: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Supratimas

• Web based

• Free

• Easy to use

• Limited Functionality

Page 52: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

SQL Sentry Plan Explorer

• Application

• Free and Paid Version

• Easy to Use

• Extensive Funtionality

Page 53: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Query Store

• Azure SQL Database

• SQL Server 2016

• Guaranteed to change how you

monitor and tune queries

Page 54: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Demo

Page 55: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Conclusion

Page 56: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Tune the Query

• Small to medium, look at the query first

• Medium to large, go straight to the execution plan

• Very large and insane, query the execution plan

• Watch for low-hanging fruit

• Fix syntax over stats– Stats over indexing

• Indexing over restructuring– Restructuring

• Read the execution plan

• Understand the business needs

56

Page 57: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better
Page 58: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Resources

• Scarydba.com/resources

• SQL Server Execution Plans

• SQL Server Query Performance

Tuning

Page 59: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Rate This Session Now!Rate with Mobile App:

• Select the session from the

Agenda or Speakers menus

• Select the Actions tab

• Click Rate Session

Rate with Website:

Register at www.devconnections.com/logintoratesession

Go to www.devconnections.com/ratesession

Select this session from the list and rate it

Tell Us

What

You

Thought

of This

Session

Be Entered to

WINPrizes!

#ITDEVCON

Page 60: Performance Tuning with Execution Plans · Performance Tuning with Execution Plans. GOAL •Show where, how and why performance issues appear within execution plans in order to better

Let’s Talk

scarydba.com

[email protected]

@gfritchey

Grant Fritchey