Updated on 2024-08-29 GMT+08:00

Top SQL

After collecting all SQL statements is enabled, Top SQL shows the SQL queries that have been contributing the most to DB load. You can sort them by multiple dimensions.

Precautions

  • Top SQL is not available to free instances. To use this function, set free instances as paid instances or subscribe to Intelligent O&M.
  • To use Top SQL, enable Collect All SQL Statements first, which is disabled by default. Collecting all SQL statements generates a performance loss of no more than 5%.
  • After Collect All SQL Statements is disabled, new SQL statements will not be collected anymore and the collected SQL data will be deleted.
  • SQL Explorer cannot record all SQL data. As for RDS for MySQL, it has the following constraints:
    • Some data cannot be recorded if a buffer overrun occurs.
    • Any SQL statement that exceeds 4096 bytes is discarded by default.

      This constraint can be removed by setting parameter rds_sql_tracer_reserve_big_records for RDS for MySQL 5.7.33.3 or later. You can set the parameter to ON by referring to Modifying RDS for MySQL Instance Parameters, which means that SQL data records containing more than 4,096 bytes are stilled recorded.

  • Data of SQL statements executed on free instances is retained for only one hour. For paid instances and the free instances that you have subscribed to Intelligent O&M for, SQL data is retained for seven days by default and can be retained for up to 30 days. After the retention period expires, collected SQL data is deleted.
  • This function is supported only in DAS 5.6.51.1 and later, 5.7.29.2 and later, and 8.0.20.3 and later versions.

Procedure

  1. Log in to the DAS console.
  2. Click in the upper left corner and select a region and project.
  3. Click in the upper left corner, and under Databases, click Data Admin Service.
  4. In the navigation pane, choose Intelligent O&M > Instance List.

    Alternatively, on the Overview page, click Go to Intelligent O&M.

  5. In the upper right corner of the Instance List page, search for instances by engine type, instance name, or instance IP.

    Figure 1 Searching for instances

  6. Locate the box containing your target instance and click Details.
  7. Choose SQL > Top SQL.

    Toggle on on the right of Collect All SQL Statements.

    You can also click Log Settings in the upper right corner of the page to enable Collect All SQL Statements.

    Figure 2 Configuring log settings

    Collect All SQL Statements takes effect once it is enabled.

    If you want to disable Collect All SQL Statements, click Log Settings in the upper right corner and toggle off the switch.

  8. On the Top SQL tab page, view execution duration of top SQL statements.

    You can select Last hour, Last 3 hours, or Last 6 hours to view execution durations of top SQL statements. You can also set a time range that spans no more than one day.
    Figure 3 Top SQL

    The Execution Duration Distribution graph shows how many times SQL statements with different elapsed time lengths are executed over a specified time range. DAS collects SQL statistics of four elapsed time lengths.

    • < 100 ms: number of SQL statement executions whose elapsed time is less than 100 ms
    • 100 ms–500 ms: number of SQL statement executions whose elapsed time ranges from 100 ms to 500 ms
    • 500 ms–1s: number of SQL statement executions whose elapsed time ranges from 500 ms to 1s
    • > 1s: number of SQL statement executions whose elapsed time is longer than 1s

    SQL statistics in the four elapsed time lengths are collected over the time range you specified.

    • If you select Last hour, SQL statement executions are calculated every 10 seconds.
    • If you select Last 6 hours, SQL statement executions are calculated every minute.
    • If you select a time range longer than 6 hours, SQL statement executions are calculated every 5 minutes.

  9. Use either of the following methods to view execution duration details, such as average execution duration, total duration, average lock wait duration, and average scanned rows.

    • Hover your mouse at any point in time on the graph to view top SQL statements at that time.
      Figure 4 Viewing top SQL statements at a specific point in time
    • Specify a time range using your mouse on the graph and you will see top SQL statements during that time range.
      Figure 5 Viewing top SQL statements in a specified time range
    • Click to export details about all top SQL templates in the list. This function is available only to paid instances.
    • In the SQL template list, locate a SQL template and click Details to view the total execution times, average rows scanned, average execution duration, and the like.
    • Locate a SQL template and click Concurrency Control in the Operation column to create a concurrency control rule. For details, see SQL Statement Concurrency Control.
    • Select Date Comparison in the upper left corner of the page to compare and analyze distributions of top SQL statements of the current instance in the same time range on different days.
      Figure 6 Top SQL comparison