欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL数据库show processlist指令使用解析

程序员文章站 2023-09-07 11:05:46
  在实际项目开发中,如果我们对数据库的压力比较大,比如有大批量的查询或者插入等sql,尤其是多线程插入等情况,针对部分执行比较慢的sql,我们可以将其kill掉,常用的一个命令就是s...

  在实际项目开发中,如果我们对数据库的压力比较大,比如有大批量的查询或者插入等sql,尤其是多线程插入等情况,针对部分执行比较慢的sql,我们可以将其kill掉,常用的一个命令就是show processlist

  1. show processlist是什么

show processlist:通过查看mysql的官网,可以发现,其主要是查询数据库中哪些线程正在执行,针对比较慢的线程(time的数值比较大的线程)我们可以将其kill掉。此外,show full processlist 返回的结果是实时变化的。

  2. show processlist怎么用

  有三种方式可以执行show processlist,可以通过命令行、sql语句、navicat客户端等。

  1) 命令行:show full processlist\g

  执行结果如下:

mysql> show full processlist\g

*************************** 1. row ***************************
id: 1
user: system user
host:
db: null
command: connect
time: 1030455
state: waiting for master to send event
info: null

*************************** 2. row ***************************
id: 2
user: system user
host:
db: null
command: connect
time: 1004
state: has read all relay log; waiting for the slave
    i/o thread to update it
info: null

*************************** 3. row ***************************
id: 3112
user: replikator
host: artemis:2204
db: null
command: binlog dump
time: 2144
state: has sent all binlog to slave; waiting for binlog to be updated
info: null

*************************** 4. row ***************************
id: 3113
user: replikator
host: iconnect2:45781
db: null
command: binlog dump
time: 2086
state: has sent all binlog to slave; waiting for binlog to be updated
info: null

*************************** 5. row ***************************
id: 3123
user: stefan
host: localhost
db: apollon
command: query
time: 0
state: null
info: show full processlist
rows in set (0.00 sec)

  2) 可以通过sql语句查询数据库中相关信息的表

  select id, db, user, host, command, time, state, info from information_schema.processlist order by time desc

  3) 可以通过navicat工具查看,如下图是使用navicat查询到的截图。

  3. show processlist怎么解读

  下面对于使用该命令查询到的结果进行解读。

  id:链接mysql 服务器线程的唯一标识,可以通过kill来终止此线程的链接。

  user:当前线程链接数据库的用户

  host:显示这个语句是从哪个ip 的哪个端口上发出的。可用来追踪出问题语句的用户

  db: 线程链接的数据库,如果没有则为null

  command: 显示当前连接的执行的命令,一般就是休眠或空闲(sleep),查询(query),连接(connect)

  time: 线程处在当前状态的时间,单位是秒

  state:显示使用当前连接的sql语句的状态,很重要的列,后续会有所有的状态的描述,请注意,state只是语句执行中的某一个状态,一个 sql语句,已查询为例,可能需要经过copying to tmp table,sorting result,sending data等状态才可以完成

  info: 线程执行的sql语句,如果没有语句执行则为null。这个语句可以使客户端发来的执行语句也可以是内部执行的语句

  4. show processlist结果怎么处理

  在上面的步骤中,我们可以查到每个线程的执行时间等信息,因此针对执行时间比较长的线程,我们可以直接将其kill掉,直接执行 kill id号即可。

  如果要查时间超过5分钟的,可以拼接并执行以下sql

select concat('kill ', id, ';') from information_schema.processlist where command != 'sleep' and time > 5*60 order by time desc

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。