通过top -p mysqlpid -H 可以找到thread_os_id ->

                    通过thread_os_id 和  performance_schema.threads 找到thread_id ->
                     通过thread_id和performance_schema.events_statements_current 找到sql语句

[root@node2 ~]# ps -ef | grep -i mysql
mysql 2296 1211 18 14:17 ? 00:03:46 /home/db/mysql/product/bin/mysqld --basedir=/home/db/mysql/product --datadir=/mysqldata/data --plugin-dir=/home/db/mysql/product/lib/plugin --user=mysql --log-error=/mysqldata/logs/mysql_error.log --pid-file=/mysqldata/data/mysqld.pid --socket=/home/db/mysql/product/mysql.sock --port=13306

[root@node2 ~]# top -p 2296 -H
top - 14:59:55 up 42 min, 4 users, load average: 0.41, 0.81, 1.39
Threads: 67 total, 2 running, 65 sleeping, 0 stopped, 0 zombie
%Cpu(s): 94.7 us, 4.7 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.7 si, 0.0 st
KiB Mem : 2238208 total, 77696 free, 943520 used, 1216992 buff/cache
KiB Swap: 2621436 total, 2621436 free, 0 used. 1139404 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAN
2966 mysql 20 0 6212608 868916 30128 R 97.3 38.8 11:46.51 mysqld
2460 mysql 20 0 6212608 868916 30128 R 0.3 38.8 0:09.28 mysqld
2296 mysql 20 0 6212608 868916 30128 S 0.0 38.8 0:00.79 mysqld

系统PID=2966 占用CPU 97%
通过performance_schema.threads.thread_os_id=2966可以找到thread_id,processlist_id ;

root@localhost 15:03:34 [performance_schema]>select thread_id,name ,PROCESSLIST_ID,THREAD_OS_ID from threads where thread_os_id = 2966 ;
thread_idnamePROCESSLIST_IDTHREAD_OS_ID
78thread/sql/one_connection102966

+-----------+---------------------------+----------------+--------------+

通过performance_schema.events_statements_current.thread_id = 78 可以找到当前占用cpu的SQL:

root@localhost 15:07:37 [performance_schema]>select DIGEST_TEXT from performance_schema.events_statements_current where thread_id = 78 ;
DIGEST_TEXT
SELECT * FROM t AS t1 , t AS t2 , t AS t3 , t AS t4

DIGEST_TEXT 的长度由变量max_digest_length控制:

root@localhost 16:08:01 [performance_schema]>show variables like 'max_digest_length' ;
Variable_nameValue
max_digest_length1024

标签: MySQL