All Products
Search
Document Center

Database Autonomy Service:SQL optimization

Last Updated:Mar 28, 2026

Database Autonomy Service (DAS) automatically analyzes SQL statements and returns optimization suggestions with expected performance gains — so you can decide whether to apply them. Use this feature for slow SQL troubleshooting, pre-release code reviews, and routine self-checks.

Prerequisites

Before you begin, make sure that:

  • The database engine is one of the following:

    • RDS for MySQL — Basic and Cluster editions are not supported

    • MyBase MySQL

    • PolarDB for MySQL — single-node clusters (formerly standalone instances) are not supported

    • PolarDB-X 2.0 — instances with major version 5.4.13 and minor version in the range [16415631, 16504348] are not supported. To check your version, see View and upgrade an instance version

    • MongoDB

  • The instance is connected to DAS. See Introduction to instance connection

  • The instance's accessed state is Normal Access

Limitations

  • SQL statements that use X-Engine tables cannot be diagnosed or optimized.

  • In PolarDB-X, when a SQL statement runs 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 is not a directly executable SQL statement, features that rely on the original statement for analysis or optimization may be limited.

Optimize SQL from the Slow Log Analysis page

Important

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.

  1. Log on to the DAS console.

  2. In the left navigation pane, choose Intelligent O&M Center > Instance Monitoring.

  3. Find the target instance and click the instance ID to open the instance details page.

  4. In the left navigation pane, choose Request Analysis > Slow Logs.

  5. On the Slow Log Analysis page, find the SQL statement to optimize:

    • 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.

  6. In the dialog box that appears, click Create Plan.

  7. Select an execution node type:

    Node typeDescriptionWhen to use
    Standby Node (Default)Secondary node of the current instanceRoutine query analysis
    Event NodeBusiness node where the SQL statement was executedTroubleshooting and optimization

  8. Click Confirm Creation.

DAS generates a visual execution plan for the selected SQL statement.

Read the visual execution plan

The visual execution plan shows the execution status at the current time — it is not a historical record.

Execution flows from bottom to top and from left to right. The node at the bottom is the query's starting point; data flows upward through layers until the final result is output.

For complex execution graphs, use the scaling tool to adjust the view, or click the reset button to return to the initial view.

Node colors

Node color indicates access efficiency:

ColorEfficiencyAccess methods
GreenEfficientsystem, const, eq_ref, ref, ref_or_null, index_merge
YellowMediumfulltext, unique_subquery, index_subquery, range
RedInefficient — prioritize for optimizationall, index

Node display

Each node shows three pieces of information:

PositionContent
TopNode type, such as TABLE_SCAN or INDEX_SCAN
LeftCost — a relative metric evaluated by the optimizer, based on CPU, memory, and disk I/O
RightEstimated number of returned rows

Start optimization with nodes marked in red. These represent access methods with the highest performance impact.

Glossary

The following terms appear in the visual execution plan.

TermPlain nameDescription
QUERY_BLOCKQuery blockA semantic unit of a SQL statement. Each independent query or subquery forms a query block, identified by select_id in the EXPLAIN output.
ATTACHED_SUBQUERIESAttached subqueryA subquery attached to a WHERE, HAVING, or ON clause using predicates such as EXISTS, IN, or ANY. Has a logical dependency on the main query.
CORRELATED_SUBQUERYCorrelated subqueryA nested query that references columns from an outer query. Requires binding to external context values during execution, which can lead to O(n²) complexity.
NON_CORRELATED_SUBQUERYNon-correlated subqueryA self-contained subquery that can execute independently of the outer query. The optimizer usually pre-calculates and materializes it as a constant.
MATERIALIZED_FROM_SUBQUERYMaterialized subqueryAn optimization strategy introduced in MySQL 5.6+. Persists the subquery result to an in-memory temporary table; often associated with a derived table in the subqueryN format.
OPTIMIZED_AWAY_SUBQUERIESOptimized-away subqueryA subquery completely removed after query rewrite optimization, such as a constant subquery pushdown. Does not appear in the final execution plan.
QUERY_SPECIFICATIONSQuery specification structureThe syntactic elements that describe a complete query, including the SELECT list, FROM clause, and other components.
SELECT_LIST_SUBQUERIESSELECT list subqueryA scalar subquery in the projection column (SELECT field list). Must return a single scalar value for each iteration.
INDEX_SCANIndex scanA data access pattern that uses a B+ tree index. Can be a forward or reverse scan; may involve retrieving data from the table using the index.
TABLE_SCANFull table scanScans all pages of the clustered index when no suitable index is available, or when the proportion of data to access exceeds a threshold. Performance degrades with data volume.
ORDERING_OPERATIONResult set sortingAn ORDER BY operation that uses an explicit sorting algorithm such as filesort. May use memory or temporary disk files.
NESTED_LOOPNested loop joinFor each row in the outer table, traverses matching rows in the inner table. Efficient when the join predicate provides effective filtering.
DUPLICATES_REMOVALResult deduplicationImplements DISTINCT semantics using a unique index on a temporary table or sort-then-filter. Cost depends on data distribution.
WINDOWING_OPERATIONWindow function calculationAn analytic function such as ROW_NUMBER or RANK performed on a window defined by an OVER() clause. May require sorting the full dataset.
TABLEBase table referenceA physical storage object directly accessed in the execution plan. Contains the table name, alias, and access method (such as const, system, or range).

Optimize SQL from the Instance Sessions page

Important

SQL optimization on the Instance Sessions page is not supported for self-managed MySQL, MongoDB, or RDS for PostgreSQL instances.

  1. Log on to the DAS console.

  2. In the left navigation pane, choose Intelligent O&M Center > Instance Monitoring.

  3. Find the target instance and click the instance ID to open the instance details page.

  4. In the left navigation pane, click Instance Sessions.

  5. In the Instance Sessions area, find the session to optimize and click Optimize.

  6. In the SQL Diagnostic Optimization dialog box, review the diagnosis results. DAS diagnoses the SQL statement based on its complexity, the data volume of the related table, and the database load. Diagnosis may take more than 20 seconds. After diagnosis completes, DAS shows the results, optimization suggestions, and expected performance gains.

  7. To apply the suggestion, click Copy in the upper-right corner and paste the optimized SQL statement into your database client or Data Management (DMS) to run it. To discard the suggestion, click Cancel.

View SQL diagnosis history

  1. Log on to the DAS console.

  2. In the left navigation pane, choose Intelligent O&M Center > Instance Monitoring.

  3. Find the target instance and click the instance ID to open the instance details page.

  4. In the left navigation pane, click Request Diagnostic History.

The history shows all past diagnosis records for the current instance, including the SQL content, diagnosis status, diagnosis time, and diagnosis results.