Managing Slow Query Logs
Scenarios
Slow query logs record statements that exceed the long_query_time value (1 second by default). You can view log details and statistics to identify statements that are executing slowly and optimize the statements. You can also download slow query logs for service analysis.
Constraints
Original logs will be automatically deleted 30 days later. If the instance is deleted, its logs are also deleted.
Parameter Description
| Parameter | Description |
|---|---|
| long_query_time | Any SQL statement whose execution time reaches or exceeds the value of this parameter is written to the slow query log. The value can be specified to a resolution of microseconds. The default value is 1 (unit: second). The recommended value is 1s. Note: The lock wait time is not calculated into the query time. Modifying long_query_time takes effect globally. After the modification, the new value is applied to new connections. The existing connections still use the old value. |
| log_queries_not_using_indexes | Specifies whether to record slow queries without indexes. The default value is OFF. |
| log_throttle_queries_not_using_indexes | Limits the number of SQL statements without indexes per minute that can be written to slow query logs. The default value is 0. |
Viewing Slow Query Logs
- Log in to the RDS console.
- Click
in the upper left corner and select a region. - On the Instances page, click the target instance name.
- In the navigation pane, choose Logs and click the Slow Query Logs tab.
- On the Log Details tab page, turn on the toggle switch
next to Show Original Logs. Alternatively, on the Instances page, select multiple RDS for MySQL instances and choose More > Configure Slow Query Log Sensitization above the instance list to enable this function for all the selected instances. You can enable it for up to 50 instances at a time.
Figure 1 Turning on Show Original Logs
- In the displayed dialog box, click OK. Figure 2 Submitting the request
- Log in to the RDS console.
- Click
in the upper left corner and select a region. - On the Instances page, click the target instance name.
- In the navigation pane, choose Logs and click the Slow Query Logs tab.
- On the Log Details tab page, you can view the slow query log records of a specific statement type or time range. Figure 3 Slow query log details
If Slow Query Log Ingestion to LTS is enabled, you can switch to the analysis view in the upper right corner of the page to see more details. For details about each module in the analysis view, see Viewing Log Management.Figure 4 Analysis view
- The long_query_time parameter determines how long a SQL statement must run before it is recorded in the slow query log. Changes to this parameter apply only to queries logged after the modification. For example, if the value of this parameter is changed from 1 to 0.1 (unit: second), RDS starts recording statements that meet the new threshold and still displays the previously recorded logs that do not meet the new threshold.
- A maximum of 2,000 slow query log records can be queried. To view more slow query log records, submit a service ticket.
- To export log details, you can export them by time range. A maximum of 1,000 records can be exported at a time.
Table 2 Slow query log details Parameter
Description
Execute Statement
The slow query log records only statements that have been executed and whose execution time exceeds the threshold. Statements still in progress are not logged.
If the length of a single line of a SQL statement exceeds 10 KB or the statement has more than 200 lines, the SQL statement will be truncated. SQL statements in the slow query log details may appear incomplete because they have been specially processed, so they are for reference only.
Statement Type
- You can choose a statement type from the drop-down list in the upper right corner. Options include: All types, SELECT, INSERT, UPDATE, DELETE, CREATE, CALL, ALTER, TRUNCATE, RENAME, FLUSH, EXPLAIN, LOCK, GRANT, REVOKE, COMMIT, ROLLBACK, DROP, and WITH.
- You can also click
in the upper right corner to select a time range and view slow query logs for different periods. Only slow query logs generated within the last month can be viewed.
Execution Status
Execution status of a SQL statement. If the execution status is displayed as 0, execution of the SQL statement is properly completed. However, if the SQL statement is forcibly terminated, its execution status is not 0.
The execution status is left blank if the version is 5.7.23 or earlier, 5.6.41 or earlier, or if log_slow_extra is disabled.
- 0: The SQL statement execution is successful.
- 1: Data is corrupted.
- 1053: The server is disconnected.
- 1317: The SQL statement execution is interrupted.
- 3024: The SQL statement execution times out.
Occurred
UTC time when the slow SQL statement starts to be executed.
Execution Time (s)
Duration of the slow SQL query.
Lock Wait Time (s)
Time spent waiting for both table locks and row locks.
Result Rows
Only SELECT statements return the number of result rows (column Result Rows). The number of result rows for the INSERT, UPDATE, DELETE, and CREATE statements is 0 by default.
Scanned Rows
Number of data rows that are actually accessed and processed during the SQL execution.
Database
Database where the SQL statement is executed on.
You can filter slow query logs by specifying a database name (only exact searches work and special characters are not allowed).
Username
Account that executed the SQL statement.
IP Address
Host used to connect to the database. The value is localhost for local login.
- Log in to the RDS console.
- Click
in the upper left corner and select a region. - On the Instances page, click the target instance name to go to the Summary page.
- In the navigation pane, choose Logs and click the Slow Query Logs tab.
- Click the Statistics tab. Figure 5 Statistics
- On the Statistics page, only the latest 5,000 slow SQL queries within a specified period are analyzed.
- To export statistics, you can export them by time range. A maximum of 1,000 records can be exported at a time.
Table 3 Slow query log statistics Parameter
Description
Execute Statement
- On the Statistics page, only one of the SQL statements of the same type is displayed as an example. For example, if two select sleep(N) statements, select sleep(1) and select sleep(2), are executed in sequence, only select sleep(1) will be displayed.
- If Show Original Logs is enabled, all of the slow SQL statements are displayed. For example, if select sleep(1) and select sleep(2) are executed in sequence, both of them will be recorded in the log.
Statement Type
You can choose a statement type from the drop-down list in the upper right corner. Options include: All types, SELECT, INSERT, UPDATE, DELETE, CREATE, CALL, ALTER, TRUNCATE, RENAME, FLUSH, EXPLAIN, LOCK, GRANT, REVOKE, COMMIT, ROLLBACK, DROP, and WITH.
No. and Ratio of SQL Executions
Number of times that this type of SQL statement was executed slowly in the selected time range, along with the percentage it represents out of all slow SQL statements during the same period.
Avg. Execution Time (s)
Average duration of slow SQL queries within the selected time range.
Avg. Lock Waiting Time (s)
Average lock wait time within the selected time range.
Avg. No. of Result Rows
Average number of result rows in the selected period.
Avg. No. of Scanned Rows
Average number of scanned rows in the selected period.
Database
Database where the SQL statement is executed on. If any database name in slow query log statistics contains special characters (such as < > '), the special characters will be escaped.
You can filter slow query log statistics by specifying a database name (only exact searches work and special characters are not allowed).
Downloading a Slow Query Log
You can download a log that is not larger than 40 MB directly from the console.
To download a log larger than 40 MB, use OBS Browser+.
- Log in to the RDS console.
- Click
in the upper left corner and select a region. - On the Instances page, click the target instance name.
- In the navigation pane, choose Logs and click the Slow Query Logs tab.
- On the Downloads tab page, find the log which is in the Preparation completed state and click Download in the Operation column. Figure 6 Downloading a slow query log
- The system automatically loads the downloading preparation tasks. The loading duration is determined by the log file size and network environment.
- When the log is being prepared for download, the log status is Preparing.
- When the log is ready for download, the log status is Preparation completed.
- If the preparation for download fails, the log status is Abnormal.
Logs in the Preparing or Abnormal status cannot be downloaded.
- The download link is valid for 5 minutes. After the download link expires, a message is displayed indicating that the download link has expired. If you need to redownload the log, click OK.
- The downloaded log file contains only the logs of the primary node or a read replica.
- The system automatically loads the downloading preparation tasks. The loading duration is determined by the log file size and network environment.
- Log in to the RDS console.
- Click
in the upper left corner and select a region. - On the Instances page, click the target instance name.
- In the navigation pane, click Logs. On the Slow Query Logs page, obtain the name of the slow query log file. Figure 7 Downloading a slow query log
- Download OBS Browser+.
- Decompress and install OBS Browser+. Figure 8 Logging in to OBS Browser+
- On the OBS Browser+ download page of any backup of the current instance, obtain the name of an external bucket. Figure 9 Obtaining an external bucket name
- Add an external bucket. Figure 10 Adding an external bucket
If you want to access OBS external buckets across accounts, the access permission is required. For details, see Granting IAM Users Under an Account the Access to a Bucket and Resources in the Bucket.
- Click the name of the external bucket to go to the object list page. In the search box on the right, search for the log file name obtained in 4 and click
to download the log file. Figure 11 Downloading a log file
The downloaded log file contains only the logs of the primary node or a read replica.
Slow Query Log Ingestion to LTS
- Log in to the RDS console.
- Click
in the upper left corner and select a region. - On the Instances page, click the target instance name to go to the Summary page.
- In the navigation pane, choose Logs and click the Slow Query Logs tab.
- Click Ingest Logs to LTS in the upper right corner of the page.
- In the displayed dialog box, select Ingest Logs to LTS, select Slow logs under Log Types, select a log group and log stream, and click OK. Figure 12 Configuring slow query log ingestion to LTS
FAQ
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot