Download presentation
Presentation is loading. Please wait.
1
Plan cache performance tuning
Arthur Daniels
2
DBA-Art.com Twitter: @ArthurDanSQL
Arthur Daniels DBA at SAP Fieldglass Enjoys tuning SQL
3
Contents First half: What is the plan cache Second half: Demos
4
How execution plans are made
Someone sends a query to your SQL Server Execution plan is created SQL stores the plan in cache for reuse
5
Architecture of the plan cache
In the plan cache, every plan is a template If a query matches, the template will be used!* Match is determined based the query used to create the template, SET options such as SET ANSI NULLS ON/OFF, along with other requirements based on the type of compiled plan. Plan cache helps with plan re-use and stable performance
6
Compiled plans Adhoc Proc Prepared
Prepared and Proc are ideal for plan re-use. Adhoc will only re-use if the text is an exact match
7
DMVs/Ways to query the Plan Cache
Sys.dm_exec_cached_plans Sys.dm_exec_sql_text Sys.dm_exec_query_plan Sys.dm_exec_query_stats
8
Query hash SQL Handle Query Plan Hash Plan Handle
9
So why do we care? Performance! “Bad” templates in the cache
Finding implicit conversions and queries that need to be tuned
10
Intro to performance tools
SQL Query Stress Created by Adam Machanic Allows you to run queries/workloads
14
First, take a look at your cache
Pick any scripts to analyze your plan cache but seriously, use sp_BlitzCache Created by Jeremiah Peschka, free, open source
15
All plan caches have quirks
It’s likely all the top findings are bad. Form an action plan ordered by what you perceive is the highest priority Log sp_BlitzCache to a table during peak server usage to establish a baseline That doesn’t mean it’s time to freak out.
16
Prep: Running sp_BlitzCache to a table
Tools used: Sp_BlitzCache SQL Query Stress Run QueryStress-PostType-3 to create a basic load. Then, clear the cache, and run QueryStress-PostType-1 before moving onto Demo 2. This puts the bad plan in cache
17
Implicit Conversions – A Problem
18
Demo 2: What do bad queries look like?
Run bad queries
19
Intro to Bad Parameter Sniffing
SQL Server records parameters Picks the best execution plan for just that set!
20
Setting up a parameter sniffing scenario
In Stack Overflow, Post Type = 1 are questions Post Type = 3 are wiki There’s 1,100,336 questions and 28 wiki
21
Demo 3: Parameter Sniffing
Use SQL Query Stress
22
Solutions to this Let’s fix it!
Manually DBCC FREEPROCCACHE(specific plan) Scheduling a job to watch for the plan By Google, licensed under Apache License 2.0 via GitHub
23
Demo 4: Timed clear of plan cache
24
New tool: the Query Store
Requires SQL Server 2016 Tools and Records What Query Store can’t do
25
Demo 5: Solving the same problem but with Query Store
26
DBA-Art.com Search Plan Cache for warning signs
Bad templates can ruin performance Force fix inside Query Store Tools used today: Query Store Sp_BlitzCache SQL Query Stress
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.