PolarDB for MySQL defines SQL statements that take more than 1 second to execute as slow SQL queries, and provides an SQL analysis feature in the console. You can view slow log trends and slow query log statistics. The system also provides optimization suggestions and diagnostic analysis for the related SQL statements to help you perform in-depth analysis and optimization.
Prerequisites (optional)
To make sure that you can view slow SQL query logs, check whether your account has been granted the AliyunHDMFullAccess permission. For finer-grained permission management, you can use a custom policy to grant permissions to a RAM user for flexible control over PolarDB.
Note
The maximum length of a slow log is 16 KB. The portion that exceeds this length is truncated.
View slow SQL queries
Log on to the PolarDB console. In the left-side navigation pane, click Clusters, select the region where the cluster is deployed, and click the ID of the target cluster to go to the cluster details page.
In the left-side navigation pane, choose .
Slow SQL page provides the following three features:
Slow log analysis
On the Slow Log Analysis tab, select a time range, and then view Slow Log Trend, Event Distribution, Slow Log Statistics, and Slow Log Details.
In the Slow Log Trend chart, you can select a specific point in time to view the corresponding Slow Log Statistics and Slow Log Details.
NoteIf a slow SQL statement is too long to be fully displayed, hover the pointer over the statement to view the complete text in a pop-up window.
In the Event Distribution section, you can find slow log events within the specified time range. Click an event to view its details.
-
From the Node ID drop-down list, you can view the number of slow requests for each node.
On the Slow Query Log Statistics and Slow Query Log Details tabs, click
to save the slow query log information to a local file.Click
to go to OpenAPI Explorer and debug the API. The currently selected and entered parameters are passed automatically.In the Slow Log Statistics section:
Above the list, you can select filter conditions to refine the data. The available filter conditions vary by database engine.
Click an ID in the Query ID column of a SQL template to view its correlation analysis and a detailed list, including user distribution, client distribution, and metric trends.
In the Actions column for the target SQL template, 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 and paste the optimized SQL statement into a database client or DMS to run it. If you do not accept the suggestion, click Cancel to close the dialog box.
NoteDAS diagnoses SQL statements based on their complexity, the data volume of related tables, and the database load. The diagnosis may take more than 20 seconds to complete. Once complete, the SQL diagnostics engine provides a result, an optimization suggestion, and the expected performance improvement. You can then decide whether to accept the suggestion.
In the Actions column for the target SQL template, click Throttling. On the SQL Throttling page, configure throttling parameters for the target SQL statement. For more information, see SQL throttling.
For PolarDB for MySQL database instances, click IMCI in the Actions column to view the documentation for In-Memory Column Index (IMCI).
NoteThe IMCI button is displayed for a PolarDB for MySQL instance if it does not have an IMCI node, its slow query log's Max Execution Time exceeds 20 seconds, and the Max Scanned Rows exceeds 200,000.
For complex queries on large volumes of data, we recommend that you use In-Memory Column Index (IMCI) to improve query performance.
In the Slow Log Details section, you can also click Optimize or Throttling in the Actions column for a target SQL statement to perform SQL Diagnostic Optimization or SQL Throttling.
Automatic SQL optimization suggestions
Automatic SQL optimization suggestions page: select the time range that you want to view.
Enable automatic SQL optimization: automatically identifies problematic SQL queries, generates index optimization suggestions, and creates indexes without causing table locks.
NoteYou can view the details based on your business needs. For more information, see Automatic SQL optimization.
Query governance
On the Query Governance page, view the query governance results.
Query governance results overview: view the result data that the system has tagged and categorized.
NoteOnly instances with DAS Enterprise Edition enabled are counted for Failed SQL Executions.
Query governance trends: view how the query governance results change within the selected time range.
Top Rankings: view the Best-performing Instances and Worst-performing Instances.
Worst-performing instances: view the number of slow SQL query executions for each database instance.
Best-performing instances: view the change in the number of slow SQL query executions for each database instance. A negative value indicates a decrease in the number of slow SQL query executions (a good optimization result), and a positive value indicates an increase.
DAS recommends that you focus on the Optimizable SQL rankings.
SQL to Be Optimized: set filter criteria to display only the SQL queries that require governance.
NoteYou can filter by DB name, SQL keyword, rule tag, and database username. These four filters are combined with a logical AND.
Separate multiple DB names with commas (,). They are combined with a logical OR.
Separate multiple SQL keywords with spaces. They are combined with a logical AND.
Separate multiple database usernames with commas (,). They are combined with a logical OR.
You can select multiple rule tags. The selected rules are combined with a logical OR.
In the Actions column of the target SQL sample, click Suggestions to view the detailed governance recommendations.
In the Actions column of the target SQL sample, click Add Tag to tag this SQL query manually. For more information about the meaning of each tag, see SQL tags that support manual tagging.
You can also select the SQL queries that you want to tag manually and tag them in batches.
In the Actions column of the target SQL sample, click Sample to view the details of the slow query log sample for this SQL query.
In the Actions column of the target SQL sample, click Trend to view the slow log analysis details of this SQL query. For more information about slow log analysis, see Slow query log.
You can export and share the data of the SQL queries to be optimized as needed. For more information, see Slow query log.
Failed SQL: set filter criteria to display only the SQL queries that you want to view.
NoteOnly instances with DAS Enterprise Edition enabled are counted for Failed SQL.
You can filter by DB name and SQL keyword. These two filters are combined with a logical AND.
Separate multiple DB names with commas (,). They are combined with a logical OR.
Separate multiple SQL keywords with spaces. They are combined with a logical AND.
In the Actions column of the target SQL sample, click Sample to view the details of this SQL sample.
Adjust the slow SQL query threshold
Go to the PolarDB console and open the page, where you can set the long_query_time parameter to adjust the threshold that defines a slow SQL query.
Parameter | Description |
long_query_time | Records all queries whose execution time exceeds the parameter value in the slow query log. Valid values: 0.03 to 31536000. Default value: 1. Unit: seconds. |
FAQ
Best practices
Related APIs
API | Description |
Queries the statistics of slow query logs for a PolarDB for MySQL cluster. | |
Queries the details of slow query logs for a PolarDB for MySQL cluster. | |
Queries whether SQL collection features (such as audit logs and SQL Explorer) are enabled for a PolarDB for MySQL cluster. | |
Enables or disables SQL collection features (such as audit logs and SQL Explorer) for a PolarDB for MySQL cluster. |