MySQL锁阻塞分析(一)

2014-11-24 11:21:44 · 作者: · 浏览: 2

日常维护中,经常会碰到线程被阻塞,导致数据库响应非常慢,下面就看看如何获取是哪个线程导致了阻塞的。

blog地址:http://blog.csdn.net/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 |
+----+------+-----------+------+---------+------+--------------+------------------------------------------+
4 rows in set (0.00 sec)
如果 数据库存在较多线程的话,这种方法确实不太好确认的。

3.2 直接使用show engine innodb status查看

------------
TRANSACTIONS
------------
Trx id counter 4131
Purge done for trx's n:o < 4119 undo n:o < 0 state: running but idle
History list length 126
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0, not started
MySQL thread id 2, OS thread handle 0x7f953ffff700, query id 115 localhost root init
show engine innodb status
---TRANSACTION 4130, ACTIVE 41 sec starting index read
mysql tables in use 1, locked 1
LOCK 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 updating
delete from emp where empno=7788
------- TRX HAS BEEN WAITING 41 SEC FOR THIS LOCK TO BE GRANTED:   ## 等待了41s
RECORD 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 waiting
Record 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 read
mysql tables in use 1, locked 1
LOCK 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 updating
update emp set sal=3500 where empno=7788
------- TRX HAS BEEN WAITING 45 SEC FOR THIS LOCK TO BE GRANTED:   ## 等待了45s
RECORD 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 waiting
Record 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 sec
2 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中却无法分析得到这个结果。

从上面来看,线程4和线程7都在等待往test.emp中的主键上加X锁,page num=3,但是线程7等待的时间为45s,而线程4等待的时间为41s,是较线程7之后申请的锁,所以可以判断是线程7阻塞了线程4。至于线程7为什么出现等待,这里分析不到根因。

3.3 使用mysqladmin debug查看

# mysqladmin -S /tmp/mys