39
Understanding SQL Server Execution Plans Dr Greg Low

Understanding SQL Server Execution Plans

Embed Size (px)

Citation preview

Page 1: Understanding SQL Server Execution Plans

Understanding SQL Server

Execution Plans

Dr Greg Low

Page 2: Understanding SQL Server Execution Plans

Managing Director and Mentor – SolidQ Australia

Host of SQL Down Under Podcast

Microsoft Regional Director

Microsoft MVP for SQL Server

Organizer of SQL Down Under CodeCamp

Author with MS Press, SQL Server Magazine and Rational Press

Who am I?

Page 3: Understanding SQL Server Execution Plans

Session Prerequisites

Basic understanding of:

T-SQL

Table Design

Indexing

Desire to understand reading execution plans

Page 4: Understanding SQL Server Execution Plans

Module Overview

Core Execution Plan Concepts

Common Execution Plan Elements

Query, Table and Join Hints

Page 5: Understanding SQL Server Execution Plans

Why Query Plans Matter

Common Questions:

Why does my query take so long?

Why does this query take so much longer than a very

similar query?

Why is SQL Server ignoring this perfectly fine index I’ve

built?

Execution Plans can help:

Best use is in verifying expected plans

Page 6: Understanding SQL Server Execution Plans

Query Execution

SQL Server parses T-SQL

Resolves object references

Query Optimizer finds a sufficiently good plan

Plan calculation not required for DDL

Storage Engine executes the plan

Instance details stored in an execution context

Plan is stored in a Plan Cache

Page 7: Understanding SQL Server Execution Plans

Execution Plans

Optimizer is cost-based

Biggest impact CPU and I/O

Finding the best plan might take too long

Might decide on a trivial plan

SELECT * FROM SomeTable;

Index usage is a key decision

Statistics based decisions

Page 8: Understanding SQL Server Execution Plans

Actual vs Estimated Plans

Query Optimizer creates an estimated plan

Actual plan used may differ

Out of date statistics

Parallelism

Incorrect estimated plan

Page 9: Understanding SQL Server Execution Plans

Re-executing Queries

Plan re-use is generally desirable

Parameter sniffing an exception

Plans can become unusable

Correctness (SET options, schema changes, etc.)

Optimality (data modifications)

Options available to force compilation behavior

Plans are evicted based upon a cost algorithm

Page 10: Understanding SQL Server Execution Plans

Execution Plan Formats

Text-based plans

Now deprecated

Graphical Plans

XML-based plans

Page 11: Understanding SQL Server Execution Plans

Capturing Execution Plans

SQL Server Profiler

Can capture query execution details

Options for all query plan types

Can include XML column with query plan

Performance events> Showplan XML event

SQL Server Management Studio

Data Collector

SQL Server 2008 Management Data Warehouse

DMVs

Page 12: Understanding SQL Server Execution Plans

Viewing Execution Plans – SSMS

SQL Server Management Studio

Show estimated execution plan

Include actual execution plan in results

Compare plans between queries

Save/view plans in .sqlplan format

Specifically request plans in code

SET SHOWPLAN_XML ON;

Page 13: Understanding SQL Server Execution Plans

Viewing Execution Plans - DMVs

Dynamic Management Views (and Dynamic Management Functions) Show current server state

Not persisted internally

Many useful views/functions related to execution:sys.dm_exec_connections

sys.dm_exec_sessions

sys.dm_exec_requests

sys.dm_exec_sql_text()

sys.dm_exec_query_plan()

sys.dm_exec_cached_plans

sys.dm_exec_cached_plan_dependent_objects()

Page 14: Understanding SQL Server Execution Plans

View Cached Plans

SELECT cp.objtype AS PlanType,

OBJECT_NAME(st.objectid,st.dbid) AS ObjectName,

cp.refcounts AS ReferenceCounts,

cp.usecounts AS UseCounts,

st.text AS SQLBatch,

qp.query_plan AS QueryPlan

FROM sys.dm_exec_cached_plans AS cp

CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle) AS qp

CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) AS st;

Page 15: Understanding SQL Server Execution Plans

Core Execution Plan Concepts

demonstration

Page 16: Understanding SQL Server Execution Plans

Module Overview

Core Execution Plan Concepts

Common Execution Plan Elements

Query, Table and Join Hints

Page 17: Understanding SQL Server Execution Plans

SELECT Queries – Basic Operations

Page 18: Understanding SQL Server Execution Plans

Basic Operations

demonstration

Page 19: Understanding SQL Server Execution Plans

SELECT Queries - Lookups

Page 20: Understanding SQL Server Execution Plans

Lookups

demonstration

Page 21: Understanding SQL Server Execution Plans

Join Operations

Page 22: Understanding SQL Server Execution Plans

Join Operations

demonstration

Page 23: Understanding SQL Server Execution Plans

Aggregation

Page 24: Understanding SQL Server Execution Plans

Aggregation

demonstration

Page 25: Understanding SQL Server Execution Plans

Data Modification

Page 26: Understanding SQL Server Execution Plans

Data Modification

demonstration

Page 27: Understanding SQL Server Execution Plans

Views

Page 28: Understanding SQL Server Execution Plans

Views

demonstration

Page 29: Understanding SQL Server Execution Plans

Module Overview

Core Execution Plan Concepts

Common Execution Plan Elements

Query, Table and Join Hints

Page 30: Understanding SQL Server Execution Plans

Query Hints – Common Operations

HASH | ORDER GROUP

MERGE | HASH | CONCAT UNION

FAST n

FORCE ORDER

EXPAND VIEWS | NOEXPAND

MAXRECURSION

Page 31: Understanding SQL Server Execution Plans

Query Hints - Compilation

MAXDOP

OPTIMIZE FOR

RECOMPILE

PARAMETERIZATION SIMPLE | FORCED

ROBUST PLAN

KEEP PLAN

KEEPFIXED PLAN

USE PLAN

Page 32: Understanding SQL Server Execution Plans

JOIN Hints

LOOP JOIN

MERGE JOIN

HASH JOIN

Page 33: Understanding SQL Server Execution Plans

Table Hints

NOEXPAND

INDEX(n)

INDEX(name)

FASTFIRSTROW

Page 34: Understanding SQL Server Execution Plans

Join Hints

demonstration

Page 35: Understanding SQL Server Execution Plans

Session Summary

Excellent tooling available with SQL Server

Clear visibility into query execution

Hints to use (sparingly) to control behavior

Page 36: Understanding SQL Server Execution Plans

Upcoming Training

Melbourne

Advanced T-SQL – 6th July (5 days)

SQL Server Bootcamp for SharePoint Admins – 1st July (3 days)

Solving Business Problems with MDX – 23rd September (3 days)

Designing and Developing Solutions with Analysis Services –

14th July (4 days)

Working with SQL Server 2008 Integration Services – 3rd August

(2 days)

Working with SQL Server 2008 Reporting Services – 21st August

(2 days)

Page 37: Understanding SQL Server Execution Plans

Rushabh Mehta coming to visit

End to End Business Intelligence

5th October (5 days)

PASS Executive Vice President

Also heading to the SQL Code Camp

Page 38: Understanding SQL Server Execution Plans

Places to be!

PASS Community Connection

www.sqldownunder.com (SDU Code Camp tab)

October 10th and 11th Wagga Wagga

PASS U.S. Summit

www.sqlpass.org

November 2th to 5th Seattle

Page 39: Understanding SQL Server Execution Plans

Thanks!

[email protected]

www.sqldownunder.com

http://sqlblog.com/blogs/greg_low

www.solidq.com.au