Databases 13 min read

MySQL Deadlock Investigation Record

The article documents a MySQL 5.6.35 deadlock occurring during inventory deduction, explains how Index Merge caused inconsistent lock ordering across multiple indexes, and offers solutions such as forcing specific indexes, disabling Index Merge, adding composite indexes, or restructuring the SQL to prevent the deadlock.

vivo Internet Technology
vivo Internet Technology
vivo Internet Technology
MySQL Deadlock Investigation Record

本文记录了在MySQL 5.6.35版本中遇到的死锁问题及其排查过程。死锁发生在库存扣减接口中,涉及商品库存表(store表)的死锁,具体sql如下:

update store set store = store - #{store} where sku_code = #{skuCode} and ws_code = #{wsCode} and (store - #{store}) >= 0

通过分析MySQL的加锁机制(包括主键索引、唯一索引、非唯一索引的加锁方式),发现死锁的原因是Index Merge优化导致的多个索引同时加锁顺序不一致。解决方案包括强制指定索引、禁用Index Merge、添加联合索引或优化sql结构。

本文深入探讨了MySQL索引组织表、二级索引、加锁机制等知识,为数据库开发人员提供死锁排查思路。

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

indexingdatabasedeadlockmysqllocking
vivo Internet Technology
Written by

vivo Internet Technology

Sharing practical vivo Internet technology insights and salon events, plus the latest industry news and hot conferences.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.