You can use the slow query log optimization feature to diagnose and optimize SQL statements. An ApsaraDB RDS for MySQL instance is used in this topic to demonstrate how to optimize SQL statements.

Prerequisites

Optimize SQL statements on the Slow Log Statistics tab

  1. On the Slow Log Statistics tab, find the SQL template. Click Optimize in the Actions column.
    Note For more information about how to go to the Slow Log Statistics tab, see Analyze slow query logs.
  2. In the SQL Diagnostic Optimization dialog box, click Copy in the upper-right corner and paste the optimized SQL statements to the database client or DMS for execution. If you do not accept the SQL diagnostic suggestions, click OK.
    In the SQL Diagnostic Optimization dialog box, click Copy in the upper-right corner and paste the optimized SQL statements to the database client or DMS for execution. If you do not accept the SQL diagnostic suggestions, click OK.
    Note DAS performs SQL diagnostics based on the complexity of SQL statements, the amount of data in the table, and the database load. Suggestions may be returned in more than 20 seconds. After the diagnostics are complete, the SQL diagnostics engine provides diagnosis results, optimization suggestions, and expected optimization benefits. You can determine whether to adopt the suggestions.

Optimize SQL statements on the Instance Sessions page

  1. On the Instance Sessions page, select the SQL statement and click Optimize.
    Note For more information about how to go to the Instance Sessions page, see Instance.
  2. In the SQL Diagnostic Optimization dialog box, click Copy in the upper-right corner and paste the optimized SQL statements to the database client or DMS for execution. If you do not accept the SQL diagnostic suggestions, click OK.
  3. In the left-side navigation pane, click Request Diagnostic History to view the optimization history of the current instance, such as the start time of the diagnostics and SQL statements.