Updated on 2024-07-18 GMT+08:00

Viewing and Downloading 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.

Slow query logs generated within the last month can be viewed.

RDS supports the following statement types:
  • All statement types
  • SELECT
  • INSERT
  • UPDATE
  • DELETE
  • CREATE

Parameter Description

Table 1 Parameters related to slow queries

Parameter

Description

long_query_time

Specifies how many microseconds a SQL query has to take to be defined as a slow query log. The default value is 1s. When the execution time of an SQL statement exceeds the value of this parameter, the SQL statement is recorded in slow query logs.

The recommended value is 1s. Note: The lock wait time is not calculated into the query time.

log_queries_not_using_indexes

Specifies whether to record the slow query 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 the slow query log. The default value is 0.

Viewing Log Details

  1. Log in to the management console.
  2. Click in the upper left corner of the page and choose Databases > Relational Database Service.
  3. On the Instances page, click the target DB instance.
  4. In the navigation pane on the left, choose Logs. On the Slow Query Logs page, click Log Details to view details about slow query logs.

    • You can view the slow query log records of a specified execution statement type or a specific time period.
    • Only SELECT statements return the number of result rows. The number of result rows for the INSERT, UPDATE, DELETE, and CREATE statements is 0 by default.
    • Slow query logs only record executed statements whose execution duration exceeds the threshold.
    • The long_query_time parameter determines when a slow query log is recorded. However, changes to this parameter do not affect already recorded logs. If long_query_time is changed from 1s to 0.1s, RDS starts recording statements that meet the new threshold and still displays the previously recorded logs that do not meet the new threshold. For example, a 1.5s SQL statement that was recorded when the threshold was 1s will not be deleted now that the new threshold is 2s.

Viewing Statistics

  1. Log in to the management console.
  2. Click in the upper left corner of the page and choose Databases > Relational Database Service.
  3. On the Instances page, click the target DB instance.
  4. In the navigation pane on the left, choose Logs. On the Slow Query Logs page, click Statistics to view details.

    • 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.
    • No. and Ratio of SQL Executions indicates the ratio of the slow executions to the total executions of the SQL statement.
    • On the Statistics page, only the latest 5,000 slow SQL statements within a specified period are analyzed.
    • You can filter slow log statistics by database name (which cannot contain any special characters), statement type, or time period. The database name supports only exact search.
    • If any database name in the slow log statistics contains special characters such as < > ', the special characters will be escaped.

Downloading a Slow Query Log

  1. Log in to the management console.
  2. Click in the upper left corner of the page and choose Databases > Relational Database Service.
  3. On the Instances page, click the target DB instance.
  4. In the navigation pane on the left, choose Logs. On the Slow Query Logs page, click Downloads. In the log list, locate a log whose status is Preparation completed and click Download in the Operation column.

    • 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 logs contain only the logs of the primary node.