当 MySQL 数据库的 CPU 使用率异常升高时,有必要快速找出有问题的 SQL 语句。在这篇文章中,我们尝试提供一种方法来实现这一目标。
以下是实际步骤。
1.使用top
命令查找 MySQL 进程中 CPU 使用率最高的线程。
# Find the MySQL process ID
ps -ef | grep mysql
# Use the process ID to find the thread IDs with the highest CPU usage:
top -H -p
在top
中,按P
按 CPU 使用率排序。
注意线程 ID,例如 39449。
2.登录数据库,查询 performance_schema 和 information_schema。
-- Query the threads in the performance schema:
SELECT * FROM performance_schema.threads;
-- Query the current process list:
SELECT * FROM information_schema.processlist;
使用以下 SQL 语句查找特定线程信息,包括操作系统线程 IDthread_os_id
和执行的 SQL 语句。用前面提到的线程 ID 替换**[specific thread ID]:**
SELECT
a. USER,
a. HOST,
a.db,
b.thread_os_id,
b.thread_id,
a.id processlist_id,
a.command,
a.time,
a.state,
a.info
FROM
information_schema.PROCESSLIST a,
performance_schema.threads b
WHERE
a.id = b.processlist_id
AND b.thread_os_id = [specific thread ID];
输出结果将显示 SQL 语句。