MySQL5.6在按主键选择不同行时选择更新时死锁

of1yzvn4  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(272)

我很难弄清楚为什么会出现这种僵局。有人能告诉我怎么避免吗?为什么?
据我所见,有2个事务正在执行(id是主键):

T1: SELECT id FROM table WHERE id IN (1,2) FOR UPDATE
T2: SELECT id FROM table WHERE id IN (3,4) FOR UPDATE

我使用这个select来锁定稍后在事务中更新的行。如您所见,它们都选择了不同的行,但死锁仍然存在。这是预期的行为吗?如果是,为什么?我不明白。它甚至没有说缝隙锁正在发生。
这是我们的垃圾场 SHOW ENGINE INNODB STATUS ```
LATEST DETECTED DEADLOCK

2020-08-06 18:33:43 2ae26b344700

***(1) TRANSACTION:

TRANSACTION 234270104596, ACTIVE 0 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 10 lock struct(s), heap size 2936, 29 row lock(s), undo log entries 15
MySQL thread id 942898994, OS thread handle 0x2ae261fc6700, query id 74391216578 192.31.24.111 db Sending data
SELECT id_customer FROM sn_user_quality WHERE id_customer IN (3574634,1892448,4157953,4191571) FOR UPDATE

***(1) WAITING FOR THIS LOCK TO BE GRANTED:

RECORD LOCKS space id 2045 page no 3 n bits 352 index PRIMARY of table db.sn_user_quality trx id 234270104596 lock_mode X waiting
Record lock, heap no 283 PHYSICAL RECORD: n_fields 9; compact format; info bits 0
0: len 4; hex 003f7201; asc ?r ;;
1: len 2; hex 0000; asc ;;
2: len 1; hex 00; asc ;;
3: len 6; hex 00368b961c3a; asc 6 :;;
4: len 7; hex 690001000a257a; asc i %z;;
5: len 4; hex 800012c0; asc ;;
6: len 4; hex 80001bbc; asc ;;
7: len 5; hex 99a70cf26d; asc m;;
8: len 5; hex 99a70d486b; asc Hk;;

***(2) TRANSACTION:

TRANSACTION 234270104634, ACTIVE 0 sec starting index read
mysql tables in use 1, locked 1
11 lock struct(s), heap size 2936, 22 row lock(s), undo log entries 15
MySQL thread id 942898997, OS thread handle 0x2ae26b344700, query id 74391216598 192.31.24.111 db Sending data
SELECT id_customer FROM sn_user_quality WHERE id_customer IN (3331279,4204817,765650) FOR UPDATE

***(2) HOLDS THE LOCK(S):

RECORD LOCKS space id 2045 page no 3 n bits 352 index PRIMARY of table db.sn_user_quality trx id 234270104634 lock_mode X
Record lock, heap no 66 PHYSICAL RECORD: n_fields 9; compact format; info bits 0
0: len 4; hex 000baed2; asc ;;
1: len 2; hex 0000; asc ;;
2: len 1; hex 00; asc ;;
3: len 6; hex 00368b95f3b2; asc 6 ;;
4: len 7; hex 790001403f26c5; asc y @?& ;;
5: len 4; hex 80001518; asc ;;
6: len 4; hex 80001b58; asc X;;
7: len 5; hex 99a6f16688; asc f ;;
8: len 5; hex 99a70d4867; asc Hg;;

Record lock, heap no 67 PHYSICAL RECORD: n_fields 9; compact format; info bits 0
0: len 4; hex 000baed2; asc ;;
1: len 2; hex 07e4; asc ;;
2: len 1; hex 07; asc ;;
3: len 6; hex 00362608019e; asc 6& ;;
4: len 7; hex 0300000179041c; asc y ;;
5: len 4; hex 80000b54; asc T;;
6: len 4; hex 80001ce8; asc ;;
7: len 5; hex 99a6f16688; asc f ;;
8: len 5; hex 99a6ff7e50; asc ~P;;

Record lock, heap no 169 PHYSICAL RECORD: n_fields 9; compact format; info bits 0
0: len 4; hex 000baed2; asc ;;
1: len 2; hex 07e4; asc ;;
2: len 1; hex 08; asc ;;
3: len 6; hex 00368b95f3b2; asc 6 ;;
4: len 7; hex 790001403f26e6; asc y @?& ;;
5: len 4; hex 80001518; asc ;;
6: len 4; hex 80001b58; asc X;;
7: len 5; hex 99a7020088; asc ;;
8: len 5; hex 99a70d4867; asc Hg;;

Record lock, heap no 271 PHYSICAL RECORD: n_fields 9; compact format; info bits 0
0: len 4; hex 004021a0; asc @! ;;
1: len 2; hex 0000; asc ;;
2: len 1; hex 00; asc ;;
3: len 6; hex 00368b961c3a; asc 6 :;;
4: len 7; hex 690001000a252a; asc i %*;;
5: len 4; hex 80001324; asc $;;
6: len 4; hex 80001b58; asc X;;
7: len 5; hex 99a709450e; asc E ;;
8: len 5; hex 99a70d486b; asc Hk;;

Record lock, heap no 272 PHYSICAL RECORD: n_fields 9; compact format; info bits 0
0: len 4; hex 004021a0; asc @! ;;
1: len 2; hex 07e4; asc ;;
2: len 1; hex 08; asc ;;
3: len 6; hex 00368b961c3a; asc 6 :;;
4: len 7; hex 690001000a2552; asc i %R;;
5: len 4; hex 80001324; asc $;;
6: len 4; hex 80001b58; asc X;;
7: len 5; hex 99a709450e; asc E ;;
8: len 5; hex 99a70d486b; asc Hk;;

Record lock, heap no 283 PHYSICAL RECORD: n_fields 9; compact format; info bits 0
0: len 4; hex 003f7201; asc ?r ;;
1: len 2; hex 0000; asc ;;
2: len 1; hex 00; asc ;;
3: len 6; hex 00368b961c3a; asc 6 :;;
4: len 7; hex 690001000a257a; asc i %z;;
5: len 4; hex 800012c0; asc ;;
6: len 4; hex 80001bbc; asc ;;
7: len 5; hex 99a70cf26d; asc m;;
8: len 5; hex 99a70d486b; asc Hk;;

Record lock, heap no 284 PHYSICAL RECORD: n_fields 9; compact format; info bits 0
0: len 4; hex 003f7201; asc ?r ;;
1: len 2; hex 07e4; asc ;;
2: len 1; hex 08; asc ;;
3: len 6; hex 00368b961c3a; asc 6 :;;
4: len 7; hex 690001000a25a1; asc i % ;;
5: len 4; hex 800012c0; asc ;;
6: len 4; hex 80001bbc; asc ;;
7: len 5; hex 99a70cf26d; asc m;;
8: len 5; hex 99a70d486b; asc Hk;;

***(2) WAITING FOR THIS LOCK TO BE GRANTED:

RECORD LOCKS space id 2045 page no 3 n bits 352 index PRIMARY of table db.sn_user_quality trx id 234270104634 lock_mode X waiting
Record lock, heap no 38 PHYSICAL RECORD: n_fields 9; compact format; info bits 0
0: len 4; hex 0032d4cf; asc 2 ;;
1: len 2; hex 0000; asc ;;
2: len 1; hex 00; asc ;;
3: len 6; hex 00368b961c14; asc 6 ;;
4: len 7; hex 67000001ad0c2b; asc g +;;
5: len 4; hex 8000157c; asc |;;
6: len 4; hex 80001a2c; asc ,;;
7: len 5; hex 99a6ef6840; asc h@;;
8: len 5; hex 99a70d486b; asc Hk;;

***WE ROLL BACK TRANSACTION (1)

table是空的

CREATE TABLE sn_user_quality (
id_customer int(10) unsigned NOT NULL,
year smallint(5) unsigned NOT NULL,
month tinyint(3) unsigned NOT NULL,
tag_quality decimal(8,4) NOT NULL DEFAULT '0.5000',
tag_rating_quality decimal(8,4) NOT NULL DEFAULT '0.5000',
date_add datetime NOT NULL,
date_upd datetime NOT NULL,
PRIMARY KEY (id_customer,year,month),
KEY year_month (year,month),
CONSTRAINT sn_user_quality_ibfk_1 FOREIGN KEY (id_customer) REFERENCES sn_profile (id_customer) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题