日常维护中,经常会碰到线程被阻塞,导致数据库响应非常慢,下面就看看如何获取是哪个线程导致了阻塞的。
blog地址:http://blog.gaodaima.com/hw_libo/article/details/39080809
1. 环境说明
RHEL 6.4 x86_64 + MySQL 5.6.19
事务隔离级别:RR
2. 测试过程
3. 查看锁阻塞线程信息
这里用几中方法进行分析:
3.1 使用show processlist查看
MySQL [(none)]> show processlist;+----+------+-----------+------+---------+------+--------------+------------------------------------------+| Id | User | Host | db | Command | Time | State | Info |+----+------+-----------+------+---------+------+--------------+------------------------------------------+| 2 | root | localhost | NULL | Query | 0 | init | show processlist || 3 | root | localhost | test | Query | 70 | Sending data | select count(*) from t3 a,t3 b || 4 | root | localhost | test | Query | 65 | updating | delete from emp where empno=7788 || 7 | root | localhost | test | Query | 68 | updating | update emp set sal=3500 where empno=7788 |+----+------+---<p>本文来源gao!%daima.com搞$代*!码9网(</p>--------+------+---------+------+--------------+------------------------------------------+4 rows in set (0.00 sec)
如果数据库存在较多线程的话,这种方法确实不太好确认的。
3.2 直接使用show engine innodb status查看
------------TRANSACTIONS------------Trx id counter 4131Purge done for trx's n:o < 4119 undo n:o < 0 state: running but idleHistory list length 126LIST OF TRANSACTIONS FOR EACH SESSION:---TRANSACTION 0, not startedMySQL thread id 2, OS thread handle 0x7f953ffff700, query id 115 localhost root initshow engine innodb status---TRANSACTION 4130, ACTIVE 41 sec starting index readmysql tables in use 1, locked 1LOCK WAIT 2 lock struct(s), heap size 360, 1 row lock(s)MySQL thread id 4, OS thread handle 0x7f953ff9d700, query id 112 localhost root updatingdelete from emp where empno=7788------- TRX HAS BEEN WAITING 41 SEC FOR THIS LOCK TO BE GRANTED: ## 等待了41sRECORD LOCKS space id 16 page no 3 n bits 88 index `PRIMARY` of table `test`.`emp` trx id 4130 lock_mode X locks rec but not gap waitingRecord lock, heap no 9 PHYSICAL RECORD: n_fields 10; compact format; info bits 0 ## 线程4在等待往test.emp中的主键上加X锁,page num=3 0: len 4; hex 80001e6c; asc l;; 1: len 6; hex 000000001018; asc ;; 2: len 7; hex 91000001420084; asc B ;; 3: len 5; hex 53434f5454; asc SCOTT;; 4: len 7; hex 414e414c595354; asc ANALYST;; 5: len 4; hex 80001d8e; asc ;; 6: len 4; hex 208794f0; asc ;; 7: len 4; hex 80000bb8; asc ;; 8: SQL NULL; 9: len 4; hex 80000014; asc ;;---------------------TRANSACTION 4129, ACTIVE 45 sec starting index readmysql tables in use 1, locked 1LOCK WAIT 2 lock struct(s), heap size 360, 1 row lock(s)MySQL thread id 7, OS thread handle 0x7f953ff6c700, query id 111 localhost root updatingupdate emp set sal=3500 where empno=7788------- TRX HAS BEEN WAITING 45 SEC FOR THIS LOCK TO BE GRANTED: ## 等待了45sRECORD LOCKS space id 16 page no 3 n bits 88 index `PRIMARY` of table `test`.`emp` trx id 4129 lock_mode X locks rec but not gap waitingRecord lock, heap no 9 PHYSICAL RECORD: n_fields 10; compact format; info bits 0 ## 线程7在等待往test.emp中的主键上加X锁,page num=3 0: len 4; hex 80001e6c; asc l;; 1: len 6; hex 000000001018; asc ;; 2: len 7; hex 91000001420084; asc B ;; 3: len 5; hex 53434f5454; asc SCOTT;; 4: len 7; hex 414e414c595354; asc ANALYST;; 5: len 4; hex 80001d8e; asc ;; 6: len 4; hex 208794f0; asc ;; 7: len 4; hex 80000bb8; asc ;; 8: SQL NULL; 9: len 4; hex 80000014; asc ;;---------------------TRANSACTION 4128, ACTIVE 51 sec2 lock struct(s), heap size 360, 1 row lock(s)MySQL thread id 3, OS thread handle 0x7f953ffce700, query id 110 localhost root cleaning up
我们知道,主要根因还是thread=3引起的,但从innodb status中却无法分析得到这个结果。