Understanding SQL Server Execution Plans
Understanding SQL Server Execution Plans
Dr Greg Low
Who am I?
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
Session Prerequisites
Basic understanding of:
T-SQL Table Design Indexing
Module Overview
Core Execution Plan Concepts
similar query? Why is SQL Server ignoring this perfectly fine index Ive built?
Query Execution
SQL Server parses T-SQL Resolves object references Query Optimizer finds a sufficiently good plan
Plan calculation not required for DDL
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;
Re-executing Queries
Plan re-use is generally desirable
Parameter sniffing an exception
Options available to force compilation behavior Plans are evicted based upon a cost algorithm
DMVs
Management Functions)
Show current server state Not persisted internally
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;
demonstration
Core Execution Plan Concepts
Module Overview
Core Execution Plan Concepts
demonstration
Basic Operations
demonstration
Lookups
Join Operations
demonstration
Join Operations
Aggregation
demonstration
Aggregation
Data Modification
demonstration
Data Modification
Views
demonstration
Views
Module Overview
Core Execution Plan Concepts
MAXRECURSION
KEEP PLAN
KEEPFIXED PLAN USE PLAN
JOIN Hints
LOOP JOIN MERGE JOIN HASH JOIN
Table Hints
NOEXPAND INDEX(n) INDEX(name) FASTFIRSTROW
demonstration
Join Hints
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)
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)
Places to be!
PASS Community Connection
www.sqldownunder.com (SDU Code Camp tab)
Thanks!
[email protected] www.sqldownunder.com http://sqlblog.com/blogs/greg_low www.solidq.com.au