Database Autonomy Service (DAS) provides an SQL optimization feature that automatically generates diagnosis results, optimization suggestions, and expected performance gains. Use these results to decide whether to accept the suggestions. This topic describes how to use the SQL optimization feature.
Prerequisites
The database engine is:
ApsaraDB RDS for MySQL
MyBase MySQL
PolarDB for MySQL
NoteSingle-node clusters (formerly standalone instances) of PolarDB for MySQL are not supported.
PolarDB-X 2.0
NotePolarDB-X 2.0 instances with major version
5.4.13and a minor version within the range of[16415631,16504348]are not supported. To view the version of a PolarDB-X 2.0 instance, see View and upgrade an instance version.MongoDB
The destination instance is connected to DAS. For more information, see Connect a database instance to DAS.
The accessed state of the destination instance is Normal Access.
Feature limits
Diagnosis and optimization of SQL statements that use X-Engine tables are not supported.
In PolarDB-X, if you execute an SQL statement using Prepared Statements, the slow query log records the statement template, such as
select * from test where a = ? and b = ?, and the bound parameters, such asparams: [1, 2], separately. Because this format cannot be directly executed as a valid SQL statement, analysis or optimization capabilities that rely on the original statement may be limited.
Create a visual SQL analysis on the Slow Log Analysis page
This feature renders complex SQL execution flows in a graphical way. The graphical interface provides a clear view of the query execution path, the efficiency of each node, and potential performance bottlenecks. This helps you quickly locate and optimize issues in scenarios such as slow SQL statement optimization, pre-release code review, and self-checks.
Currently, SQL optimization on the Slow Log Analysis page is supported only for instances of ApsaraDB RDS for MySQL 5.6, 5.7, and 8.0 and PolarDB for MySQL 5.6, 5.7, and 8.0.
Log on to the DAS console.
In the navigation pane on the left, click .
Find the target instance and click the instance ID to open the instance details page.
In the navigation pane on the left, click Request Analysis > Slow Logs.
On the Slow Log Analysis page:
On the Slow Log Statistics tab, find the target SQL template and click Optimize in the Actions column.
On the Slow Log Details tab, find the target SQL statement and click Optimize in the Actions column.
In the dialog box that appears, click Create Plan.
Select an execution node type.
Standby node (default): The secondary node of the current instance. It is mainly used for regular query analysis.
Event node: The business node where the SQL statement was actually executed. It is suitable for troubleshooting and optimization.
Click Confirm Create to create a graphical execution plan.
Graphical execution plan details
Perform SQL optimization on the Instance Session page
Currently, SQL optimization on the Instance Session page is not supported for self-managed MySQL, MongoDB, and ApsaraDB RDS for PostgreSQL database instances.
Log on to the DAS console.
In the navigation pane on the left, click .
Find the target instance and click the instance ID to open the instance details page.
In the navigation pane on the left, click Instance Sessions.
In the Instance Sessions area, select the session that you want to optimize and click Optimize.
In the SQL Diagnostic Optimization dialog box, view the SQL diagnosis results.
If you accept the suggestion, click Copy in the upper-right corner of the page and paste the optimized SQL statement into a database client or DMS to execute it. If you do not accept the suggestion, click Cancel to end the diagnosis.
NoteDAS diagnoses the SQL statement based on its complexity, the data volume of the corresponding table, and the database payload. The diagnosis may take more than 20 seconds. After the diagnosis is complete, the SQL diagnostics engine provides the diagnosis results, optimization suggestions, and expected performance gains. You can use the diagnosis results to decide whether to accept the suggestions.
View SQL diagnosis history
Log on to the DAS console.
In the navigation pane on the left, click .
Find the target instance and click the instance ID to open the instance details page.
In the navigation pane on the left, click Request Diagnostic History. View the SQL diagnosis history of the current instance, such as the SQL content, diagnosis status, diagnosis time, and diagnosis results.