Twitter handle: @sammesel Sr. SQL Premier Field Engineer sam.mesel@microsoft.com Twitter handle: @sammesel LinkedIn: sam mesel Query Store.

Slides:



Advertisements
Similar presentations
Yukon – What is New Rajesh Gala. Yukon – What is new.NET Framework Programming Data Types Exception Handling Batches Databases Database Engine Administration.
Advertisements

Module 13: Performance Tuning. Overview Performance tuning methodologies Instance level Database level Application level Overview of tools and techniques.
Module 17 Tracing Access to SQL Server 2008 R2. Module Overview Capturing Activity using SQL Server Profiler Improving Performance with the Database Engine.
The Essentials: DMV’s and T-SQL for the DBA Rocky Mountain Tech Tri-Fecta.
Connect with life Praveen Srvatsa Director | AsthraSoft Consulting Microsoft Regional Director, Bangalore Microsoft MVP, ASP.NET.
SQL Server 2005 SP2 Israeli SQL Server User Group March 2005 Ami Levin
Module 18 Monitoring SQL Server 2008 R2. Module Overview Monitoring Activity Capturing and Managing Performance Data Analyzing Collected Performance Data.
Troubleshooting SQL Server Enterprise Geodatabase Performance Issues
How a little code can help with support.. Chris Barba – Developer at Cimarex Energy Blog:
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Danette Dineen Riviello Magellan Health March 17,
DMV Performance Monitoring & Tuning Presented by Franklin Yamamoto.
Learningcomputer.com SQL Server 2008 – Profiling and Monitoring Tools.
SQLRX – SQL Server Administration – Tips From the Trenches SQL Server Administration – Tips From the Trenches Troubleshooting Reports of Sudden Slowdowns.
Module 8: Implementing Stored Procedures. Overview Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans.
Troubleshooting SQL Server Performance: Tips &Tools Amit Khandelwal.
Matt Lavery & Joanna Podgoetsky Being a DBA is cool again with SQL 2016 DAT335 A.
Gail Shaw XpertEase DAT 305 Topics Background Information Query Hints Plan Cache Metadata Plan Guides Plan Freezing Monitoring Plan Guide Use.
Why Should I Care About … The Plan Cache? Tuning When Stakeholders Won’t Say Where It Hurts.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
SQL Advanced Monitoring Using DMV, Extended Events and Service Broker Javier Villegas – DBA | MCP | MCTS.
Improve query performance with the new SQL Server 2016 query store!! Michelle Gutzait Principal Consultant at
This document is provided for informational purposes only and Microsoft makes no warranties, either express or implied, in this document. Information.
Session Name Pelin ATICI SQL Premier Field Engineer.
MANAGEMENT DATA WAREHOUSE AND DATA COLLECTOR Ian Lanham.
Dive deep into application performance using Query Store
Welcome To the 2016 Query Store
What Is The SSIS Catalog and Why Do I Care?
You Inherited a Database Now What?
Microsoft Query Store and Live Query Statistics overview:
Cleveland SQL Saturday Catch-All or Sometimes Queries
Troubleshooting SQL Server high CPU usage
Dynamic SQL Writing Efficient Queries on the Fly
Inside the SQL Server Query Store
SQL Server Data Collector From Every Angle
Building a Performance Monitoring System using XEvents and DMVs
Query Performance Tuning: Start to Finish
Things You Can Find in the Plan Cache.
Art of War With Bad Code Andrew J. Kelly
Query Store: Making Sure Your Database Queries Run Optimally
Query Store What’s it all About? Andrew J. Kelly
SQL Server Monitoring Overview
Dynamic SQL Writing Efficient Queries on the Fly
Reading Execution Plans Successfully
Root Cause Analysis with DMVs
Peeking into the Plan Cache with SQL Server 2008
Performance Monitoring Using Extended Events, DMVs & Query Store
Building a Performance Monitoring System using XEvents and DMVs
Introducing the SQL Server 2016 Query Store
SQL Server 2016 Query Data Store
Plan cache performance tuning
Reading Execution Plans Successfully
Targeting Wait Statistics with Extended Events
Dynamic Management Views a practical overview!
Parameter Sniffing: the Good, the Bad, and the Ugly
You Inherited a Database Now What?
Introduction to Execution Plans
Go, go Query Store! Gail Shaw.
Parameter Sniffing: the Good, the Bad, and the Ugly
Dynamic Management Views a practical overview!
Analyzing Performance Problems Using XEvents, DMVs & Query Store
Building a Performance Monitoring System using XEvents and DMVs
Introduction to Execution Plans
From adaptive to intelligent: query processing in SQL Server 2019
Query Optimization Techniques
Reading execution plans successfully
Introduction to Execution Plans
Using wait stats to determine why my server is slow
Analyzing Performance Problems Using XEvents, DMVs & Query Store
Will Query Store fix ‘ALL’ my Performance Issues? Surbhi Pokharna
Presentation transcript:

