0% found this document useful (0 votes)
89 views31 pages

Plan Caching and Recompilation

This document provides an overview of Module 8 which covers plan caching and recompilation in SQL Server. It includes 4 lessons that cover the plan cache internals, troubleshooting with the plan cache, automatic tuning, and the query store. The lessons provide information on topics like plan cache management, examining the plan cache, recompilation issues, automatic tuning features like sys.dm_db_tuning_recommendations, and how to enable and configure the query store.

Uploaded by

Ayamin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views31 pages

Plan Caching and Recompilation

This document provides an overview of Module 8 which covers plan caching and recompilation in SQL Server. It includes 4 lessons that cover the plan cache internals, troubleshooting with the plan cache, automatic tuning, and the query store. The lessons provide information on topics like plan cache management, examining the plan cache, recompilation issues, automatic tuning features like sys.dm_db_tuning_recommendations, and how to enable and configure the query store.

Uploaded by

Ayamin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Module 8

Plan Caching and Recompilation


Module Overview

• Plan Cache Internals


• Troubleshooting with the Plan Cache
• Automatic Tuning
• Query Store
Lesson 1: Plan Cache Internals

• Query Plan Caching and Retrieval


• Plan Cache Management
• Queries Without Plan Cache Entries
• Maximizing Plan Cache Efficiency
• Examining the Plan Cache
• Demonstration: Analyzing the Query Plan Cache
Query Plan Caching and Retrieval

• Four plan cache stores, for different objects:


• Object Plans
• SQL Plans
• Bound Trees
• Extended Stored Procedures
• Compiled plan is held in a hash bucket in the relevant
plan cache store, uniquely identified by a plan handle
• A plan can be reused if bucket hash and plan cache key
match
• An executable plan is a session-specific instance of a
compiled plan
Plan Cache Management

• Plan cache housekeeping will begin when the


cache stores hit threshold values
• The least-expensive plans are removed first from
the plan cache
• Manually clear the plan cache with DBCC
commands
• Clear the cached plans for a single object with
sp_recompile
• Other operations clear the plan cache
Queries Without Plan Cache Entries

• Queries that cannot be cached:


• Ad hoc and prepared T-SQL statements requiring
object name resolution
• Queries marked for recompilation:
• CREATE…WITH RECOMPILE
• OPTION (RECOMPILE)
• EXECUTE…WITH RECOMPILE

• Natively compiled procedures for memory-


optimized tables
Maximizing Plan Cache Efficiency

• Auto-parameterization
• By default, only used for queries with an evenly
distributed cardinality
• Optimize for ad hoc workloads
• Ad hoc plans are cached only on second execution
• Prepared statements
• Used by database APIs (ODBC, OLEDB)
• Object Plans plan cache store
• Beware parameter sniffing
Examining the Plan Cache

• sys.dm_exec_cached_plans
• One row per cached plan
• sys.dm_exec_query_plan
• Query plan in XML format
• sys.dm_exec_text_query_plan
• Query plan in text format
• sys.dm_exec_plan_attributes
• Plan attributes
• sys.dm_exec_query_stats and
sys.dm_exec_procedure_stats
• Plan statistics
Demonstration: Analyzing the Query Plan Cache

In this demonstration, you will see:


• Methods for collecting information about the
plan cache
• A method for retrieving a query execution plan
for an ad hoc Transact-SQL statement from the
plan cache
• The effects of DBCC FREEPROCCACHE and
sp_recompile
• An example of auto-parameterization
Lesson 2: Troubleshooting with the Plan Cache

• Query Execution Plan Recompilation


• Recompilation Issues
• Problems of the Plan Cache
• Using the Plan Cache to Guide Optimization
• Demonstration: Troubleshooting Ad Hoc Plan
Caching
Query Execution Plan Recompilation

• Recompilation occurs when a cached plan is


invalidated and a new plan is compiled
• A cached plan may become invalid because:
• It becomes incorrect (for example, a schema change)
• It becomes non-optimal (for example, a statistics
change)
• Recompilation can be tracked using:
• SQL Profiler
• Extended Events
• Windows Performance Monitor
Recompilation Issues

• Parameter sniffing—fewer recompiles than


expected. Address with:
• OPTION(RECOMPILE)
• OPTION(OPTIMIZE FOR...)

• Statistics changes
• KEEP PLAN query hint reduces recompilations caused
by statistics changes
• KEEPFIXED PLAN query hint prevents recompilations
caused by statistics changes
• Try to avoid coding patterns that generate extra
recompilations
Problems of the Plan Cache

• Plan cache bloat:


• Caused by multiple copies of the same query execution
plan entering the cache
• Addressed by:
• Code changes
• Optimize for ad hoc workloads setting (server level)
• Monitor the plan cache with:
• Plan cache related wait statistics
• Windows performance counters
Using the Plan Cache to Guide Optimization

