Updated on 2024-10-14 GMT+08:00

High CPU Usage of RDS for MySQL Instances

If the CPU usage of your RDS for MySQL instance is high or close to 100%, database performance deteriorates. For example, data read/write becomes slow, connecting to the instance takes a longer time, or errors are reported when you are trying to delete data.

The following functions of Data Admin Service (DAS) are only available to the accounts that are using them, from November 25, 2021, 00:00 GMT+08:00: SQL tuning, table structure comparison and synchronization, data tracking and rollback, data generator, and DBA intelligent O&M in Development Tool, as well as space, intelligent parameter recommendation, historical transaction, and binlog parsing functions in Intelligent O&M.

  • For DB instances created after November 25, 2021, 00:00 GMT+08:00, Solution 1 is recommended.
  • For DB instances created before November 25, 2021, 00:00 GMT+08:00, Solution 2 is recommended.

You are advised to enable SQL audit in advance so that you can view SQL execution records in audit logs to locate the fault when the CPU usage is high.

Solution 1

Analyze slow SQL logs and CPU usage to locate slow queries and then optimize them.

  1. View the slow SQL logs to check for slowly executed SQL queries and view their performance characteristics (if any) to locate the cause.

    For details on viewing RDS for MySQL logs, see Slow Query Log.

  2. View the CPU usage metric of your DB instance.

    For details, see Performance.

  3. Create read replicas to reduce read pressure from primary DB instances.
  4. Add indexes for associated fields in multi-table association queries.
  5. Do not use the SELECT statement to scan all tables. You can specify fields or add the WHERE condition.

Solution 2

You can identify slow query statements and optimize them according to the suggestions provided by DAS to reduce the CPU usage.

  1. Connect to the RDS for MySQL DB instance.

    You can connect to an instance through a private or public network. For details, see the Relational Database Service Getting Started.

  2. Run the following command to show the running threads and locate the queries that are slowly executed:

    show full processlist

    Check the values in the Time and State columns. As shown in the preceding figure, the long-running transaction ID is 4038566.

  3. Use SQL diagnosis of DAS to identify SQL statements that are executed frequently, consume a large amount of resources, or take a long time to execute. You can optimize the statements according to the diagnosis suggestions to ensure the stability of the database performance.

    1. Log in to the DAS console.
    2. In the navigation pane, choose Intelligent O&M > Instance List.
    3. Click Details on the instance.
      Figure 1 Instance list
    4. Choose SQL > SQL Diagnosis.
    5. Select a database, enter an SQL statement, and click Diagnose.
      Figure 2 SQL diagnosis
    6. View diagnosis details and obtain statement optimization suggestions.
      Figure 3 Tuning details
      • Only the diagnosis of SELECT, INSERT, UPDATE, and DELETE statements is supported. An INSERT statement must contain a SELECT clause.
      • SQL statements for querying system databases like information_schema, test, and mysql are not supported.
      • SQL statements that use views are not supported.
      • Using SQL diagnosis can obtain table structure and data distribution information (non-original). The obtained information is only used for logic diagnosis, but not stored on the DAS server.
      • Obtaining table structure and data distribution information may cause additional load on the DB instance, but has little impact on its performance.
      • Only the SQL diagnosis history is stored on the DAS server. You can delete it from the server permanently.