收到报警某台mysql数据库慢查询数量超过5,登录上去看,发现阻塞的SQL全部是update,处于Updating状态
+---------+------+-----------+------+---------+------+----------+------------------------------------+ | ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO | +---------+------+-----------+------+---------+------+----------+------------------------------------+ | 4364109 | root | localhost | test | Query | 39 | Updating | update T_1 set col3='' where FID=3 | +---------+------+-----------+------+---------+------+----------+------------------------------------+
查看系统状态,各项都正常,但在InnoDB Row Lock Time值上有个较大的毛刺,基本得出是锁等待导致的。show processlist看到的线程里面,活动状态的没有发现执行慢的SQL,都是通过主键update,极有可能是某个事务对这个表做了dml操作没有提交commit,导致一直持有锁未释放,后面的session等待X锁。根据这个思路,在show engine innodb status的事务栏里面找到ACTIVE状态的,执行时间最长的那个session,发现是sleep状态的,kill掉这个session,阻塞现象消失。
---TRANSACTION 21427F91, ACTIVE 1421 sec, process no 69177, OS thread id 139633152562944 2 lock struct(s), heap size 376, 1 row lock(s), undo log entries 1 MySQL thread id 4364618, query id 282683146 localhost root
至于为什么会出现未提交SQL,再和开发一起去排查
通过show engine innodb status去查session比较麻烦,简单的SQL如下
mysql> select a.trx_mysql_thread_id,b.TIME from information_schema.INNODB_TRX a inner join information_schema.processlist b on a.trx_mysql_thread_id=b.id where a.trx_state='RUNNING' and b.time>10 and b.COMMAND='Sleep'; +---------------------+------+ | trx_mysql_thread_id | TIME | +---------------------+------+ | 4364618 | 614 | +---------------------+------+ 1 row in set (0.00 sec)
事务ID=4364618,事务时长:614秒。