Peeking into the Plan Cache with SQL Server 2008

Slides:



Advertisements
Similar presentations
MIX 09 4/15/ :14 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Advertisements

Connect with life Praveen Srvatsa Director | AsthraSoft Consulting Microsoft Regional Director, Bangalore Microsoft MVP, ASP.NET.
Tess Ferrandez ASP.NET Escalation Engineer Microsoft Session Code: WIA402.
Larry Mead Microsoft Corp. Jon Flanders Session Code: INT203.
Session 1.
Built by Developers for Developers…. © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
2 Avoiding Stored Procedure Recompiles Dr Greg Low Managing Director Solid Q Australia Session Code: DAT454.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Customer Combiner and Modifier © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
customer.
demo Demo.
Connect with life Nauzad Kapadia Quartz Systems
Gail Shaw XpertEase DAT 305 Topics Background Information Query Hints Plan Cache Metadata Plan Guides Plan Freezing Monitoring Plan Guide Use.
Alyson Powell Erwin Sr. Program Manager Microsoft BIN307.
Maciej Pilecki Consultant Project Botticelli Ltd. DAT404.
Connect with life Praveen Srivatsa Founder and CEO – AstraSoft.NET Vinod Kumar Technology Evangelist – Databases and BI.
Scott Morrison Program Manager Microsoft Corporation Session Code: WUX308.
How We Do Language Design at Microsoft (C#, Visual Basic, F#)
Tech·Ed North America /6/2018 2:20 AM
Tech·Ed  North America /11/ :01 AM SESSION CODE: DEV405
6/12/ :53 PM DEV311 Deep Dive into Microsoft Visual Studio Team Foundation Server 2010 Reporting Steven Borg, Principal ALM Consultant Northwest.
6/16/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
2010 Microsoft BI Conference
6/26/2018 9:02 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Upgrading Your C# Programming Skills to Be a More Effective Developer
Tech·Ed North America /31/2018 4:35 PM
Microsoft Ajax Taking Ajax to the Next Level
9/11/2018 5:53 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Tech·Ed North America /14/2018 7:13 PM
Tech·Ed North America /15/2018 3:31 AM
Optimizing Microsoft SQL Server 2008 Applications Using Table Valued Parameters, XML, and MERGE
Sysinternals Tutorials
Tech·Ed North America /19/ :44 PM
Advanced Dashboard Creation Using Microsoft SharePoint Server 2010
Jason Zander Unplugged
SQL Server 2016 Query Data Store
Tech·Ed North America /4/2018 2:51 AM
12/5/2018 3:24 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Tech·Ed North America /5/2018 6:43 PM
Tech·Ed North America /7/2018 9:06 AM
Tech·Ed North America /7/2018 2:51 PM
Ben Robb MVP, SharePoint Server cScape Ltd Session Code: OFS207
Data Driven ASP.NET Web Forms Applications Deep Dive
Tech Ed North America /1/ :36 AM Required Slide
Tech·Ed North America /2/2019 4:47 PM
TechEd /11/ :44 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered.
Tech·Ed North America /17/2019 1:47 AM
1/17/2019 9:05 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Tech·Ed North America /17/2019 6:01 PM
Microsoft SharePoint Conference 2009 Jon Flanders
Visual Studio 2010 SharePoint Development Tools Overview
Peter Provost Sr. Program Manager Microsoft Session Code: DEV312
Tech·Ed North America /22/2019 7:40 PM
Building Silverlight Apps with RIA Services
Developing Advanced Applications with Windows Azure
MDC-B203 Deploying Applications in Microsoft System Center Virtual Machine Manager Using Services John Messec Program Manager Microsoft.
Tech·Ed North America /28/ :49 PM
Power Query & Database Tuning
Tech Ed North America /12/2019 6:45 AM Required Slide
Tech·Ed North America /25/ :53 PM
Hack-proofing your Clients using Windows 7 Security!
Шитманов Дархан Қаражанұлы Тарих пәнінің
Lap Around the Windows Azure Platform
Code First Development in Microsoft ADO.NET Entity Framework 4.1
Building BI applications using PowerPivot for Excel
7/5/2019 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
7/28/ :33 PM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or.
Tech·Ed North America /7/2019 2:30 PM
Presentation transcript:

Peeking into the Plan Cache with SQL Server 2008 Bob Beauchemin Developer Skills Partner, SQLskills Session Code: DAT405

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

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

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

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

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

question & answer

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 www.microsoft.com/teched Sessions On-Demand & Community www.microsoft.com/learning Microsoft Certification & Training Resources http://microsoft.com/technet Resources for IT Professionals http://microsoft.com/msdn Resources for Developers

Complete an evaluation on CommNet and enter to win an Xbox 360 Elite!

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.