MySQL中如何找出CPU高或者IO高的会话

2024-01-24 18:29:55 浏览数 (1)

1、找到CPU最高的会话

step1、根据 top -H -p 9120 显示出线程级别的监控信息(这里的9120是mysqld的进程号)

代码语言:sql复制
# 这里也可以使用htop 然后F4过滤出mysqld进程。使用ps -eLf | grep mysqld也行,但是结果非常不便于查看

top结果类似如下:
top - 22:38:09 up 8 days, 11:59,  8 users,  load average: 0.88, 0.35, 0.29
Threads:  47 total,   7 running,  40 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.3 us,  4.6 sy,  0.0 ni, 92.0 id,  1.0 wa,  0.0 hi,  1.0 si,  0.0 st
KiB Mem : 32847520 total,  1145488 free, 18872872 used, 12829160 buff/cache
KiB Swap:        0 total,        0 free,        0 used. 11155296 avail Mem 

PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME  COMMAND                                                                                                                      
9211 mysql     20   0 5709204   4.8g  42112 R 99.7 15.2   0:17.25 connection     --> top这里找到的是CPU最高的
9160 mysql     20   0 5709204   4.8g  42112 R 17.9 15.2   0:01.75 ib_log_writer  
9158 mysql     20   0 5709204   4.8g  42112 R 12.6 15.2   0:01.82 ib_log_flush   
9146 mysql     20   0 5709204   4.8g  42112 D  3.7 15.2   0:18.50 ib_pg_flush_co 
9159 mysql     20   0 5709204   4.8g  42112 S  3.3 15.2   0:00.67 ib_log_wr_notif
9150 mysql     20   0 5709204   4.8g  42112 D  3.0 15.2   0:00.34 ib_pg_flush-1  
9153 mysql     20   0 5709204   4.8g  42112 D  3.0 15.2   0:00.31 ib_pg_flush-3  

step2、查询ps库,找到某个线程对应的mysql的threads信息

代码语言:sql复制
performance_schema> select * from performance_schema.threads where THREAD_OS_ID=25920 G -- 这里的THREAD_OS_ID就是step1中看到的PID
*************************** 1. row ***************************
            THREAD_ID: 753
                 NAME: thread/sql/one_connection
                 TYPE: FOREGROUND
       PROCESSLIST_ID: 710   --> 这个是mysql里面show proceeslist 看到的会话id
     PROCESSLIST_USER: root
     PROCESSLIST_HOST: localhost
       PROCESSLIST_DB: sbtest
  PROCESSLIST_COMMAND: Query
     PROCESSLIST_TIME: 6
    PROCESSLIST_STATE: updating
     PROCESSLIST_INFO: delete from sbtest8 where id>1     --> 这个就是重IO操作的sql明细
     PARENT_THREAD_ID: NULL
                 ROLE: NULL
         INSTRUMENTED: YES
              HISTORY: YES
      CONNECTION_TYPE: Socket
         THREAD_OS_ID: 25920   -->  top里面看到的MySQL的线程号
       RESOURCE_GROUP: USR_default
     EXECUTION_ENGINE: PRIMARY
    CONTROLLED_MEMORY: 12336
MAX_CONTROLLED_MEMORY: 28816
         TOTAL_MEMORY: 174710
     MAX_TOTAL_MEMORY: 270422
1 row in set (0.00 sec)

2、找到IO最高的MySQL的会话

step1、如果要找到IO高的会话,可以使用 iotop -o 看到的结果类似如下:

代码语言:bash复制
Total DISK READ :	0.00 B/s | Total DISK WRITE :     114.78 M/s
Actual DISK READ:	0.00 B/s | Actual DISK WRITE:     104.79 M/s
TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND  
9146 be/4 mysql       0.00 B/s    5.69 M/s  ?unavailable?  mysqld [ib_pg_flush_co]
9150 be/4 mysql       0.00 B/s    6.03 M/s  ?unavailable?  mysqld [ib_pg_flush-1]
9151 be/4 mysql       0.00 B/s    3.33 M/s  ?unavailable?  mysqld [ib_pg_flush-2]
9153 be/4 mysql       0.00 B/s    3.46 M/s  ?unavailable?  mysqld [ib_pg_flush-3]
9160 be/4 mysql       0.00 B/s   18.72 M/s  ?unavailable?  mysqld [ib_log_writer]
9211 be/4 mysql       0.00 B/s   20.95 M/s  ?unavailable?  mysqld [connection]   --> 主要关注connection这种,上面的ib_pg_flush-x 通常都是因为客户端的请求引起的,因此优先分析connection这种thread。

可以定位到thread_id=9211,然后继续使用step2的方法查询performance_schema.threads即可。

0 人点赞