Twitter handle: @sammesel Sr. SQL Premier Field Engineer sam.mesel@microsoft.com Twitter handle: @sammesel LinkedIn: sam mesel Query Store

DBA challenges

Situations Available tools: CPU spikes Memory Queries taking longer to run Disk contention DMVs Profiler Trace Extended Events PSSDIAG / SQLNexus 3rd Party tools Anyone still using Profiler Trace ?

Using DMVs to investigate /* The query below returns the Top Cumulative CPU within last 1hour */ SELECT last_execution_time, total_worker_time AS [Total CPU Time], execution_count, total_worker_time / execution_count AS [Avg CPU Time], text, qp.query_plan FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text (qs.sql_handle) AS st CROSS APPLY sys.dm_exec_query_plan (qs.plan_handle) AS qp WHERE DATEDIFF(hour, last_execution_time, getdate()) < 1 ORDER BY total_worker_time DESC;

Other DBA activities Find queries that are executed the most ? Nothing else to do? Other DBA activities Find queries that are executed the most ? Optimizations on queries that run 1,000s of times will lead to big gains

Compile and Optimize Query Query Execution Check for Recompile Compile and Optimize Query Fetch Plan from Cache Plan Cache lookup Execute Query Finish Execution Query Execution Found Not found Not needed Recompile Convert TSQL statement into a handle. Uses this handle to search the cache select * from table where col_a = 1  #abc SELECT * FROM Table WHERE col_a = 1  #bcd 2) Normalized TSQL get a QUERY-HASH select * from table where col_a = 1  #abc  #G123 SELECT * FROM Table WHERE col_a = 1  #bcd  #G123

more DBA activities Find queries that have multiple Execution Plans? Nothing else to do? more DBA activities Find queries that have multiple Execution Plans? Use “Query Fingerprinting” and “Plan Fingerprinting” https://blogs.msdn.microsoft.com/bartd/2008/09/03/query-fingerprints-and-plan-fingerprints-the-best-sql-2008-feature-that-youve-never-heard-of/

More advanced DBAs Find queries that regressed over time ? Still bored? Find queries that regressed over time ? This requires more elaborated solutions

Demo using basic DMVs to investigate Query Cache Contents Start [initialdemo.cmd] Open queries: [QUery Data Store - 000 Using Basic DMVs to investigate Query Cache.sql]

Possible Complications Solutions: Query is no longer in cache ? Can the server be so busy that it flushes out queries from query cache ? Would it be the case my server has too little memory ? Was SQL Server restarted ? Has the DBA flushed the cache? Frequent DMV collections

Possible Complications (2) Solutions With new plan we can get better or worse performance Why does execution plan change ? SQL Server Query Optimizer performs calculations to decide amongst several Execution plans, which one will be “good enough” for your query. Once a plan is chosen, it will be placed in the query cache As data changes, the query optimizer might select a different plan As the schema changes, there will be different options for the query optimizer to consider Plan Guide Limited to 8KB of query plan text (XML) Plan Freezing

Demo Query with Multiple Plans USE either one: Query Data Store - 008 - Compatibility Level 110 130 - SP. SQL Query Data Store - 009 - Compatibility Level 110 130 – DynamicSQL.SQL

Plan Freezing -- first, get the plan that is cached: SELECT s.plan_handle, s.statement_start_offset, t.text FROM sys.dm_exec_query_stats AS s CROSS APPLY sys.dm_exec_sql_text (s.sql_handle) AS t; -- now use the plan handle, and statement start offset from the line that begins -- with query text pattern -- for the next step this step freezes the plan in cache: EXECUTE sp_create_plan_guide_from_handle @name = N'getAddresses‘ -- change to actual plan handle from cache ,@plan_handle = 0x050007009AB3472DC082D1ECA10100000100000000… -- change this to the actual offset from cache ,@statement_start_offset = 142

