现象:

并发插入的时候,发现数据库主键断层并且丢失数据

查看死锁日志:

show engine innodb status

解决

看日志很清楚问题出在哪
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启动死锁检测,自动回滚数据,导致数据丢失。

日志

  1. =====================================
  2. 2021-03-31 04:02:05 0x7f00a1b9a700 INNODB MONITOR OUTPUT
  3. =====================================
  4. Per second averages calculated from the last 2 seconds
  5. -----------------
  6. BACKGROUND THREAD
  7. -----------------
  8. srv_master_thread loops: 4180 srv_active, 0 srv_shutdown, 502342 srv_idle
  9. srv_master_thread log flush and writes: 506522
  10. ----------
  11. SEMAPHORES
  12. ----------
  13. OS WAIT ARRAY INFO: reservation count 6644
  14. OS WAIT ARRAY INFO: signal count 9731
  15. RW-shared spins 0, rounds 12025, OS waits 4148
  16. RW-excl spins 0, rounds 58722, OS waits 257
  17. RW-sx spins 1044, rounds 21215, OS waits 283
  18. Spin rounds per wait: 12025.00 RW-shared, 58722.00 RW-excl, 20.32 RW-sx
  19. ------------------------
  20. LATEST DETECTED DEADLOCK
  21. ------------------------
  22. 2021-03-31 03:50:51 0x7f00a1738700
  23. *** (1) TRANSACTION:
  24. TRANSACTION 7351540, ACTIVE 0 sec inserting
  25. mysql tables in use 1, locked 1
  26. LOCK WAIT 5 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 3
  27. MySQL thread id 8087, OS thread handle 139640693270272, query id 2319676 nacos.xycmsgcc.com 10.0.0.61 sgcc update
  28. 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`)
  29. values
  30. (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')
  31. *** (1) WAITING FOR THIS LOCK TO BE GRANTED:
  32. 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
  33. Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
  34. 0: len 8; hex 73757072656d756d; asc supremum;;
  35. *** (2) TRANSACTION:
  36. TRANSACTION 7351538, ACTIVE 0 sec inserting
  37. mysql tables in use 1, locked 1
  38. 5 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 3
  39. MySQL thread id 8082, OS thread handle 139640685430528, query id 2319682 nacos.xycmsgcc.com 10.0.0.61 sgcc update
  40. 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`)
  41. values
  42. (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')
  43. *** (2) HOLDS THE LOCK(S):
  44. 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
  45. Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
  46. 0: len 8; hex 73757072656d756d; asc supremum;;
  47. *** (2) WAITING FOR THIS LOCK TO BE GRANTED:
  48. 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
  49. Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
  50. 0: len 8; hex 73757072656d756d; asc supremum;;
  51. *** WE ROLL BACK TRANSACTION (2)
  52. ------------
  53. TRANSACTIONS
  54. ------------
  55. Trx id counter 7351561
  56. Purge done for trx's n:o < 7351560 undo n:o < 0 state: running but idle
  57. History list length 6
  58. LIST OF TRANSACTIONS FOR EACH SESSION:
  59. ---TRANSACTION 421116431257696, not started
  60. 0 lock struct(s), heap size 1136, 0 row lock(s)
  61. ---TRANSACTION 421116431255872, not started
  62. 0 lock struct(s), heap size 1136, 0 row lock(s)
  63. ---TRANSACTION 421116431272288, not started
  64. 0 lock struct(s), heap size 1136, 0 row lock(s)
  65. ---TRANSACTION 421116431270464, not started
  66. 0 lock struct(s), heap size 1136, 0 row lock(s)
  67. ---TRANSACTION 421116431268640, not started
  68. 0 lock struct(s), heap size 1136, 0 row lock(s)
  69. ---TRANSACTION 421116431242192, not started
  70. 0 lock struct(s), heap size 1136, 0 row lock(s)
  71. ---TRANSACTION 421116431267728, not started
  72. 0 lock struct(s), heap size 1136, 0 row lock(s)
  73. ---TRANSACTION 421116431262256, not started
  74. 0 lock struct(s), heap size 1136, 0 row lock(s)
  75. ---TRANSACTION 421116431260432, not started
  76. 0 lock struct(s), heap size 1136, 0 row lock(s)
  77. ---TRANSACTION 421116431258608, not started
  78. 0 lock struct(s), heap size 1136, 0 row lock(s)
  79. ---TRANSACTION 421116431250400, not started
  80. 0 lock struct(s), heap size 1136, 0 row lock(s)
  81. ---TRANSACTION 421116431247664, not started
  82. 0 lock struct(s), heap size 1136, 0 row lock(s)
  83. ---TRANSACTION 421116431246752, not started
  84. 0 lock struct(s), heap size 1136, 0 row lock(s)
  85. ---TRANSACTION 421116431241280, not started
  86. 0 lock struct(s), heap size 1136, 0 row lock(s)
  87. ---TRANSACTION 421116431243104, not started
  88. 0 lock struct(s), heap size 1136, 0 row lock(s)
  89. ---TRANSACTION 421116431239456, not started
  90. 0 lock struct(s), heap size 1136, 0 row lock(s)
  91. ---TRANSACTION 421116431271376, not started
  92. 0 lock struct(s), heap size 1136, 0 row lock(s)
  93. ---TRANSACTION 421116431237632, not started
  94. 0 lock struct(s), heap size 1136, 0 row lock(s)
  95. ---TRANSACTION 421116431235808, not started
  96. 0 lock struct(s), heap size 1136, 0 row lock(s)
  97. ---TRANSACTION 421116431256784, not started
  98. 0 lock struct(s), heap size 1136, 0 row lock(s)
  99. ---TRANSACTION 421116431266816, not started
  100. 0 lock struct(s), heap size 1136, 0 row lock(s)
  101. ---TRANSACTION 421116431269552, not started
  102. 0 lock struct(s), heap size 1136, 0 row lock(s)
  103. ---TRANSACTION 421116431263168, not started
  104. 0 lock struct(s), heap size 1136, 0 row lock(s)
  105. ---TRANSACTION 421116431264992, not started
  106. 0 lock struct(s), heap size 1136, 0 row lock(s)
  107. ---TRANSACTION 421116431253136, not started
  108. 0 lock struct(s), heap size 1136, 0 row lock(s)
  109. ---TRANSACTION 421116431236720, not started
  110. 0 lock struct(s), heap size 1136, 0 row lock(s)
  111. ---TRANSACTION 421116431240368, not started
  112. 0 lock struct(s), heap size 1136, 0 row lock(s)
  113. ---TRANSACTION 421116431265904, not started
  114. 0 lock struct(s), heap size 1136, 0 row lock(s)
  115. ---TRANSACTION 421116431264080, not started
  116. 0 lock struct(s), heap size 1136, 0 row lock(s)
  117. ---TRANSACTION 421116431259520, not started
  118. 0 lock struct(s), heap size 1136, 0 row lock(s)
  119. ---TRANSACTION 421116431261344, not started
  120. 0 lock struct(s), heap size 1136, 0 row lock(s)
  121. ---TRANSACTION 421116431254048, not started
  122. 0 lock struct(s), heap size 1136, 0 row lock(s)
  123. ---TRANSACTION 421116431251312, not started
  124. 0 lock struct(s), heap size 1136, 0 row lock(s)
  125. ---TRANSACTION 421116431248576, not started
  126. 0 lock struct(s), heap size 1136, 0 row lock(s)
  127. ---TRANSACTION 421116431245840, not started
  128. 0 lock struct(s), heap size 1136, 0 row lock(s)
  129. ---TRANSACTION 421116431254960, not started
  130. 0 lock struct(s), heap size 1136, 0 row lock(s)
  131. ---TRANSACTION 421116431244016, not started
  132. 0 lock struct(s), heap size 1136, 0 row lock(s)
  133. ---TRANSACTION 421116431238544, not started
  134. 0 lock struct(s), heap size 1136, 0 row lock(s)
  135. ---TRANSACTION 421116431244928, not started
  136. 0 lock struct(s), heap size 1136, 0 row lock(s)
  137. ---TRANSACTION 421116431252224, not started
  138. 0 lock struct(s), heap size 1136, 0 row lock(s)
  139. ---TRANSACTION 421116431249488, not started
  140. 0 lock struct(s), heap size 1136, 0 row lock(s)
  141. ---TRANSACTION 421116431234896, not started
  142. 0 lock struct(s), heap size 1136, 0 row lock(s)
  143. --------
  144. FILE I/O
  145. --------
  146. I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
  147. I/O thread 1 state: waiting for completed aio requests (log thread)
  148. I/O thread 2 state: waiting for completed aio requests (read thread)
  149. I/O thread 3 state: waiting for completed aio requests (read thread)
  150. I/O thread 4 state: waiting for completed aio requests (read thread)
  151. I/O thread 5 state: waiting for completed aio requests (read thread)
  152. I/O thread 6 state: waiting for completed aio requests (write thread)
  153. I/O thread 7 state: waiting for completed aio requests (write thread)
  154. I/O thread 8 state: waiting for completed aio requests (write thread)
  155. I/O thread 9 state: waiting for completed aio requests (write thread)
  156. Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] ,
  157. ibuf aio reads:, log i/o's:, sync i/o's:
  158. Pending flushes (fsync) log: 0; buffer pool: 0
  159. 5720 OS file reads, 71589 OS file writes, 33717 OS fsyncs
  160. 0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
  161. -------------------------------------
  162. INSERT BUFFER AND ADAPTIVE HASH INDEX
  163. -------------------------------------
  164. Ibuf: size 1, free list len 0, seg size 2, 31 merges
  165. merged operations:
  166. insert 33, delete mark 1, delete 0
  167. discarded operations:
  168. insert 0, delete mark 0, delete 0
  169. Hash table size 34679, node heap has 32 buffer(s)
  170. Hash table size 34679, node heap has 4 buffer(s)
  171. Hash table size 34679, node heap has 16 buffer(s)
  172. Hash table size 34679, node heap has 19 buffer(s)
  173. Hash table size 34679, node heap has 11 buffer(s)
  174. Hash table size 34679, node heap has 23 buffer(s)
  175. Hash table size 34679, node heap has 72 buffer(s)
  176. Hash table size 34679, node heap has 40 buffer(s)
  177. 0.00 hash searches/s, 0.00 non-hash searches/s
  178. ---
  179. LOG
  180. ---
  181. Log sequence number 5125706096
  182. Log flushed up to 5125706096
  183. Pages flushed up to 5125706096
  184. Last checkpoint at 5125706087
  185. 0 pending log flushes, 0 pending chkp writes
  186. 24010 log i/o's done, 0.00 log i/o's/second
  187. ----------------------
  188. BUFFER POOL AND MEMORY
  189. ----------------------
  190. Total large memory allocated 137428992
  191. Dictionary memory allocated 3632821
  192. Buffer pool size 8192
  193. Free buffers 1496
  194. Database pages 6479
  195. Old database pages 2379
  196. Modified db pages 3
  197. Pending reads 0
  198. Pending writes: LRU 0, flush list 0, single page 0
  199. Pages made young 22, not young 0
  200. 0.00 youngs/s, 0.00 non-youngs/s
  201. Pages read 5175, created 1304, written 44850
  202. 0.00 reads/s, 0.00 creates/s, 0.00 writes/s
  203. Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
  204. Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
  205. LRU len: 6479, unzip_LRU len: 0
  206. I/O sum[0]:cur[0], unzip sum[0]:cur[0]
  207. --------------
  208. ROW OPERATIONS
  209. --------------
  210. 0 queries inside InnoDB, 0 queries in queue
  211. 0 read views open inside InnoDB
  212. Process ID=14923, Main thread ID=139641154496256, state: sleeping
  213. Number of rows inserted 1011045, updated 23161, deleted 100224, read 5308941008
  214. 0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 22133.93 reads/s
  215. ----------------------------
  216. END OF INNODB MONITOR OUTPUT
  217. ============================

[1]. Mysql死锁如何排查:insert on duplicate死锁一次排查分析过程. [OL]https://www.cnblogs.com/jay-huaxiao/p/11456921.html