V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
LeeReamond
V2EX  ›  问与答

有关 mysql 主键自增锁的问题应该如何解决?

  •  
  •   LeeReamond · 2021-07-06 11:55:03 +08:00 · 830 次点击
    这是一个创建于 1017 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题,最近发现好像 mysql,innodb 主键自增表当中,如果并发插入超过一定数量就会死锁,有什么办法可以避免这点吗?

    4 条回复    2021-07-06 14:26:28 +08:00
    jj783850915
        1
    jj783850915  
       2021-07-06 12:41:23 +08:00 via Android   ❤️ 1
    锁交叉才会死锁 看你的描述应当不会出现这种情况
    3dwelcome
        2
    3dwelcome  
       2021-07-06 13:46:34 +08:00
    我以前遇到过 Java 莫名锁死的问题,百思不得其解。

    最后查下来是 OutOfMemory, 服务器上把几个数据库内存预分配调小后,就莫名消失了。
    zhangysh1995
        3
    zhangysh1995  
       2021-07-06 14:25:36 +08:00
    应该不会死锁,文档写的锁了要等待:

    AUTO-INC Locks
    An AUTO-INC lock is a special table-level lock taken by transactions inserting into tables with AUTO_INCREMENT columns. In the simplest case, if one transaction is inserting values into the table, any other transactions must wait to do their own inserts into that table, so that rows inserted by the first transaction receive consecutive primary key values.
    The innodb_autoinc_lock_mode variable controls the algorithm used for auto-increment locking. It allows you to choose how to trade off between predictable sequences of auto-increment values and maximum concurrency for insert operations.

    可以发一下详细的死锁信息大家看看。

    ps: https://dev.mysql.com/doc/refman/8.0/en/innodb-auto-increment-handling.html
    zhangysh1995
        4
    zhangysh1995  
       2021-07-06 14:26:28 +08:00
    另外,有个 mysql 专区
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5439 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 03:34 · PVG 11:34 · LAX 20:34 · JFK 23:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.