Download presentation
Presentation is loading. Please wait.
Published byNicholas Cain Modified over 6 years ago
2
Peeking into the Plan Cache with SQL Server 2008
Bob Beauchemin Developer Skills Partner, SQLskills Session Code: DAT405
3
Agenda Peeking Into the Plan Cache Analyzing Performance Trends
Understand Plans and the Cache Plan Cache DMVs Parameterization and Parameter Sniffing Analyzing Performance Trends Activity Monitor Management Data Warehouse
4
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
5
Statement Execution & Caching
New Statement Lookup in Plan Cache Found Executable Plan Found Compiled Plan Not Found Query Optimization (Plan Generation, View Matching, Statistics, Costing) Query Execution (Query Operators, Memory Grants, Parallelism, Showplan) Language Processing (Parse/Bind, Statement/Batch Execution, Plan Cache Management) Parse Auto-Param Bind, Expand Views Query Optimization Generate Executable Plan Fix Memory Grant & DoP Execute Return Plans to Cache
6
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
7
Where to Find Query Plan Info
SQL Server 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
8
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
9
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
10
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
11
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
12
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
13
Plan Caches Four major plan caches Query plans are reentrant
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
14
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
15
Autoparameterization
Makes parameterized statement from non-parameterized 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
16
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)
17
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
18
Plan Guides and Plan Freezing
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
19
Template Plan Guides Autoparameterization is controlled by a database-level setting Template plan guide can override that setting On a per-query template basis
20
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
21
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
22
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
23
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
24
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
25
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
26
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
27
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
28
question & answer
29
Resources Required Slide Speakers, www.microsoft.com/teched
TechEd 2009 is not producing a DVD. Please announce that attendees can access session recordings at TechEd Online. Resources Sessions On-Demand & Community Microsoft Certification & Training Resources Resources for IT Professionals Resources for Developers
30
Complete an evaluation on CommNet and enter to win an Xbox 360 Elite!
31
Required Slide © 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.