Database Autonomy Service (DAS) provides an SQL optimization feature that automatically generates diagnostic results, optimization suggestions, and estimated performance gains. You can review these results to decide whether to adopt the suggestions. This topic describes how to use the SQL optimization feature.
Prerequisites
-
The database engine is one of the following:
-
RDS for MySQL
NoteThe Basic and Cluster editions of RDS for MySQL are not supported.
-
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 in the range of[16415631,16504348]are not supported. To view the version of your PolarDB-X 2.0 instance, see View and upgrade an instance version. -
MongoDB
-
-
The target instance is connected to DAS. For more information, see Connect an instance to DAS.
-
The access status of the target instance is Normal Access.
Limitations
-
You cannot diagnose or optimize SQL statements for tables that use the X-Engine.
-
In PolarDB-X, if you execute an SQL statement by using a prepared statement, the slow query log records the statement template (for example,
select * from test where a = ? and b = ?) and the bound parameters (for example,params: [1, 2]) separately. Because this format cannot be directly executed as a valid SQL statement, features that rely on the original statement for analysis or optimization may be limited.
Create visual SQL analysis from slow logs
This feature presents complex SQL execution flows in an intuitive, visual format. Its graphical interface clearly shows the query's execution path, node efficiency, and potential bottlenecks, helping you quickly identify and fix issues during SQL optimization, code reviews, and health checks.
SQL optimization on the Slow Log Analysis page is supported only for RDS for MySQL 5.6, 5.7, and 8.0 and PolarDB for MySQL 5.6, 5.7, and 8.0 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 left-side navigation pane, 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 standby node of the current instance. This node is mainly used for routine query analysis.
-
Event node: The business node where the SQL statement was actually executed. This node is suitable for troubleshooting and optimization.
-
-
Click Confirm Creation to create a Visual Execution Plan.
Visual execution plan
Optimize SQL from the Instance Sessions page
Self-managed MySQL, MongoDB, and RDS for PostgreSQL instances do not support SQL optimization on the Instance Sessions page.
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 left-side navigation pane, 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 that appears, view the SQL diagnosis results.
If you accept the suggestion, click Copy in the upper-right corner and paste the optimized SQL into your database client or DMS for execution. If you do not accept the suggestion, click Cancel to close the dialog box.
NoteDAS diagnoses SQL statements based on their complexity, table data volume, and database load. A diagnosis may take over 20 seconds. Afterward, the engine provides diagnostic results, optimization suggestions, and estimated performance gains for you to review and adopt.
After the diagnosis is complete, the page displays the original SQL, the execution plan (including columns such as select_type, type, and rows), the diagnostic results (including DDL statements for row-store index suggestions and index details), optimization benefits (for example, "Expected performance improvement: 0.55x" and "Low optimization gains"), and statement optimization 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 left-side navigation pane, click Request Diagnostic History. Here, you can view the instance's SQL diagnosis history, which includes details like the SQL content, diagnosis status, time, and results.