• Many performance measures are captured for


cache plans in sys.dm_exec_query_stats and
sys.dm_exec_procedure_stats
• Use these DMVs to identify candidate statements
for optimization and performance tuning
Demonstration: Troubleshooting Ad Hoc Plan Caching

In this demonstration, you will see:


• How to turn on the optimize for ad-hoc
workloads setting
• The effects of turning on the optimize for ad-
hoc workloads setting
Lesson 3: Automatic Tuning

• What is Automatic Tuning?


• sys.dm_db_tuning_recommendations
• Plan Choice Correction
What is Automatic Tuning?

• Changes in query execution plans that impact


performance can be time consuming to find and fix
• Plan choice regression is a recompiled plan that results in
poorer performance
• Automatic tuning
• Collects data about query performance and execution plans
• Finds links between plan changes and reduced performance
• Generates a script to force the previous plan to be used
• Can be configured to automatically apply the previous script
sys.dm_db_tuning_recommendations

• sys.dm_db_tuning_recommendations returns:
• A score between 0 and 100 to indicate the anticipated
performance impact
• A JSON string containing the recommendations,
including:
• Metrics used to identify plan choice regression
• Commands used to apply the recommendation
• Contents of sys.dm_db_tuning_recommendations
are retained until the instance is restarted
Plan Choice Correction

• Automatic plan choice correction:


• Enabled at the database level
• Plan choice recommendations are forced when:
• CPU time gain > 10 seconds
• Fewer errors in the recommended plan
• Results are monitored
• If performance does not improve, the plan is recompiled
• Manual plan choice correction
• Manually apply recommendations from the DMV
• Manually monitor performance and act on findings
Lesson 4: Query Store

• What is the Query Store?


• Enabling the Query Store
• Configuring the Query Store
• Accessing Query Store Data
• Forcing Query Execution Plans
• Demonstration: Working with the Query Store
What is the Query Store?

• Stores queries, plans, and run-time statistics


• Highlights database pattern usage
• Helps troubleshoot query performance issues
• Compatible with on-premises and Azure SQL
databases
Enabling the Query Store

• Switch on using ALTER DATABASE


ALTER DATABASE AdventureWorks SET
QUERY_STORE = ON;

• To use SSMS, right-click AdventureWorks, click


Properties, and then on the Query Store page,
change Operation Mode
• Not compatible with system databases
Configuring the Query Store

• View Query Store options using


sys.database_query_store_options
• Configure Query Store options using ALTER
DATABASE SET QUERY STORE (OPTION = VALUE)
• View and configure Query Store options using
SSMS
Accessing Query Store Data

• Access query stored data through DMVs or SSMS


• SSMS:
• Regressed Queries
• Overall Resource Consumption
• Top Resource Consuming Queries
• Tracked Queries

• DMVs:
• Parallel the plan cache DMVs
Forcing Query Execution Plans

• SSMS:
• Click Force Plan button when viewing a query plan in
the Query Store
• Click Unforce Plan button to undo
• Transact-SQL:
• Use sp_querystore_force_plan to force a plan
• Use sp_querystore_unforce_plan to unforce a plan
• Regularly check the performance of forced plans
Demonstration: Working with the Query Store

In this demonstration, you will see how to:


• Configure the Query Store
• Access Query Store data
• Force and unforce a query execution plan using
the Query Store
Lab: Plan Caching and Recompilation

• Exercise 1: Troubleshooting with the Plan Cache


• Exercise 2: Working with the Query Store

Logon Information
Virtual machine: 10987C-MIA-SQL
User name: ADVENTUREWORKS\Student
Password: Pa55w.rd

Estimated Time: 60 minutes


Lab Scenario

Adventure Works Cycles is a global manufacturer,


wholesaler, and retailer of cycle products. The
owners of the company have decided to start a
new direct marketing arm. It has been created as a
new company named Proseware Inc. Even though
it has been set up as a separate company, it will
receive some IT-related services from Adventure
Works and will be provided with a subset of the
corporate Adventure Works data.
Lab Scenario (continued)

The existing Adventure Works SQL Server platform


has been moved to a new server that is capable of
supporting both the existing workload and the
workload from the new company.
Lab Review

In this lab, you have learned how to troubleshoot


cached query plans, and how to use the Query
Store.
Module Review and Takeaways

In this module, you have learned about how query


execution plans are cached to enhance the
performance of the SQL Server Database Engine. In
addition to learning methods for viewing details of
cached plans, and influencing query plan selections,
you learned how to manage the content of the plan
cache.
You also learned about the Query Store, and how it
simplifies the process of troubleshooting query
plans.

You might also like