linux环境下排查cpu占比高的MySQL数据库sql语句

  自MySQL 5.7版本后,PERFORMANCE_SCHEMA.THREADS表中新增一个字段THREAD_OS_ID,对应操作系统中的线程ID

  1.安装sysstat工具包(为了使用pidstat命令),更多信息参考: https://www.linuxidc.com/Linux/2019-08/160082.htm

    Ubuntu: apt-get install sysstat
    CentOS/RedHat: yum -y install sysstat

  2.查看linux系统中cpu占比高的线程id,使用"pidstat -t -p <mysqld_pid> 1"命令

    -t: 表示显示线程id,不然默认显示进程id

    -p <mysqld_pid>: 表示只显示mysql进程下的线程,mysql pid可通过top命令查看

    1: 表示每秒更新一次

  3.查看cpu占比高的线程ID,TID,如32053  

  4. 在mysql数据库执行"select * from performance_schema.threads where THREAD_OS_ID = 32053 G"

    g 和在sql语句中写’;’是等效的
    G 将查到的结构旋转90度变成纵向    

   5.可以看到有个PROCESSLIST_INFO字段,表示的就是SQL语句,最后用explain命令分析SQL,进行优化

参考:

1.https://www.percona.com/blog/2020/04/23/a-simple-approach-to-troubleshooting-high-cpu-in-mysql/

2.https://www.linuxidc.com/Linux/2019-08/160082.htm

3.https://blog.csdn.net/guoqianqian5812/article/details/51754594

原文地址:https://www.cnblogs.com/silence-maple/p/15125968.html