查看mysql资源占用高的线程及其详细信息

发布时间 2023-09-21 14:19:23作者: 积极向上的徐先生
结合操作系统线程 查看mysql中的sql资源 消耗  ( 5.7 才可以, 5.7时   performance_schema.threads表 才加入的  thread_os_id 系统线程字段 
 1 --1、top -H  查看具体线程的CPU消耗
 2 [root@hostmysql80 mysql]# top -H
 3  
 4  
 5 --2、iotop -u mysql 查看具体线程的IO消耗
 6 [root@hostmysql80 mysql_setup]# iotop -u mysql
 7  
 8  
 9 --3、mysql中 查看操作系统线程id(thread_os_id)  和sql 对应
10 SELECT a.name,
11        a.thread_id,
12        a.thread_os_id,     //操作系统的线程id  (top -H 对应PID,       iotop -u mysql 对应TID)
13        a.processlist_id,   //mysql进程id,可以kill query 或者kill (connection)杀掉。
14        a.type,             //线程类型,分前台线程和后台线程
15        b.user,             //用户
16        b.host,             //ip
17        b.db,               //操作的库名称
18        b.command,          //sql类型
19        b.time,             //sql执行时间 单位:秒
20        b.state,            //sql状态
21        b.info              //sql语句
22   FROM performance_schema.threads a
23   LEFT JOIN information_schema.processlist b
24     ON a.processlist_id = b.id
25 where a.type = 'FOREGROUND';

 参考链接:

MySQL查看线程内存占用情况 - 知乎 (zhihu.com)

Mysql查看状态,连接数,线程数以及Mysql性能监控工具doDBA的使用以及优化 - mungerz - 博客园 (cnblogs.com)

MySQL-进程占用CPU资源高问题分析 - KuBee - 博客园 (cnblogs.com)