mysql快速定位cpu 占比过高的sql语句

当MySQL数据库的CPU使用率异常升高时,需要快速定位SQL有问题的语句

1、使用top命令找出mysl进程中占用CPU靠前的线程

#找出mysql 的进程号
ps -ef | grep mysql

#根据进程号,找出占用CPU靠前的线程号
top -H -p <mysqld进程id>

image-20240711152229011

top 中,按大写的P ,进行CPU 使用率排序

找到线程ID 号,为39449

image-20240711152300355

2、登录到数据库查询 performance_schemainformation_schema

-- 查询性能模式中的线程信息 select * from performance_schema.threads;

-- 查询当前运行的进程列表 select * from information_schema.processlist

使用以下SQL语句可以查询到具体的线程信息,包括其操作系统线程ID(thread_os_id)和正在执行的SQL语句:

贴入, <具体线程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 = <具体线程id>;

202407111539275