Understanding SQL Server Execution Plans

Post on 02-Dec-2014

31 views 5 download

transcript

Understanding SQL Server

Execution Plans

Dr Greg Low

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?

Session Prerequisites

Basic understanding of:

T-SQL

Table Design

Indexing

Desire to understand reading execution plans

Module Overview

Core Execution Plan Concepts

Common Execution Plan Elements

Query, Table and Join Hints

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

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

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

Actual vs Estimated Plans

Query Optimizer creates an estimated plan

Actual plan used may differ

Out of date statistics

Parallelism

Incorrect estimated plan

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

Execution Plan Formats

Text-based plans

Now deprecated

Graphical Plans

XML-based 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

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;

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

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;

Core Execution Plan Concepts

demonstration

Module Overview

Core Execution Plan Concepts

Common Execution Plan Elements

Query, Table and Join Hints

SELECT Queries – Basic Operations

Basic Operations

demonstration

SELECT Queries - Lookups

Lookups

demonstration

Join Operations

Join Operations

demonstration

Aggregation

Aggregation

demonstration

Data Modification

Data Modification

demonstration

Views

Views

demonstration

Module Overview

Core Execution Plan Concepts

Common Execution Plan Elements

Query, Table and Join Hints

Query Hints – Common Operations

HASH | ORDER GROUP

MERGE | HASH | CONCAT UNION

FAST n

FORCE ORDER

EXPAND VIEWS | NOEXPAND

MAXRECURSION

Query Hints - Compilation

MAXDOP

OPTIMIZE FOR

RECOMPILE

PARAMETERIZATION SIMPLE | FORCED

ROBUST PLAN

KEEP PLAN

KEEPFIXED PLAN

USE PLAN

JOIN Hints

LOOP JOIN

MERGE JOIN

HASH JOIN

Table Hints

NOEXPAND

INDEX(n)

INDEX(name)

FASTFIRSTROW

Join Hints

demonstration

Session Summary

Excellent tooling available with SQL Server

Clear visibility into query execution

Hints to use (sparingly) to control behavior

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)

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

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

Thanks!

greg@greglow.com

www.sqldownunder.com

http://sqlblog.com/blogs/greg_low

www.solidq.com.au