Let’s Get Started! Rick Lowe Why Should I Care About … The Plan Cache

Slides:



Advertisements
Similar presentations
How a little code can help with support.. Chris Barba – Developer at Cimarex Energy Blog:
Advertisements

Developers of a suite of products to help you monitor and optimize Windows/SQL Server performance o Performance Advisor – awareness and control over Windows.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Danette Dineen Riviello Magellan Health March 17,
DMV Performance Monitoring & Tuning Presented by Franklin Yamamoto.
SQLRX – SQL Server Administration – Tips From the Trenches SQL Server Administration – Tips From the Trenches Troubleshooting Reports of Sudden Slowdowns.
Instructor: Craig Duckett Lecture 03: Tuesday, April 14, 2015 SQL Sorting, Aggregates and Joining Tables 1.
Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007.
1 Intro stored procedures Declaring parameters Using in a sproc Intro to transactions Concurrency control & recovery States of transactions Desirable.
Gail Shaw XpertEase DAT 305 Topics Background Information Query Hints Plan Cache Metadata Plan Guides Plan Freezing Monitoring Plan Guide Use.
Execution Plans Detail From Zero to Hero İsmail Adar.
Why Should I Care About … The Plan Cache? Tuning When Stakeholders Won’t Say Where It Hurts.
Improve query performance with the new SQL Server 2016 query store!! Michelle Gutzait Principal Consultant at
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Just Enough Database Theory for Power Pivot / Power BI
AP CSP: Cleaning Data & Creating Summary Tables
Cleveland SQL Saturday Catch-All or Sometimes Queries
Wait-Time Based Performance Management David Waugh Confio Software
Things You Can Find in the Plan Cache.
SQL Server Monitoring Overview
Database Performance Tuning and Query Optimization
Introduction to Execution Plans
Performance Monitoring Using Extended Events, DMVs & Query Store
Weird Stuff I Saw While ... Supporting a Java Team
From 4 Minutes to 8 Seconds in an Hour (or a bit less)
The Key to the Database Engine
Now where does THAT estimate come from?
Let’s Get Started! Rick Lowe Why Should I Care About … The Plan Cache
Why Should I Care About … The Plan Cache?
Let’s Get Started! Rick Lowe Why Should I Care About … The Plan Cache
Rick Lowe Why Should I Care About … The Plan Cache
Twitter Sr. SQL Premier Field Engineer Twitter LinkedIn: sam mesel Query Store.
Query Optimization Techniques
Top Tips for Better TSQL Stored Procedures
Why Should I Care About … The Plan Cache?
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Execution Plans Demystified
Statistics: What are they and How do I use them
Steve Hood SimpleSQLServer.com
Let’s Get Started! Rick Lowe
SQL Server Query Plans Journeyman and Beyond
When I Use NOLOCK AND OTHER HINTS
Why Should I Care About … The Plan Cache?
Targeting Wait Statistics with Extended Events
Why Should I Care About … The Plan Cache?
Dynamic Management Views a practical overview!
Parameter Sniffing: the Good, the Bad, and the Ugly
Four Rules For Columnstore Query Performance
Introduction to Execution Plans
Parameter Sniffing: the Good,the Bad, and the Ugly
When I Use NOLOCK AND OTHER HINTS
Parameter Sniffing: the Good, the Bad, and the Ugly
Introduction to Access
Chapter 11 Database Performance Tuning and Query Optimization
Dynamic Management Views a practical overview!
Execution plans Eugene
Rick Lowe Why Should I Care About … The Plan Cache
Why Should I Care About … The Plan Cache?
Analyzing Performance Problems Using XEvents, DMVs & Query Store
Building a Performance Monitoring System using XEvents and DMVs
Introduction to Execution Plans
Query Optimization Techniques
Introduction to Execution Plans
Performance Tuning for SQL Developers through Execution Plans
Performance Tuning for SQL Developers through Execution Plans
Analyzing Performance Problems Using XEvents, DMVs & Query Store
Performance Tuning for SQL Developers through Execution Plans
Performance Tuning for SQL Developers through Execution Plans
Performance Tuning for SQL Developers through Execution Plans
Performance Tuning for SQL Developers through Execution Plans
Presentation transcript:

