领先的免费Web技术教程,涵盖HTML到ASP.NET

网站首页 > 知识剖析 正文

如何识别MySQL中的高CPU消耗SQL语句

nixiaole 2024-12-18 16:10:16 知识剖析 11 ℃

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

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

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

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

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

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

找到线程ID 号,为39449

2、登录到数据库查询performance_schema和information_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>;

Tags:

最近发表
标签列表