sql server query store e automatic tuning - delphi day · 2019-09-20 · query store options...
TRANSCRIPT
![Page 1: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/1.jpg)
SQL Server Query Store e
Automatic Tuning
![Page 2: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/2.jpg)
Sergio GovoniCentro Software
@segovoni
![Page 3: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/3.jpg)
AGENDA
➔SQL Server Query Store➔ Query plan choice change can cause performance
issues➔ What does the Query Store do for you?
➔Automatic Tuning in SQL Server 2017 and Azure SQL Database
![Page 4: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/4.jpg)
Test Environment for you
➔Download and install the last version of SQL Server Management Studio➔ https://bit.ly/2K7Jzgu
➔Connect to Azure SQL Database➔ Server: delphi-day-2019.database.windows.net➔ User: UserDDay2019➔ Password: h5AZP5M7t3MZ3YSfGZQ9CPNtU➔ Database: AdventureWorks2017, QueryStore
➔Enjoy ☺
![Page 5: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/5.jpg)
SQL Server Query Store
![Page 6: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/6.jpg)
Plan changes can cause performance issues
• Database is not working• How to fix it ASAP?
Web site is down
• Impossible to predict the root cause
Temporary perf. issues
• Performance regression caused by the new version
SQL Server upgrade
![Page 7: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/7.jpg)
➔Query Optimizer generates and evaluates many plans for the same query
➔Data changes every hour, so the QO might select a different plan
➔Generally, boundary plans have the same performance
Why do plan changes happen?
![Page 8: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/8.jpg)
➔Problems you face for slow running queries➔ Time of the day and compiled parameters➔ Plan cache may not be suitable for
troubleshooting➔You have to find out why it is show➔When you have the issue on you hands
➔ Can you modify the query text?➔ Do you know the SP to create the Plan Guide?
Fixing query plan regression is hard
![Page 9: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/9.jpg)
➔ It stores all the plan choices and related performance metrics for each query
➔ It identifies queries that have become slower recently
➔ It allows you to force an execution plan easily➔ It makes sure your changes work across
server restart, upgrades, failover and query recompiles
What does the Query Store do for you?
![Page 10: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/10.jpg)
How the Query Store captures data
![Page 11: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/11.jpg)
➔DMVs➔Query Store reports➔When memory
pressure occurs, some data In-Memory will be flushed to the disk
How the Query Store exposes data
sys.query_store_runtime_stats
On-Disk Data
In-MemoryData
![Page 12: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/12.jpg)
➔Query text starts from the first character of the first token and it ends at the last character of the last token➔ Comments and spaces, before or after, don’t
change anything➔ Comments and spaces inside count➔ Each unique query text is stored just once in the
DMV sys.query_store_query_text
How the Query Store interprets the query text
![Page 13: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/13.jpg)
➔A console application executes this query in a loop on a database where Query Store is enabled
➔ Parameters are generated by a randomized function, values are between 0 and 100
➔ Plan cache is cleaned when parameters values are less than 2
Demo environment
![Page 14: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/14.jpg)
Demo
![Page 15: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/15.jpg)
Query Store options
![Page 16: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/16.jpg)
Query Store Options
➔Because collecting data has always a cost, you can tune the Query Store with these options➔ INTERVAL_LENGTH_MINUTES➔ MAX_STORAGE_SIZE_MB➔ MAX_PLANS_PER_QUERY (200 by default)➔ QUERY_CAPTURE_MODE (ALL, AUTO, NONE)➔ DATA_FLUSH_INTERVAL_SECONDS
![Page 17: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/17.jpg)
Server Upgrades
![Page 18: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/18.jpg)
➔ Are you worried about SQL Server upgrades? This is the way out➔ Restore a copy of the production DB on a test server➔ Run a significative workload➔ Look at the Query Store for performance
regressions
➔ Plan forced works across➔ Recompilation➔ Server reboots and failover
Server Upgrades
![Page 19: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/19.jpg)
Automatic Tuning
![Page 20: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/20.jpg)
Automatic Tuning
➔Automatic tuning is a database feature that provides insight into potential query performance problems, recommend solutions, and automatically fix identified problems
➔There are two automatic tuning features that are available➔ Automatic plan correction➔ Automatic index management
![Page 21: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/21.jpg)
Automatic plan correction
➔ Identifies execution plans choice regression
➔Automatically fix the issue by forcing the last known good plan
➔Available on SQL Server 2017 Enterprise Edition and Azure SQL Database
![Page 22: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/22.jpg)
Automatic Plan Correction
➔Automatic Plan Correction takes plan forcing one step further
➔ It is a plan forcing, but it is done automatically for you by SQL Server
➔ It uses Query Store, so Query Store must be enabled in order to use it
![Page 23: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/23.jpg)
Automatic Plan Correction
Compare query performance
Regression in performance?
If regressed, force last good
plan
Measure Performance
If regression: un-force
![Page 24: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/24.jpg)
Automatic index management
➔ It can help you optimize your indexes➔ Identifies indexes that could improve
performance of your queries that read data from the tables
➔ Identifies the redundant indexes or indexes that were not used in longer period of time that could be removed
➔Available on Azure SQL Database
![Page 25: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/25.jpg)
Demo
![Page 26: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/26.jpg)
Query Store in SQL Server 2019 (CTP 3.0)
![Page 27: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/27.jpg)
Query Store in SQL Server 2019 (CTP 3.0)
➔Query Store has a new option for QUERY_CAPTURE_MODE
➔The “CUSTOM” option allows you to define what queries are captured based on➔ EXECUTION_COUNT➔ TOTAL_COMPILE_CPU_TIME_MS➔ TOTAL_EXECUTION_CPU_TIME_MS
![Page 28: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/28.jpg)
Defaults changed in SQL Server 2019
➔MAX_STORAGE_SIZE_MB➔ from 100MB to 1000MB
➔QUERY_STORE_CAPTURE_MODE➔ from ALL to AUTO
➔ It doesn’t capture insignificant queries based on some thresholds that are not documented
➔ The default value in Azure SQL Database is already AUTO
![Page 29: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/29.jpg)
Resources
➔A good starting point➔ https://bit.ly/2VSwjlf
➔UGISS channel on Vimeo➔ SQL Server 2016 Query Store un nuovo modo di
ottimizzare le query https://vimeo.com/149648363
➔ Troubleshooting delle prestazioni delle query con SQL Server 2016 Query Store https://vimeo.com/200794871
![Page 30: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/30.jpg)
Q&A
![Page 31: SQL Server Query Store e Automatic Tuning - Delphi Day · 2019-09-20 · Query Store Options Because collecting data has always a cost, you can tune the Query Store with these options](https://reader034.vdocuments.site/reader034/viewer/2022050215/5f61253ee0641678722183cd/html5/thumbnails/31.jpg)