Let’s Get Started! Rick Lowe Why Should I Care About … The Plan Cache Data Flowe Solutions LLC

Rick Lowe, Data FLowe Solutions LLC rick@data-flowe.com http://www.data-flowe.com/ DataFLowe http://dataflowe.wordpress.com/ 2 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Ugly. But Trust Me, It’s Cool. SELECT TOP 10 SUBSTRING(st.text, qs.statement_start_offset / 2 + 1, CASE qs.statement_end_offset WHEN -1 THEN LEN(st.text) ELSE qs.statement_end_offset / 2 END – qs.statement_start_offset / 2) AS stmt, qs.last_execution_time, qs.execution_count, qs.creation_time, qs.total_worker_time, qs.min_worker_time, qs.max_worker_time, qs.total_elapsed_time, qs.min_elapsed_time, qs.max_elapsed_time, qs.total_logical_reads, qs.max_logical_reads, qs.min_logical_reads, qs.total_logical_writes, qs.max_logical_writes, qs.min_logical_writes, qp.query_plan FROM sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp ORDER BY qs.total_elapsed_time DESC 3 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

How We Wish Things Worked Hey Rick, server’s slow. What query are you running? SELECT Title, FirstName FROM Person.Person WHERE LastName = 'Anderson'; 4 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

5 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

6 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

7 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

How We Wish Things Worked Continued Any chance “Title” is new? We might need to tweak our indexes to support it. Yeah, title is new. I didn’t think it’d make a difference, sorry. NP, I’ll test the change today. Should be in production on Monday. 8 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

How Things Actually Work (Or Don’t Work) Hey Rick, server’s slow. What query are you running? Uh, … What? 9 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

You Often Can’t Get the Query Users won’t remember what they were doing Users don’t actually type, or even see, query Documentation isn’t detailed enough to contain queries/procedure calls If it was, it’s not up to date Developers won’t have query memorized May vary based on parameters 10 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

And Might Get Weird Replies “Can’t you just make everything faster?” “It was fine yesterday, just fix it” “Ask that fancy monitoring tool you bought” “Disk is cheap, lets just index every column. Problem solved!” “Are you trying to blame my code for your database performance?” “So I’ve been reading about Hadoop…” 11 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Tangent – Users Aren’t Stupid This just isn’t their area of expertise. We are surrounded by very complicated devices that have very simple interfaces They may think of the database like a car – complicated, but it either works or it doesn’t You wouldn’t tell your mechanic that you suspect the car broke because you set a gallon of milk on the passenger seat 12 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

But Wait, This Is Good News The bad news – not all queries are equal The good news – not all queries are equal! Tuning is only noticeable for queries that waste a lot of resources Queries that waste a lot of resources also use a lot of resources So if we could find out what queries use the most resources, we might work miracles 13 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Outline of a Solution Keep track of queries run Keep track of resources used … aggregated across all runs Query plans too Running all the time Without using extended events  14 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

My Best Friends sys.dm_exec_query_stats – query info Time created, last execution, # executions Elapsed time and worker time Read and write stats sys.dm_exec_sql_text() – query text, given the handle from exec_query_stats sys.dm_exec_query_plan() – query plan, given the handle from exec_query_stats 15 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Requirements SQL Server 2005 or later For query_hash and query_plan_hash, SQL Server 2008 or later View Server State permission 16 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Don’t Write This Down SELECT TOP 10 SUBSTRING(st.text, qs.statement_start_offset / 2 + 1, CASE qs.statement_end_offset WHEN -1 THEN LEN(st.text) ELSE qs.statement_end_offset / 2 END – qs.statement_start_offset / 2) AS stmt, qs.last_execution_time, qs.execution_count, qs.creation_time, qs.total_worker_time, qs.min_worker_time, qs.max_worker_time, qs.total_elapsed_time, qs.min_elapsed_time, qs.max_elapsed_time, qs.total_logical_reads, qs.max_logical_reads, qs.min_logical_reads, qs.total_logical_writes, qs.max_logical_writes, qs.min_logical_writes, qp.query_plan FROM sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp ORDER BY qs.total_elapsed_time DESC 17 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

