MySQL数据库的一次死锁实例分析
1、故事起因于2016年11月15日的一个生产bug。业务场景是:归档一个表里边的数据到历史表里边,同是删除主表记录。
2、背景场景简化如下(数据库引擎innodb,数据隔离级别rr[repeatable])
-- 创建表test1 create table test1 ( id int(11) not null auto_increment, name varchar(10) not null, primary key (id) ); insert into test1 values('hello'); -- 创建表test2 create table test2 ( id int(11) not null auto_increment, name varchar(10) not null, primary key (id) ); -- transcation 1 begin; insert into test2 select * from test1 where id = 1; delete from test1 where id = 1; -- transcation 2 begin; insert into test2 select * from test1 where id = 1;
3、具体执行顺序
transcation1 | transcation2 |
begin; — 这条sql得到test1表主键索引锁共享锁s(id=1) insert into test2 select * from test1 where id = 1; |
|
begin; — 这条sql试图获取test1表主键索引锁共享锁s(id=1),但是已经被t1占有,所以它进入锁请求队列. insert into test2 select * from test1 where id = 1; |
|
— 这条sql试图把自己拥有的test1表主键索引锁共享锁s(id=1)升级为排它锁x(id=1) — 这时t1也发起一个锁请求,这个时候mysql发现锁请求队列里边已存在一个事物t2对(id=1)的这条记录申请了s锁,死锁产生了。 delete from test1 where id = 1; |
|
死锁产生后mysql根据两个事务的权重,事务2的权重更小,被选为死锁的牺牲者,rollback。 | |
t2 rollback 之后t1成功获取了锁执行成功 |
mysql 官方解释
deadlock occurs here because client a needs an x lock to delete the row. however, that lock request cannot be granted because client b already has a request for an x lock and is waiting for client a to release its s lock. nor can the s lock held by a be upgraded to an x lock because of the prior request by b for an x lock. as a result, innodbgenerates an error for one of the clients and releases its locks. the client returns this error。
实际场景和mysql文档有些区别,文档里边要获取的是x锁。具体事例里边要获取的是s锁。
下面我们来具体的一步步分析下mysql的死锁
1、mysql常用存储引擎的锁机制
myisam和memory采用表级锁(table-level locking)
bdb采用页面锁(page-level locking)或表级锁,默认为页面锁
innodb支持行级锁(row-level locking)和表级锁,默认为行级锁
2、各种锁特点
表级锁:开销小,加锁快;不会出现死锁;锁定粒度大,发生锁冲突的概率最高,并发度最低
行级锁:开销大,加锁慢;会出现死锁;锁定粒度最小,发生锁冲突的概率最低,并发度也最高
页面锁:开销和加锁时间界于表锁和行锁之间;会出现死锁;锁定粒度界于表锁和行锁之间,并发度一般
3、各种锁的适用场景
表级锁更适合于以查询为主,只有少量按索引条件更新数据的应用,如web应用
行级锁则更适合于有大量按索引条件并发更新数据,同时又有并发查询的应用,如一些在线事务处理系统
4、死锁
是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去。
表级锁不会产生死锁。所以解决死锁主要还是针对于最常用的innodb。
5、死锁举例分析
在mysql中,行级锁并不是直接锁记录,而是锁索引。索引分为主键索引和非主键索引两种,如果一条sql语句操作了主键索引,mysql就会锁定这条主键索引;如果一条语句操作了非主键索引,mysql会先锁定该非主键索引,再锁定相关的主键索引。
在update、delete操作时,mysql不仅锁定where条件扫描过的所有索引记录,而且会锁定相邻的键值,即所谓的next-key locking。
例如,一个表db。tab_test,结构如下:
id:主键;
state:状态;
time:时间;
索引:idx_1(state,time)
出现死锁日志如下:
?***(1) transaction: ?transaction 0 677833455, active 0 sec, process no 11393, os thread id 278546 starting index read ?mysql tables in use 1, locked 1 ?lock wait 3 lock struct(s), heap size 320 ?mysql thread id 83, query id 162348740 dcnet03 dcnet searching rows for update ?update tab_test set state=1064,time=now() where state=1061 and time < date_sub(now(), interval 30 minute) (任务1的sql语句) ?***(1) waiting for this lock to be granted: (任务1等待的索引记录) ?record locks space id 0 page no 849384 n bits 208 index `primary` of table `db/tab_test` trx id 0 677833455 _mode x locks rec but not gap waiting ?record lock, heap no 92 physical record: n_fields 11; compact format; info bits 0 ?0: len 8; hex 800000000097629c; asc b ;; 1: len 6; hex 00002866eaee; asc (f ;; 2: len 7; hex 00000d40040110; asc @ ;; 3: len 8; hex 80000000000050b2; asc p ;; 4: len 8; hex 800000000000502a; asc p*;; 5: len 8; hex 8000000000005426; asc t&;; 6: len 8; hex 800012412c66d29c; asc a,f ;; 7: len 23; hex 75706c6f6164666972652e636f6d2f6 8616e642e706870; asc xxx.com/;; 8: len 8; hex 800000000000042b; asc +;; 9: len 4; hex 474bfa2b; asc gk +;; 10: len 8; hex 8000000000004e24; asc n$;; ?*** (2) transaction: ?transaction 0 677833454, active 0 sec, process no 11397, os thread id 344086 updating or deleting, thread declared inside innodb 499 ?mysql tables in use 1, locked 1 ?3 lock struct(s), heap size 320, undo log entries 1 ?mysql thread id 84, query id 162348739 dcnet03 dcnet updating update tab_test set state=1067,time=now () where id in (9921180) (任务2的sql语句) ?*** (2) holds the lock(s): (任务2已获得的锁) ?record locks space id 0 page no 849384 n bits 208 index `primary` of table `db/tab_test` trx id 0 677833454 lock_mode x locks rec but not gap ?record lock, heap no 92 physical record: n_fields 11; compact format; info bits 0 ?0: len 8; hex 800000000097629c; asc b ;; 1: len 6; hex 00002866eaee; asc (f ;; 2: len 7; hex 00000d40040110; asc @ ;; 3: len 8; hex 80000000000050b2; asc p ;; 4: len 8; hex 800000000000502a; asc p*;; 5: len 8; hex 8000000000005426; asc t&;; 6: len 8; hex 800012412c66d29c; asc a,f ;; 7: len 23; hex 75706c6f6164666972652e636f6d2f6 8616e642e706870; asc uploadfire.com/hand.php;; 8: len 8; hex 800000000000042b; asc +;; 9: len 4; hex 474bfa2b; asc gk +;; 10: len 8; hex 8000000000004e24; asc n$;; ?*** (2) waiting for this lock to be granted: (任务2等待的锁) ?record locks space id 0 page no 843102 n bits 600 index `idx_1` of table `db/tab_test` trx id 0 677833454 lock_mode x locks rec but not gap waiting ?record lock, heap no 395 physical record: n_fields 3; compact format; info bits 0 ?0: len 8; hex 8000000000000425; asc %;; 1: len 8; hex 800012412c66d29c; asc a,f ;; 2: len 8; hex 800000000097629c; asc b ;; ?*** we roll back transaction (1) ?(回滚了任务1,以解除死锁)
原因分析:
当“update tab_test set state=1064,time=now() where state=1061 and time < date_sub(now(), interval 30 minute)”
执行时,mysql会使用idx_1索引,因此首先锁定相关的索引记录,因为idx_1是非主键索引,为执行该语句,mysql还会锁定主键索引。
假设“update tab_test set state=1067,time=now () where id in (9921180)
”几乎同时执行时,本语句首先锁定主键索引,由于需要更新state的值,所以还需要锁定idx_1的某些索引记录。
这样第一条语句锁定了idx_1的记录,等待主键索引,而第二条语句则锁定了主键索引记录,而等待idx_1的记录,这样死锁就产生了。
6、解决办法
拆分第一条sql,先查出符合条件的主键值,再按照主键更新记录:
select id from tab_test where state=1061 and time < date_sub(now(), interval 30 minute); update tab_test state=1064,time=now() where id in(......);
关于mysql死锁问题的实例分析及解决方法就介绍到这里了,希望本次的介绍能够对您有所收获!
mysql 官方文档: