Slow SQL query
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 the trend and statistics of slow query logs. In addition, the system provides optimization suggestions and diagnostic analysis for related SQL queries to facilitate in-depth analysis and optimization.
Prerequisites (optional)
To view slow query logs, make sure that 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 target cluster ID to go to the cluster details page.
In the left-side navigation pane, choose .
The 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
On the Automatic SQL optimization suggestions page, select a time range.
Enable automatic SQL optimization: This feature automatically identifies problematic SQL queries, generates index optimization suggestions, and creates indexes without causing table locks.
NoteFor more information, see Automatic SQL optimization.
Query governance
On the Query Governance page, view the query governance results.
Query governance results overview: View the categorized result data tagged by the system.
NoteOnly Failed SQL Executions for instances with DAS Enterprise Edition enabled are counted.
Query governance trends: View the trend of query governance results within the selected time range.
Top Rankings: View the Best-performing Instances and Worst-performing Instances.
Instance blacklist: View the number of slow SQL query executions for each database instance.
Instance red list: View the change in the number of slow SQL query executions for each database instance. A negative value indicates a decrease (good optimization result), while a positive value indicates an increase.
DAS recommends that you focus on the Optimizable SQL rankings.
SQL to Be Optimized: You can set filter criteria to filter 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.
Multiple DB names are separated by commas (,) and combined with a logical OR.
Multiple SQL keywords are separated by spaces and combined with a logical AND.
Multiple database usernames are separated by commas (,) and combined with a logical OR.
You can select multiple rule tags. The selected tags are combined with a logical OR.
In the Actions column for a target SQL sample, click Suggestions to view detailed governance recommendations.
In the Actions column for a target SQL sample, click Add Tag to manually tag the SQL query. For more information about the tags, see SQL tags that support manual tagging.
You can also select multiple SQL queries to tag them in a batch.
In the Actions column for a target SQL sample, click Sample to view the details of the slow query log sample for this query.
In the Actions column for a target SQL sample, click Trend to view the slow log analysis details for this query. For more information about slow log analysis, see Slow query log.
You can export and share the data of SQL queries to be optimized. For more information, see Slow query log.
Failed SQL: You can set filter criteria to filter the SQL queries that you want to view.
NoteOnly Failed SQL for instances with DAS Enterprise Edition enabled are counted.
You can filter by DB name and SQL keyword. These two filters are combined with a logical AND.
Multiple DB names are separated by commas (,) and combined with a logical OR.
Multiple SQL keywords are separated by spaces and combined with a logical AND.
In the Actions column for a target SQL sample, click Sample to view the details of this SQL sample.
Adjust the slow SQL query threshold
You can go to the PolarDB console and navigate to . On that page, set the long_query_time parameter to adjust the threshold for slow SQL queries.
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
High CPU utilization for PolarDB for MySQL
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 log and SQL Explorer) are enabled for a PolarDB for MySQL cluster. | |
Enables or disables SQL collection features (such as audit log and SQL Explorer) for a PolarDB for MySQL cluster. |