sql server expensive query But I Wanted That Query Tons of examples of the preceding query can be found by searching sql server expensive query For those who download this deck, a good one is http://solutioncenter.apexsql.com/monitor-sql-server-queries-find-poor-performers-dynamic-management-views-and-functions/ 18 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Breaking It Down - SELECT SELECT SUBSTRING(…) AS stmt, qs.last_execution_time, …, qp.query_plan The SUBSTRING pulls a single statement out of a stored procedure Most of the rest are goodies from query_stats Query_plan gives us a column we can actually click on to see the estimated plan. 19 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Breaking It Down - FROM We already discussed the DMOs FROM sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp We already discussed the DMOs CROSS APPLY – Call a function for each row, and join results We need to use APPLY instead of JOIN because the functions are taking arguments from query_stats. 20 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Breaking It Down – ORDER BY ORDER BY qs.total_elapsed_time DESC Show me the queries that users had to wait the greatest amount of time for. Why elapsed time? Good estimate for how long a person had to wait for answers 21 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

I Do a Lot of Top Ten Lists By descending total elapsed time : How long has somebody been waiting for results By descending total worker time : How much CPU is this query burning By total_logical_writes or total_logical_reads : How much IO is the query doing By last_elapsed_time : How long did this query take during last night’s batch 22 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Things to Remember Not all expensive queries are wasteful Plan cache is not persistent Estimated plans – no actual counts Different settings may mean different plans This means you, ANSI_NULLS Any change in query, even an extra space, can lead to a new plan 23 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Challenges Aggregated data means estimated plans. That means we can’t compare estimated vs actual rows – hard to spot cardinality estimation issues Recompile hints limits usefulness of cache Certain operations on temporary table prevent caching. 24 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Working With Estimated Plans Can still look at expensive operators / plan shape Table scan operators (opp to clustered index scan) means heap. 1 estimated row Most expensive statement in stored procedure different than that in the estimated plan Extreme difference between maximum and minimum values in query_stats (perhaps order by descending max/min ) 25 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Tips sys.dm_exec_procedure_stats can be used to look for slow stored procedures DBCC FREEPROCCACHE can flush a single plan (instead of the entire cache) Server side cursors (API Cursors) cause issues RECOMPILE hint and DDL statements against temporary tables also causes issues 26 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Summary At the end of this session, you should have learned: Why the plan cache is important What the key DMOs are Sample code for looking for poorly performing queries. 27 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Questions? Email me at rick@data-flowe.com Or tweet me DataFLowe Thank you! 28 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center

Version for Ad Hoc Queries WITH grp AS( SELECT query_hash, query_plan_hash, MIN(sql_handle) AS sql_handle, MIN(plan_handle) AS plan_handle, MAX(last_execution_time) AS last_execution_time, MIN(creation_time) AS creation_time, SUM(execution_count) AS execution_count, SUM(total_elapsed_time) AS total_elapsed_time, MIN(min_elapsed_time) AS min_elapsed_time, MAX(max_elapsed_time) AS max_elapsed_time FROM sys.dm_exec_query_stats GROUP BY query_hash, query_plan_hash )SELECT TOP 10 st.text, grp.last_execution_time, grp.execution_count, grp.creation_time, grp.total_elapsed_time, grp.min_elapsed_time, grp.max_elapsed_time, qp.query_plan FROM grp CROSS APPLY sys.dm_exec_sql_text(grp.sql_handle) st CROSS APPLY sys.dm_exec_query_plan(grp.plan_handle) qp ORDER BY grp.total_elapsed_time DESC 29 | The 1st EVER #SQLSatLA on June 10th 2017 Microsoft Technology Center