现象:
查看死锁日志:
解决
看日志很清楚问题出在哪
RECORD LOCKS space id 1373 page no 33 n bits 592 index idx_materialPersonalId of table sgcc_material
.material_personal_transcode
trx id 7351540 lock_mode X insert intention waiting
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
0: len 8; hex 73757072656d756d; asc supremum;;
死锁的索引为material_personal_transcode表的idx_materialPersonalId。加锁范围是hex(行号)73757072656d756d 到 asc supremum(正无穷)
所以删除索引即可
原因:
该表维护了个人素材id的索引——materialPersonalId,并且mysql为rr级别,那么会存在间隙锁;并发的过程中,insert首先会锁定间隙锁,然后插入之前加上插入意向锁;t1和t2的间隙锁是几乎重合的,此时t1的插入意向锁被t2的间隙锁阻塞了,t2的插入意向锁也被t1阻塞了,t2启动死锁检测,自动回滚数据,导致数据丢失。
日志
=====================================
2021-03-31 04:02:05 0x7f00a1b9a700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 2 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 4180 srv_active, 0 srv_shutdown, 502342 srv_idle
srv_master_thread log flush and writes: 506522
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 6644
OS WAIT ARRAY INFO: signal count 9731
RW-shared spins 0, rounds 12025, OS waits 4148
RW-excl spins 0, rounds 58722, OS waits 257
RW-sx spins 1044, rounds 21215, OS waits 283
Spin rounds per wait: 12025.00 RW-shared, 58722.00 RW-excl, 20.32 RW-sx
------------------------
LATEST DETECTED DEADLOCK
------------------------
2021-03-31 03:50:51 0x7f00a1738700
*** (1) TRANSACTION:
TRANSACTION 7351540, ACTIVE 0 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 5 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 3
MySQL thread id 8087, OS thread handle 139640693270272, query id 2319676 nacos.xycmsgcc.com 10.0.0.61 sgcc update
insert into `material_personal_transcode`(`material_personal_id`, `transcode_task_id`, `status`, `url`, `full_url`, `material_size`, `quality`, `used`, `deleted`, `source_type`, `subscribe_id`, `tenant_id`, `create_time`, `modify_time`)
values
(45873, '5947b3ec3ee7436b8eb149deca455d4a', 2, 'material/transcode/video/2021/03/31/hjkkXK4H_1617177051505.mp4', '', 0, 3, 0, 0, 1, 0, 1, '2021-03-31 15:50:51.633', '2021-03-31 15:50:51.633')
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 1373 page no 33 n bits 592 index idx_materialPersonalId of table `sgcc_material`.`material_personal_transcode` trx id 7351540 lock_mode X insert intention waiting
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
0: len 8; hex 73757072656d756d; asc supremum;;
*** (2) TRANSACTION:
TRANSACTION 7351538, ACTIVE 0 sec inserting
mysql tables in use 1, locked 1
5 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 3
MySQL thread id 8082, OS thread handle 139640685430528, query id 2319682 nacos.xycmsgcc.com 10.0.0.61 sgcc update
insert into `material_personal_transcode`(`material_personal_id`, `transcode_task_id`, `status`, `url`, `full_url`, `material_size`, `quality`, `used`, `deleted`, `source_type`, `subscribe_id`, `tenant_id`, `create_time`, `modify_time`)
values
(45872, '5879910ba32f460485211d7e8ff7e119', 2, 'material/transcode/video/2021/03/31/Gllys1Xy_1617177051435.mp4', '', 0, 3, 0, 0, 1, 0, 1, '2021-03-31 15:50:51.709', '2021-03-31 15:50:51.709')
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 1373 page no 33 n bits 592 index idx_materialPersonalId of table `sgcc_material`.`material_personal_transcode` trx id 7351538 lock_mode X
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
0: len 8; hex 73757072656d756d; asc supremum;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 1373 page no 33 n bits 592 index idx_materialPersonalId of table `sgcc_material`.`material_personal_transcode` trx id 7351538 lock_mode X insert intention waiting
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
0: len 8; hex 73757072656d756d; asc supremum;;
*** WE ROLL BACK TRANSACTION (2)
------------
TRANSACTIONS
------------
Trx id counter 7351561
Purge done for trx's n:o < 7351560 undo n:o < 0 state: running but idle
History list length 6
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 421116431257696, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431255872, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431272288, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431270464, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431268640, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431242192, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431267728, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431262256, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431260432, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431258608, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431250400, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431247664, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431246752, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431241280, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431243104, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431239456, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431271376, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431237632, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431235808, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431256784, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431266816, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431269552, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431263168, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431264992, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431253136, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431236720, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431240368, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431265904, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431264080, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431259520, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431261344, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431254048, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431251312, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431248576, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431245840, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431254960, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431244016, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431238544, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431244928, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431252224, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431249488, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421116431234896, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
--------
FILE I/O
--------
I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
I/O thread 1 state: waiting for completed aio requests (log thread)
I/O thread 2 state: waiting for completed aio requests (read thread)
I/O thread 3 state: waiting for completed aio requests (read thread)
I/O thread 4 state: waiting for completed aio requests (read thread)
I/O thread 5 state: waiting for completed aio requests (read thread)
I/O thread 6 state: waiting for completed aio requests (write thread)
I/O thread 7 state: waiting for completed aio requests (write thread)
I/O thread 8 state: waiting for completed aio requests (write thread)
I/O thread 9 state: waiting for completed aio requests (write thread)
Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] ,
ibuf aio reads:, log i/o's:, sync i/o's:
Pending flushes (fsync) log: 0; buffer pool: 0
5720 OS file reads, 71589 OS file writes, 33717 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 31 merges
merged operations:
insert 33, delete mark 1, delete 0
discarded operations:
insert 0, delete mark 0, delete 0
Hash table size 34679, node heap has 32 buffer(s)
Hash table size 34679, node heap has 4 buffer(s)
Hash table size 34679, node heap has 16 buffer(s)
Hash table size 34679, node heap has 19 buffer(s)
Hash table size 34679, node heap has 11 buffer(s)
Hash table size 34679, node heap has 23 buffer(s)
Hash table size 34679, node heap has 72 buffer(s)
Hash table size 34679, node heap has 40 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 5125706096
Log flushed up to 5125706096
Pages flushed up to 5125706096
Last checkpoint at 5125706087
0 pending log flushes, 0 pending chkp writes
24010 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 137428992
Dictionary memory allocated 3632821
Buffer pool size 8192
Free buffers 1496
Database pages 6479
Old database pages 2379
Modified db pages 3
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 22, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 5175, created 1304, written 44850
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 6479, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Process ID=14923, Main thread ID=139641154496256, state: sleeping
Number of rows inserted 1011045, updated 23161, deleted 100224, read 5308941008
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 22133.93 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================
[1]. Mysql死锁如何排查:insert on duplicate死锁一次排查分析过程. [OL]https://www.cnblogs.com/jay-huaxiao/p/11456921.html