Plan Guide CREATE PROCEDURE Sales.GetSalesOrderByCountry @Country NVARCHAR (60) AS BEGIN SELECT * FROM Sales.SalesOrderHeader AS h, Sales.Customer AS c, Sales.SalesTerritory AS t WHERE h.CustomerID = c.CustomerID AND c.TerritoryID = t.TerritoryID AND CountryRegionCode = @Country; END GO -- Run the stored proc and examine the query plan EXECUTE Sales.GetSalesOrderByCountry 'UK'; -- Next, create the plan guide and then rerun the test EXECUTE sp_create_plan_guide @name = N'PlanGuide_OptimizeForUS', @stmt = N' SELECT * FROM Sales.SalesOrderHeader AS h, Sales.Customer AS c, Sales.SalesTerritory AS t WHERE h.CustomerID = c.CustomerID AND c.TerritoryID = t.TerritoryID AND CountryRegionCode = @Country;' , @type = N'OBJECT' , @module_or_batch = N'Sales.GetSalesOrderByCountry' , @params = NULL , @hints = N'OPTION (OPTIMIZE FOR (@Country = N''US''))'; GO -- Run the stored proc and examine the query plan -- Did the plan change? Did it use the plan guide? EXECUTE Sales.GetSalesOrderByCountry 'UK';

“A Bad Plan is not the one which failed, but the one which succeeded at the Greatest Cost.” ~Anonymous DBA

What can the DBA do? Welcome to QUERY STORE Content 2 Creates custom solution storing Query and Plan history Baseline performance for each plan over time Identify queries that became slower Optimize those queries OR alternatively force a good known plan Test solution: Instance restarts query recompiles upgrades Welcome to QUERY STORE Content 2

Query Store Defined

What is Query Store? A repository created on a database basis that collects Queries and corresponding Execution plans Runtime statistics Ready to run visualizations on SSMS Regressed Queries Analysis by Resource Consumption Tracked Queries

What does it do for the DBA? Quickly find query plan with performance regression Easily fix plans by forcing previous plans Provides statistics of Queries executed on last X hours

Using Query Store Resource provides DBA with insight on queries and resource consumption

Using Query Store Insight on queries with Regressed performance

Using Query Store Insight on Resource Consumption

Using Query Store Insight on Top Resource Consumers

Using Query Store Insight on Tracked Queries

Query Store Internals

Query Execution Data is persisted to disk asynchronously in the background Query Execution Check for Recompile Compile and Optimize Query Fetch Plan from Cache Plan Cache lookup Execute Query Finish Execution Query Execution Found Not found Not needed Recompile Get forced plan Query Store Send text and plan Send execution stats

Using Query Store Collects query texts and all relevant properties Stores all plan choices and performance metrics Works across restarts / upgrades / recompiles Compile MSG Execute MSG Query Store Durability latency controlled by DB option DATA_FLUSH_INTERNAL_SECONDS Async Write-Back Compile Execute SQL Plan Store Runtime Stats Query Store Schema

Runtime Stats Interval Query Store schema internal tables exposed views Query Text Query Plan Runtime Stats sys. 1 - n 1 - n Compile stats: query_store_query_text query_context_settings query_store_query query_store_plan Context Settings Runtime Stats Interval Runtime stats: query_store_runtime_stats_interval query_store_runtime_stats One Row Per Query Text Per Plan Affecting Option (example: ANSI NULLS on/off) One Row Per Plan (for each query) One Row Per Plan Per Time Interval (example: 5 min)

Query Store details Plans and execution data are stored in the user database Query store data persists reboots, upgrades, restores etc. Plans and statistics are tracked at the database level Query Store is configurable Settings such as MAX_SIZE_MB, QUERY_CAPTURE_MODE, CLEANUP_POLICY allow you to decide how much data you want to store for how long Can be configured either via the SSMS GUI or T-SQL scripts Query Store can be viewed and managed via scripting or SSMS

What does Query Store track? Query Texts start at the first character of the first token of the statement; end at last character of last token Comments before/after do not count Spaces and comments inside *do* count Context_settings contains one row per unique combination of plan-affecting settings Different SET options cause multiple “queries” in the Query Store Plan caching/recompilation behavior unaffected

What gets captured ? Query Texts Query Plans Runtime Statistics (per unit of time, default 1 hour) Count of executions of each captured plan For each metric: average, last, min, max, stddev Duration CPU_Time CLR_Time Logical_IO_Reads Logical_IO_Writes Physical_IO_Reads DOP Query_max_use_memory RowCount

Demo Using Query Store

Short-term / Tactical Long-Term / Strategic Scenarios Find and fix query plan regressions Identify top resource consumers Reduce risks with server upgrade Deep analysis of workload patterns/perf Short-term / Tactical Long-Term / Strategic