Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

Understanding automatic tuning in SQL Server 2017

$
0
0

Monitoring databasesfor optimal query performance,creating and maintaining required indexes,and dropping rarely-used, unused or expensive indexesis a common database administration task.As administrators, we’ve all wished, at some point, that these tasks were simpler to handle.

SQL Server 2017 can now assist database administrators in performing some of these routine operationsby identifying problematic query execution plansand fixing problems with the SQL plan performance.Automatic tuning startswith continuously monitoring the databaseand learning about the workload that it serves. Automatic tuning is based on Artificial Intelligence, which makes managing the performance of the system flexible.

SQL Server can use different strategies (or SQL plans) to execute a T-SQL query. SQL Server analyzes possible plans that can be used to execute a T-SQL query and chooses the optimal plan. The plans for most of the successfully executed queries are cached and reused when the same query is executed. The plan is retained in the cache until the SQL Database Engine decides to recompile the plan and find a new one (e.g. when statistics change, index is added or removed, etc.).

The most traditional way to troubleshoot performance issues is by measuring the wait statistics. These metrics are further classified into various categories . When SQL Server is executing and waiting for the resources, the corresponding entry is made in the system objects. We can query the system objects using DMV (Dynamic Management View) sys.dm_os_wait_stats. The nature of the DMV output is cumulative; it provides an aggregated value. It keeps adding the values at frequent intervals of time. The workaround for keeping track of values during each check or get a trend is by creating a repository and pull the data from the DMV and store it at frequent intervals of time for later querying. This gives the data for specific time period for performance analysis and troubleshooting the issues. This process is a little cumbersome; most administrators do not automate the process until they deep-dive into a particularly problematic situation. However, we can track the wait stats in the Query Store with a few simple steps. There are several options available to configure the Query Store. The wait stats are further grouped into wait categories . (There are over 900+ wait types available in SQL Server.) We can query the DMV sys.query_store_wait_stats to get the wait information. The wait categories are stored in the query store along with the date and the timestamp.

The Query Store feature was introduced in SQL Server 2016. The SQL Server Query Store allows storing the history of queries, multiple plans, run time statistics, etc., and provides an insight into the query plan and performance of the database. This feature allows us to find regressed queries more easily. If a plan is not optimal and if there were plans that performed better, then we can unforce the plan, and use the more-optimal plan. This can be done using the stored procedures, sp_query_store_force_plan and sp_query_store_unforce_plan .

Automatic Tuning = Automatic Plan Creation + Automatic Index Management

Automatic plan correction is a new automatic tuning feature in SQL Server 2017 that identifies SQL query plans that are worse than the previous one, and fixes performance issues by applying the previous good plan instead of the regressed one. Doing so allows the database engineto identify opportunitieswhere alterations to the execution plansmight increase the performance of the system. By default, the Automatic Plan Correction feature is disabled.

Also, automatic tuning can actually applyany suggested alterations according to its predictions.Finally―and this is the really smart part―automatic tuning continues to monitorthe performance of the system after a change is made,to ensure that the expected results are achieved.If the change is found to not actually increase performance(or worse, found to negatively impact performance) it reverts such tuning recommendations.

Automatic Plan Correction(APC) is an extension of sp_query_sotre_force_plan. Forcing a plan is an effort to identify the optimal plan. However, it’s a manual effort in the Query Store to manage the plans (forcing and un-forcing actions are to be taken) for better performance. APC on the other hand is automatic in nature and is available in SQL Server 2017 as well as in Azure SQL Database. It uses the query store telemetry data to select and recommend the optimal plan. This is the reason the Query Store is a prerequisite.

Data collection happens in theQuery Store The most optimal plan is selected using dm_db_tuning_recommendations Automatic Plan Correction takes place based on the data The last good plan is reverted to Enable the Query Store using SSMS Use Object Explorer to browse the database properties Select the Query Store option

In the Operation Mode (Requested) , select Read Write


Understanding automatic tuning in SQL Server 2017
Enable the Query Store using T-SQL

Use the ALTER DATABASE to enable the query store.

ALTER DATABASE CURRENT SET QUERY_STORE = ON; Demonstration

We’ll use the WideWorldImporters database for the demonstration.

Restore the WWI database from the backup. You can download the database here

Clone the data to SalesOrderLines table from sales.OrderLines and Sales.Orders to SalesOrders table

SELECT * INTO SalesOrderLines from sales.OrderLines SELECT * INTO SalesOrders from sales.Orders

Enable and set the values to configure the Query Store

ALTER DATABASE CURRENT SET QUERY_STORE = ON; GO ALTER DATABASE CURRENT SET QUERY_STORE ( OPERATION_MODE = READ_WRITE, DATA_FLUSH_INTERVAL_SECONDS = 600, MAX_STORAGE_SIZE_MB = 500, INTERVAL_LENGTH_MINUTES = 30 ); GO

Clear the Procedure cache using the following T-SQL

ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;

Clear the Query Store using the following T-SQL

ALTER DATABASE CURRENT SET QUERY_STORE CLEAR ALL;

Enable the Automatic Tuning option on the database

ALTER DATABASE CURRENT SET AUTOMATIC_TUNING (FORCE_LAST_GOOD_PLAN = ON ); GO

Verify the database settings and options

SELECT * FROM sys.database_automatic_tuning_options
Understanding automatic tuning in SQL Server 2017

Every parameter and every small change in a query impacts it in some way or the other. Conversely, the performance of a query does not change if there’s no change in the configuration or the underlying data. When a query is executed, SQL Server Engine chooses what it thinks is the best plan, when executing. If a plan was compiled and cached, it can be reused from cache. In some situations, even if a certain plan was not the most optimal, but performed better than the current plan on similar parameters, the SQL Server Engine would use the old plan. This is called a plan regression.

Ide

Viewing all articles
Browse latest Browse all 3160

Trending Articles