Dive deep into application performance using Query Store Microsoft 2016 12/2/2017 1:42 AM BRK3282 Dive deep into application performance using Query Store Borko Novakovic Senior Program Manager © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Session objectives and takeaways 12/2/2017 1:42 AM Session objectives and takeaways Objectives Understand what is the Query Store and why is it important for perf. analysis Learn how you can apply it to your application scenarios Learn how it works, its capabilities and limitations Takeaways Query Store is a workload data recorder for your DB Query Store is the starting point for query troubleshooting & tuning Query Store gives you full control on-premises and in the cloud Credits to: Connor Cunningham and Query Store team Visual Studio Online Bob Ward, Principal PM Architect © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Performance issues really hurt 12/2/2017 1:42 AM Performance issues really hurt Critical application is suddenly down Database upgrade ruined app performance? Changing service tier slowed down your app? App response time / throughput varies unpredictably? © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
What is hard about query troubleshooting? 12/2/2017 1:42 AM What is hard about query troubleshooting? Users are typically not prepared Performance data is not available when needed (“I should’ve been taking snapshots before…”) Result: time to detect and mitigate are long DMVs: wait to happen again What if statement is not cached ? XEvents: how to use that thing? Which events to track, how long, how to interpret results? Finally, you’ve pinpointed regression! How to force better plan? Complex Error prone Slow © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Query Store to the rescue 12/2/2017 1:42 AM Query Store to the rescue Pre-collected, persisted data of query execution Statements, plans, compile information and runtime statistics Acts as a flight data recorder Survives crashes Records critical information Records for limited time Doesn’t record everything Why is it so special? Tracks information independent of what is cached Keeps the data across, restarts, failovers, memory pressures,… Exposes its rich repository through an easy-to-use UX Allows you to force a plan with a click of a button © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Demo: React quickly on performance bottleneck in Azure SQL DB Microsoft 2016 12/2/2017 1:42 AM Demo: React quickly on performance bottleneck in Azure SQL DB © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
When to use Query Store?
Keeping good performance upon app rollouts Microsoft Ignite 2016 12/2/2017 1:42 AM Keeping good performance upon app rollouts A leading beauty company selling direct. 60 tenants (countries) using web app hosted on SQL DB. 3 different environments, need a quick way how to identify the problem Use Query Store to ensure business continuation across frequent application rollouts “Thanks to Query Store we are able to investigate database performance issues in our site very fast” Jan Vilimek, Software Architect Team Lead © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
React quickly to performance issues Microsoft Ignite 2016 12/2/2017 1:42 AM React quickly to performance issues Dominion Enterprises is a leading marketing services company serving the needs of real estate, apartments, specialty vehicles, automotive, travel and other industries. Leverages SQL Database as a back end for their SaaS product. Broadly utilize Query Store to ensure optimal and predictable performance for their tenants. “Highly recommend to everyone!” “Query Store has allowed us to pinpoint and fix problems that would be very difficult to analyze without the detailed information it provides” Pongsakorn Pluemkoson, Database Engineer Leader © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
De-risk upgrade and ensure performance stability Microsoft Ignite 2016 12/2/2017 1:42 AM De-risk upgrade and ensure performance stability The company provides e-commerce solutions and publishes widely followed e-commerce sales data. The most critical business apps already on SQL Server 2016 Utilize Query Store from day one: to detect and fix post-upgrade regressions To continuously monitor and optimize many databases “We are replacing our 3rd party monitoring tool with Query Store” Brian Carrig, DBA Manager © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
The Visual Studio case Query Store ensuring optimal performance for VSO tenants It is extremely important to keep databases busy but not over utilized… A bad day starts with CPU spikes… High CPU troubleshooting guide: look at the Query Store
The Visual Studio case First report to look at: Top consumers per CPU. Conclusion #1 – this is NOT a plan regression
The Visual Studio case Today Yesterday Checking trend on execution count Conclusion #2 – it got increased 2.7x as of yesterday Today Yesterday
The Visual Studio case Today Yesterday Checking trend on total CPU Conclusion #3 – it got increased 660x as of yesterday! Execution count 2.7x higher but total CPU is 660x higher. Regression MUST be related to individual executions What is the query? Today Yesterday
Conclusion #4: It has to be data shape issue. The Visual Studio Case Query performs DELETE on Notification table Perhaps it just deletes more data today? If Query Store could only tell me that… Today Query deletes 4x more data but this doesn’t explain 660x CPU. Conclusion #4: It has to be data shape issue. Yesterday
Bad day deals with lots of invalid subscriptions. Microsoft 2016 12/2/2017 1:42 AM The Visual Studio case Table size didn’t grow either Plan analysis reveals app logic: deleting notifications for invalid subscriptions Final conclusion: Bad day deals with lots of invalid subscriptions. (app logic is inefficient) © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
The Visual Studio Case - recap Focusing investigation = saving time & energy Query Store eliminates the need for “breadth first” search It clearly says which investigation avenues to avoid early on Simple and intuitive process Resource consumers / regressions → baseline comparison (good vs. bad day) → query / plan details No need to switch between the tools Query Store is rich information store – additional sources are rarely needed Tuning effects immediately visible Verify tuning actions immediately thanks to zero-latency monitoring
Upgrade to SQL 2016 Upgrades Are Far Easier In SQL Server 2016 You Control When Optimizer Changes Are Enabled You Fix Regressions Quickly With Query Store Use SSMS and fix issues in minutes instead of hours/days Upgrade to SQL Server 2016 (stay at prior DB Compatibility Level) Enable Query Store, Capture Plans Set Compatiblity Level to 130 Use Query Store to Force Old Plans Quickly For Any Regressions
Scenario summary Optimization Protection Query Store Scenarios @ MSDN Regressed queries Excessive resource consumption Database upgrade Change verification Optimization Improve ad-hoc workloads Identify execution patterns Analyze execution variability Query Store Scenarios @ MSDN
Query Store Internals
Runtime Stats Interval Information in Query Store internal tables views Query Text 1 : N Query 1 : N Plan 1 : N Runtime Stats sys. 1 - n 1 - n Compile stats: query_store_query_text query_context_settings query_store_query query_store_plan Context Settings 1 : N Runtime Stats Interval One Row Per Query Text Per Plan Affecting Option Per Object Example: ANSI NULLS on/off, query inside/outside SP Runtime stats: query_store_runtime_stats_interval query_store_runtime_stats One Row Per Plan (for each query) One Row Per Plan Per Time Interval (example: 5 min)
Query Store and Statistics 12/2/2017 Query Store and Statistics Compilation Timestamp – first and last Count Duration – total, avg, last Bind (CPU and Duration) – total, avg, last Optimize (CPU and Duration) – total, avg, last Memory – total, avg, and max Execution Timestamp – first and last Execution Type (completed, client aborted, server aborted) Count Duration – min, max, last, total, avg, stdev CPU – min, max, last, total, avg, stdev Logical I/O (read and writes) – min, max, last, total, avg, stdev Physical Reads – min, max, last, total, avg, stdev CLR – min, max, last, total, avg, stdev DOP – min, max, last, total, avg, stdev Query Memory Used – min, max, last, total, avg, stdev Rowcount – min, max, last, total, avg, stdev All of these at query level. Some at plan level Aggregated at interval level (default 60 mins) Plan Store Runtime Stats Store © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, 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.
Compiling and the Plan Store 12/2/2017 1:42 AM query_text Statement Not in Store If context does not exist Statement In Store Update compile stats context_settings If context does not exist Statement in Store with New Context query Update compile stats query_plan Statement in Store with New Plan © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Execution and Runtime Stats 12/2/2017 1:42 AM Stats Interval Timeframe for aggregation of stats for plan Flush Interval How often we persist to disk runtime_stats insert exec stats Plan in store executed for new interval runtime_stats tables “start” a new stats interval = Default 60 mins runtime_stats_interval update exec stats Async persist to tables on flush interval = Default 15 mins Plan in store executed in same interval update last execution time plan store tables plan store update last execution time © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
In Out What is a Tracked Query? Any T-SQL DML statement 12/2/2017 What is a Tracked Query? WITH RECOMPILE Ad-hoc even with ‘optimize’ set Internal queries (auto stats) Any T-SQL DML statement Independent of what is cached Each statement in an object is a query Statement text appears in parameterized form Stored Procedure, Function, Trigger Including cursors and SET assignment In SELECT UPDATE INSERT DELETE Out DDL BULK INSERT “Commands” CREATE, ALTER, DROP, … DBCC, KILL, BACKUP, … Characters inside the statement matter © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, 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.
Query Store Maintenance 12/2/2017 1:42 AM Query Store Maintenance sys.database_query_store_options sys.database_query_store_options readonly_reason readonly_reason Enabling, Clearing, and State ON = Enable; OFF = Disabled (default, existing state and data kept); CLEAR = TRUNCATE tables READ_WRITE = Default when ON; READ_ONLY = intentional or problem (desired != actual) Size, limits, and retention Default max size = 100Mb (limited by overall database size). If you hit max, state = READ_ONLY Default max plans per query = 200 (this is silent but its well…200!) Default days queries kept in store = 30 days Capture and cleanup efficiently Query capture mode of AUTO (Default is ALL) = capture relevant queries based on execution count and resource consumption. NONE = Stop capture new queries but track and persist stats for existing. Sized based cleanup of AUTO (Default is AUTO) = Remove oldest queries and least expensive when 90% of max (until 80% reached). Overrides time retention (days kept in store) Maintenance at a deeper level sp_query_store_remove_plan = delete specific plan and associated runtime stats sp_query_store_remove_query = delete query, associated query text, plans and runtime stats sp_query_store_reset_exec_stats = Delete runtime stats for specific plan Query Store Best Practices well documented Query Store Best Practices well documented © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Query Store and Azure SQL Database 12/2/2017 Query Store and Azure SQL Database Capability wise identical to on-premise Catalog Views all available Available for all service tiers SSMS Reports Supported Differences Query capture = AUTO (by default) Process of automatic activation is in progress (“on-by-default” is coming soon!) We do limit resources and may revert to read-only mode memory overcommitted Powers SQL Database Advisor Query Performance Insight Index Advisor More to Come © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, 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.
Demo: Analyze workload using the Query Store Microsoft 2016 12/2/2017 1:42 AM Demo: Analyze workload using the Query Store © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Takeaways …a workload data recorder for your databases …the starting point for performance troubleshooting …gives you full control wherever you are (on-premises, in the cloud) 1 QUERY STORE 2 3
Call to action Try out Query Store and SQL DB Advisor now! Check out online materials to learn more details Query Store: A flight data recorder for your database Monitoring Performance By Using the Query Store Azure SQL Database Query Performance Insight Reach out should you have any questions Borko Novakovic ( bonova@microsoft.com )
Free IT Pro resources To advance your career in cloud technology Microsoft Ignite 2016 12/2/2017 1:42 AM Free IT Pro resources To advance your career in cloud technology Plan your career path Microsoft IT Pro Career Center www.microsoft.com/itprocareercenter Cloud role mapping Expert advice on skills needed Self-paced curriculum by cloud role $300 Azure credits and extended trials Pluralsight 3 month subscription (10 courses) Phone support incident Weekly short videos and insights from Microsoft’s leaders and engineers Connect with community of peers and Microsoft experts Get started with Azure Microsoft IT Pro Cloud Essentials www.microsoft.com/itprocloudessentials Demos and how-to videos Microsoft Mechanics www.microsoft.com/mechanics Connect with peers and experts Microsoft Tech Community https://techcommunity.microsoft.com © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Please evaluate this session 12/2/2017 1:42 AM Please evaluate this session Your feedback is important to us! From your PC or Tablet visit MyIgnite at http://myignite.microsoft.com From your phone download and use the Ignite Mobile App by scanning the QR code above or visiting https://aka.ms/ignite.mobileapp © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.