A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cloud.google.com/sql/docs/mysql/optimize-cpu-usage below:

Optimize high CPU usage in instances | Cloud SQL for MySQL

Skip to main content Optimize high CPU usage in instances

Stay organized with collections Save and categorize content based on your preferences.

High CPU utilization in an instance can be caused by various reasons such as increase in workloads, heavy transactions, slow queries, and long running transactions.

The underprovisioned instance recommender analyzes CPU utilization. If the CPU utilization levels are at or over 95% for a significant amount of time within the last 30 days, the recommender alerts you and provides additional insights to help resolve the issue.

This document explains how to review and optimize a Cloud SQL for MySQL instance if that instance is identified by the underprovisioned instance recommender as having high CPU utilization.

Recommendations

CPU utilization increases proportionally with workload. To reduce CPU utilization, check the running queries and optimize them. Here are a few steps to check the consumption of CPU.

  1. Check Threads_running and Threads_connected

    Use the following query to see the number of active threads:

    > SHOW STATUS like 'Threads_%';
    

    Threads_running is a subset of Threads_connected. The rest of the threads are idle. An increase in Threads_running would contribute to an increase in CPU usage. It's a good idea to check what's running on those threads.

  2. Check query states

    Run the SHOW PROCESSLIST command to view the ongoing queries. It returns all the connected threads in order and their actively running SQL statement.

    mysql> SHOW [FULL] PROCESSLIST;
    

    Pay attention to the state and duration columns. Check if there are many queries stuck on the same state.

  3. Check for record lock contention

    When transactions hold locks on popular index records, they block other transactions requesting the same locks. This might get into a chained effect and cause a number of requests being stuck and an increase in the value of Threads_running. To diagnose lock contention, use the information_schema.innodb_lock_waits table.

    The following query lists each blocking transaction and the numbers of associated blocked transactions.

    SELECT
      t.trx_id,
      t.trx_state,
      t.trx_started,
      COUNT(distinct w.requesting_trx_id) AS blocked_trxs
    FROM
      information_schema.innodb_lock_waits w
    INNER JOIN information_schema.innodb_trx t
       ON t.trx_id = w.blocking_trx_id
    GROUP BY t.trx_id,t.trx_state, t.trx_started
    ORDER BY t.trx_id;
    

    Both a single large DML and many concurrent small DMLs might cause row lock contentions. You can optimize this from the application side by using the following steps:

  4. Find long running transactions

    If the transactions show the current long running statements, then you can decide to either stop these transactions to reduce the pressure on the server or wait for the critical transactions to complete. If the older transactions are not showing any activities, then go to the next step to find transaction history.

  5. Check SQL statements of the long running transactions

  6. Check semaphore contention

    In a concurrent environment, mutex and read/write latch on shared resources might be the contention point, which slows the server performance. Moreover, if the semaphore wait time is more than 600 seconds, the system can crash to get out of the stall.

    To view the semaphore contention, use the following command:

    mysql> SHOW ENGINE INNODB STATUS\G
    ----------
    SEMAPHORES
    ----------
    ...
      --Thread 140396021667584 has waited at row0purge.cc line 862 for 241.00 seconds the semaphore:
      S-lock on RW-latch at 0x30c03e8 created in file dict0dict.cc line 1183
      a writer (thread id 140395996489472) has reserved it in mode  exclusive
      number of readers 0, waiters flag 1, lock_word: 0
      Last time read locked in file row0purge.cc line 862
      Last time write locked in file /build/mysql-5.7-FFKPr6/mysql-5.7-5.7.22/storage/innobase/dict/dict0stats.cc line 2376
    ...
    

    With each semaphore wait, the first line shows the thread that's waiting, the specific semaphore, and the length of time it has waited. If there are frequent semaphore waits when repeatedly running SHOW ENGINE INNODB STATUS, especially waits of more than a few seconds, it means that the system is running into concurrency bottlenecks.

    There are different contention points in different workloads and configurations.

    When the semaphores are often on btr0sea.c, the adaptive hash indexing might be the source of contention. Try to disable it using Google Cloud console or gcloud CLI.

  7. Optimize long SELECT queries

    First, review the query. Identify the goal of the query and the best way to get the results. The best query plan is the one that minimizes data access.

    mysql> EXPLAIN <the query>;
    

    See MySQL documentation to learn how to interpret the output and evaluate the query efficiency.

    Check the key column to see if the expected index is used. If not, update the index statistics:

    mysql> analyze table <table_name>
    

    Increase the number of sample pages that are used to calculate index statistics. To learn more, see the MySQL documentation.

    When using a multicolumn index, check the key_len columns to see if the index is fully leveraged to filter the records. The leftmost columns need to be equal comparisons, and the index can be used up to and including the first range condition.

    Another way to make sure the right index is used is to apply index hint and hint for table join order.

  8. Avoid a long history list with READ COMMITTED

    The history list is the list of unpurged transactions in the undo tablespace. The default isolation level of a transaction is REPEATABLE READ, which requires a transaction to read the same snapshot throughout its duration. Hence, a SELECT query blocks the purging of undo log records that were made since the query (or transaction) started. A long history list thus slows the query performance. One way to avoid building a long history list is to change the transaction isolation level to READ COMMITTED. With READ COMMITTED, there is no longer the need to keep the history list for a consistent read view. You can change the transaction isolation level globally for all sessions, for a single session, or for the next single transaction. To learn more, see the MySQL documentation.

  9. Tune server configuration

    There is a lot to say about server configuration. While the full story is beyond the scope of this document, it's worth mentioning that the server also reports various status variables that give hints on how well the related configurations are. For example:

  10. End an unwanted connection

    You can stop the query if the query seems invalid or it isn't required anymore. To learn how to identify and end the MySQL thread, see Manage database connections.

Lastly, if the CPU usage is still high and the queries form necessary traffic, then consider increasing the CPU resources in your instance to avoid database crash or downtime.

What's next

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-14 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-07-14 UTC."],[],[]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4