Bob Beauchemin Developer Skills Partner, SQLskills Session Code: DAT405 Agenda Peeking Into the Plan Cache Understand Plans and the Cache Plan Cache DMVs Parameterization and Parameter.

Download Report

Transcript Bob Beauchemin Developer Skills Partner, SQLskills Session Code: DAT405 Agenda Peeking Into the Plan Cache Understand Plans and the Cache Plan Cache DMVs Parameterization and Parameter.

Bob Beauchemin
Developer Skills Partner, SQLskills
Session Code: DAT405
Agenda
Peeking Into the Plan Cache
Understand Plans and the Cache
Plan Cache DMVs
Parameterization and Parameter Sniffing
Analyzing Performance Trends
Activity Monitor
Management Data Warehouse
Why Look At The Plan Cache
Query plans are the key to SQL's success
Query plan reuse is at the center of good
performance, cache shows
Which plans are used most frequently
If queries are parameterized
Autoparameterization data type choice
Correlate queries/plans with resource usage
Determine plan reuse potential
Statement Execution & Caching
New Statement
Found Executable
Plan
Lookup in
Plan Cache
Found Compiled
Plan
Not Found
Language Processing
Parse
Auto-Param
Bind, Expand Views
Query Optimization
Generate Executable Plan
Fix Memory Grant & DoP
Execute
Return Plans to Cache
(Parse/Bind, Statement/Batch Execution, Plan Cache
Management)
Query Optimization (Plan
Generation, View
Matching, Statistics,
Costing)
Query Execution (Query
Operators, Memory
Grants, Parallelism,
Showplan)
Tools for Troubleshooting Queries
Dynamic Management Views
SQL Profiler
Showplan
Performance Monitor
Database Tuning Advisor
SQL Server 2008 Adds
Data Collection - Management Data Warehouse
Extended Events
Where to Find Query Plan Info
SQL Server 2000-2008
Access syscacheobjects
SQL Server 2005/2008 – DMVs
sys.dm_exec_query_stats
For a variety of query statistics – like number of executions, plan
creation time (first execution into cache), last execution time, etc.
sys.dm_exec_cached_plans
sys.dm_exec_cached_plan_dependent_objects
For list of query plans, execution plans
sys.dm_exec_sql_text(sql_handle)
For the text of the sql statement executed
sys.dm_exec_query_plan(plan_handle)
sys.dm_exec_text_query_plan(plan_handle)
For the execution plan of the SQL statement executed
sys.dm_exec_plan_attributes(plan_handle)
Determine why plan is/is not being reused
sys.dm_exec_plan_optimizer_info
Optimizer steps for a plans
Sys.dm_exec_cached_plans
Maps to syscacheobjects from 2000
Not Just Query Plans
Compiled Plan
Compiled Plan Stub
Parse Tree
Extended Stored Procedure
SQLCLR Procedure
SQLCLR Function
Includes
Use Counts and Reference Counts
Plan Size
Handle to SQL statement
Sys.dm_exec_cached_plans
Discriminates between object types
Proc
Prepared Statement
Ad-Hoc Query
Replication Proc
Trigger
View
Default, Check Constraint, Rule
User Table, System Table
Sys.dm_exec_query_stats
Breaks query batches into statements
Statement start/end offset included
Resource Utilization is tracked per statement
Timings - worker time, elapsed time
I/O Activity
CLR time
Usage info
Plan generation number, create/last exec time
Handles to SQL statement, Query Plan
Sys.dm_exec_plan_attributes
Information associated with a plan
Cache keys
Execution context counts
Cursor options and information
Trigger plan as result as a MERGE statement
If attributes with is_cache_key are not the same
the plan will not be reused
What Affects Query Plans
Query Plans Created On Entry To
Batch Of SQL Statements
Procedural Code (e.g. Stored Procedure)
Plans don't consider
Current memory usage
Current lock table status
Plans do consider
Parameter values at plan creation time
Statistics
Plans tied to batch or stored procedure
And attributes
Plan Caches
Four major plan caches
Procedure cache
Adhoc query cache
XP cache
Bound Tree cache - views, defaults, rules
Query plans are reentrant
Execution plans are also cached
Execution plans not reentrant
Execution plans are reuseable
Inactive Cursor cached as part of the query plan
Plan reuse
Plan reuse is almost always is good thing
Compilation takes CPU
Less total plans -> more plans in plan cache
Plan reuse can be achieved by
Using stored procedures
Using parameterized queries
Autoparameterization
Plans can be aged out of cache
Under memory pressure
Same algorithm for all caches
Autoparameterization
Makes parameterized statement from nonparameterized
Both plans stored in plan cache
Data type chosen based on data value
Assists in query plan reuse
Two flavors of autoparameterization
Controlled as database set option or via plan guide
Simple - small set of query patterns
Forced - larger set of query patters
Parameter sniffing
Queries in procedure optimized on compile
First set of parameter values used
Never re-optimized for different values
Parameters not sniffed (or used)
If changed in procedure code
If passed to queries from variables
If parameter sniffing is a problem,
Use explicit recompiles
Modify code to be modular
multiple procedures not CASE or IF statements
OPTION (OPTIMIZE FOR VALUES/UNKNOWN)
Statement Recompilation
Plans are recompiled when
Metadata changes
Statistics change
All plan options do not match (cache_keys)
Other reasons
SELECT v.subclass_name, v.subclass_value
from sys.trace_events e inner join sys.trace_subclass_values v
on e.trace_event_id = v.trace_event_id
where e.name = 'SP:Recompile'
You can force a recompile
OPTION (RECOMPILE)
CREATE PROCEDURE ...WITH RECOMPILE
EXECUTE ...WITH RECOMPILE
sp_recompile
Plan Guides and Plan Freezing
Plan guide
A way to associate a query hint with a query
without changing the source query
Plan forcing
Specify plan as query hint in a plan guide
Plan freezing
Extract plan from cache for plan forcing
Template Plan Guides
Autoparameterization is controlled by a
database-level setting
Template plan guide can override that setting
On a per-query template basis
Query Fingerprinting
Hashes added for queries and query plans
sys.dm_exec_query_stats
sys.dm_exec_requests
Allow combining similar queries
For performance analysis
To detect parameterization opportunities
Cache Management Options - 2008
Optimize for ad-hoc workloads
Stores less info in plan cache (stub or plan vs. plan)
if plan will probably not be reused
Option access_check_cache_quota and
access_check_bucket_count
Controls TokenAndPermUserStore
Activity Monitor
Overview graphs of system health
Processes, Resource Waits, Data File I/O
Recent Expensive Queries Surface Plan Cache
Most expensive queries run in last 30 seconds
Sourced from sys.dm_exec_query_stats
Includes refactored query fingerprint information
Plan Count
Controllable Refresh Interval
Performance Monitor
You can read perfmon counters using
sys.dm_os_performance_counters
Plan Cache
Cache hit ratio
Cache pages
Object counts and objects in use
SQL Statistics
Auto-param attempts
Compilations, Recompilations
SQL Profiler
Profiler events related to SQL plans
Performance group
Degree of Parallelism assigned to statement
Performance statistics - plan re/compiled, evicted,
Auto stats
Plan guide matching
Showplan
Stored Procedure group
Cache activity - includes cache hit
SP Recompile
SQL statement recompile
Management Data Warehouse
MDW is built into SSMS 2008
Data Collection
Set of built-in collection sets
Set of reports
Extensibilities
Custom collection sets
T-SQL
Trace
Performance Monitor
Query Activity
Custom data collectors
Collection Sets and Reports
Query Statistics Activity
Query Statistics History
Query Statistics Detail
Drilldown to query plan
Disk Usage Information
Server Activity
Server Activity and Waits
Summary
Looking at the plan cache is useful for a variety
of reasons
Query plan reuse potential
Per-statement resource utilization
Easiest way is with DMVs
SQL Server tools provide visibility
SSMS Activity Monitor
SQL Profiler
Management Data Warehouse
Resources
www.microsoft.com/teched
www.microsoft.com/learning
Sessions On-Demand & Community
Microsoft Certification & Training Resources
http://microsoft.com/technet
http://microsoft.com/msdn
Resources for IT Professionals
Resources for Developers
Complete an evaluation
on CommNet and enter to
win an Xbox 360 Elite!
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should
not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS,
IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.