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 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 point in time to view the Slow Log Statistics and Slow Log Details for that time.
NoteIf a slow SQL statement is too long to be fully displayed, you can hover the mouse pointer over the statement to view the complete SQL statement in a pop-up box.
In the Event Distribution area, you can query for slow log events within a 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, you can click
to save the slow query log information to your computer.You can click
to go to the OpenAPI Explorer console. The currently selected and entered parameters are automatically carried over for API debugging.In the Slow Log Statistics area:
Above the list, you can select filter conditions to filter the data. The available filter conditions vary depending on the database engine.
Click the target SQL ID column to view correlations and a detailed list that includes user distribution, client distribution, and metric trends.
In the Actions column of the target SQL template, click Optimize. In the SQL Diagnostic Optimization dialog box that appears, view the SQL diagnosis results.
If you accept the diagnostic suggestion, click Copy in the upper-right corner of the page and paste the optimized SQL statement into your database client or DMS to execute it. If you do not accept the suggestion, click Cancel to end the diagnosis.
NoteDAS performs SQL diagnosis based on the complexity of the SQL statement, the data volume of the corresponding table, and the database payload. The diagnostic suggestion may take more than 20 seconds to be returned. After the diagnosis is complete, the diagnostics engine provides a diagnosis result, an optimization suggestion, and the expected optimization benefits. You can decide whether to accept the suggestion based on the diagnosis result.
In the Actions column of the target SQL template, click Throttling. On the SQL Throttling page, configure the throttling parameters to apply rate limiting to the target SQL statement. For more information, see SQL throttling.
For a PolarDB for MySQL database instance, in the Actions column of the target SQL template, click IMCI to view the documentation for In-Memory Column Index (IMCI).
NoteThe IMCI button is displayed if a PolarDB for MySQL database instance does not have an In-Memory Column Index node, the Max Execution Time of a slow query log exceeds 20 seconds, and the Max Scanned Rows exceeds 200,000.
For complex queries with large data volumes, you can use In-Memory Column Index (IMCI) to improve query performance.
In the Slow Log Details area, you can also click Optimize and Throttling in the Actions column for a target SQL statement to perform SQL Diagnostic Optimization and 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
High CPU utilization in 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 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. |