Mysql Innodb更新主键和辅助索引上的锁顺序
当我研究由两个更新查询引起的死锁时。有一点我不能理解。 1.行锁定设置的顺序? 2.执行更新时,主索引和辅助索引上的锁定设置顺序? 3.SHOW INNODB STATUS SHOW WAITING x lock structs,是同时需要还是在授予其他锁之后需要一个锁? 4.如果一个锁结构想要锁定一些行,那么进程是原子的吗 这是我的僵局: 表: 更新查询1:Mysql Innodb更新主键和辅助索引上的锁顺序,mysql,innodb,rowlocking,Mysql,Innodb,Rowlocking,当我研究由两个更新查询引起的死锁时。有一点我不能理解。 1.行锁定设置的顺序? 2.执行更新时,主索引和辅助索引上的锁定设置顺序? 3.SHOW INNODB STATUS SHOW WAITING x lock structs,是同时需要还是在授予其他锁之后需要一个锁? 4.如果一个锁结构想要锁定一些行,那么进程是原子的吗 这是我的僵局: 表: 更新查询1: update study_update_deadlock set version=version+1 where u_key=16635
update study_update_deadlock set version=version+1 where u_key=1663577608119220637 and nu_key=12498159
更新查询2:
update study_update_deadlock set quantity=quantity+1 where u_key=1470344318505049187 and nu_key=12498159
数据库中的某些行
场景的关键点:
2016-06-29 18:58:32 700001f3b000
*** (1) TRANSACTION:
TRANSACTION 76027, ACTIVE 0 sec fetching rows
mysql tables in use 3, locked 3
LOCK WAIT 5 lock struct(s), heap size 1184, 8 row lock(s)
MySQL thread id 33311, OS thread handle 0x700001ab7000, query id 204129 localhost root updating
update study_update_deadlock set quantity = quantity+1 where u_key= 1470344318505049187 and nu_key=12498159
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 16 page no 22255 n bits 392 index `PRIMARY` of table `test`.`study_update_deadlock` trx id 76027 lock_mode X locks rec but not gap waiting
Record lock, heap no 255 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
0: len 4; hex 8036731e; asc 6s ;;
1: len 6; hex 0000000128fa; asc ( ;;
2: len 7; hex 770000179e081e; asc w ;;
3: len 8; hex 97163705443d799d; asc 7 D=y ;;
4: len 8; hex 8000000000beb4ef; asc ;;
5: len 4; hex 800000bb; asc ;;
6: len 4; hex 8000005a; asc Z;;
*** (2) TRANSACTION:
TRANSACTION 76028, ACTIVE 0 sec starting index read
mysql tables in use 3, locked 3
4 lock struct(s), heap size 1184, 3 row lock(s)
MySQL thread id 33312, OS thread handle 0x700001f3b000, query id 204130 localhost root updating
update study_update_deadlock set version = version+1 where u_key= 1663577608119220637 and nu_key=12498159
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 16 page no 22255 n bits 392 index `PRIMARY` of table `test`.`study_update_deadlock` trx id 76028 lock_mode X locks rec but not gap
Record lock, heap no 255 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
0: len 4; hex 8036731e; asc 6s ;;
1: len 6; hex 0000000128fa; asc ( ;;
2: len 7; hex 770000179e081e; asc w ;;
3: len 8; hex 97163705443d799d; asc 7 D=y ;;
4: len 8; hex 8000000000beb4ef; asc ;;
5: len 4; hex 800000bb; asc ;;
6: len 4; hex 8000005a; asc Z;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 16 page no 22336 n bits 952 index `nu_key` of table `test`.`study_update_deadlock` trx id 76028 lock_mode X waiting
Record lock, heap no 660 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 8; hex 8000000000beb4ef; asc ;;
1: len 4; hex 8036731c; asc 6s ;;
*** WE ROLL BACK TRANSACTION (2)
这是由间隙锁和记录锁引起的。
mysql的GDB dubug源代码可以发现,mysql内部文档有一些关键点,但丢失了一些细节。这是由间隙锁和记录锁造成的。
mysql的GDB dubug源代码可以找到,mysql内部文档有一些关键点,但丢失了一些细节。(我知道这并不能解决您的问题。)
索引(u\u键,nu\u键)
,按任意顺序,都应该可以消除问题。@RickJames Thx!多列索引可能会解决这个问题,但我想要的是在哪里可以找到一些关于锁设置的文档!您是否在运行时关闭了自动提交功能?你有一个开始
,但没有提交
(还没有)?@RickJames Thx!我编写了一个python脚本,在for循环中运行update1和update2;看看这是否能消除这个问题。然后了解“事务”,并分析应用程序,以确定“事务”中可能需要哪些查询组合。(我知道这并不能解决您的问题。)索引(u\u键,nu\u键)
,按任意顺序,都可以消除问题。@RickJames Thx!多列索引可能会解决这个问题,但我想要的是在哪里可以找到一些关于锁设置的文档!您是否在运行时关闭了自动提交功能?你有一个开始
,但没有提交
(还没有)?@RickJames Thx!我编写了一个python脚本,在for循环中运行update1和update2;看看这是否能消除这个问题。然后了解“事务”,并分析应用程序,以确定“事务”中可能需要哪些查询组合。
2016-06-29 18:58:32 700001f3b000
*** (1) TRANSACTION:
TRANSACTION 76027, ACTIVE 0 sec fetching rows
mysql tables in use 3, locked 3
LOCK WAIT 5 lock struct(s), heap size 1184, 8 row lock(s)
MySQL thread id 33311, OS thread handle 0x700001ab7000, query id 204129 localhost root updating
update study_update_deadlock set quantity = quantity+1 where u_key= 1470344318505049187 and nu_key=12498159
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 16 page no 22255 n bits 392 index `PRIMARY` of table `test`.`study_update_deadlock` trx id 76027 lock_mode X locks rec but not gap waiting
Record lock, heap no 255 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
0: len 4; hex 8036731e; asc 6s ;;
1: len 6; hex 0000000128fa; asc ( ;;
2: len 7; hex 770000179e081e; asc w ;;
3: len 8; hex 97163705443d799d; asc 7 D=y ;;
4: len 8; hex 8000000000beb4ef; asc ;;
5: len 4; hex 800000bb; asc ;;
6: len 4; hex 8000005a; asc Z;;
*** (2) TRANSACTION:
TRANSACTION 76028, ACTIVE 0 sec starting index read
mysql tables in use 3, locked 3
4 lock struct(s), heap size 1184, 3 row lock(s)
MySQL thread id 33312, OS thread handle 0x700001f3b000, query id 204130 localhost root updating
update study_update_deadlock set version = version+1 where u_key= 1663577608119220637 and nu_key=12498159
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 16 page no 22255 n bits 392 index `PRIMARY` of table `test`.`study_update_deadlock` trx id 76028 lock_mode X locks rec but not gap
Record lock, heap no 255 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
0: len 4; hex 8036731e; asc 6s ;;
1: len 6; hex 0000000128fa; asc ( ;;
2: len 7; hex 770000179e081e; asc w ;;
3: len 8; hex 97163705443d799d; asc 7 D=y ;;
4: len 8; hex 8000000000beb4ef; asc ;;
5: len 4; hex 800000bb; asc ;;
6: len 4; hex 8000005a; asc Z;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 16 page no 22336 n bits 952 index `nu_key` of table `test`.`study_update_deadlock` trx id 76028 lock_mode X waiting
Record lock, heap no 660 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 8; hex 8000000000beb4ef; asc ;;
1: len 4; hex 8036731c; asc 6s ;;
*** WE ROLL BACK TRANSACTION (2)