V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
BBCCBB
V2EX  ›  程序员

[MySQL] 如何在 mysql5.7 版本实现 8.0 版本的 for update skip locked 功能?

  •  
  •   BBCCBB · Mar 16, 2021 · 1644 views
    This topic created in 1875 days ago, the information mentioned may be changed or developed.

    万能的 v 友, 看到 mysql8 版本下加了针对 lock 的 no wait 和 skip locked 功能..

    比如:

    create table A (
      id int primariy key;
    )
    
    insert into A values (1), (2), (3);
    
    

    现在有两个线程 t1, t2.

    t1 执行

    select * from A where id > 0 for update limit 1;
    

    t2 执行

    select * from A where id > 0 for update limit 1;
    

    现在 t2 会在 id=1 的地方等待 t1 释放 id=1 的锁. 而如果 t2 改成这样

    select * from A where id > 0 for update skip locked limit 1;
    

    那么 t2 会跳过 id=1 转而针对 id=2 加锁.

    但是这个功能是 8.0.1 才加上的

    请问大佬们在 mysql5.7 如何实现这种功能呢? 或者说在没有skip locked feature 的数据库上有没有可能通过设计上来实现这个功能呢?

    4 replies    2021-03-17 15:08:24 +08:00
    BBCCBB
        1
    BBCCBB  
    OP
       Mar 16, 2021
    🐶
    killergun
        2
    killergun  
       Mar 17, 2021
    改源码
    BBCCBB
        3
    BBCCBB  
    OP
       Mar 17, 2021
    @killergun 你🐮🍺, 还有其他办法吗
    BBCCBB
        4
    BBCCBB  
    OP
       Mar 17, 2021
    捞捞, 没办法就算了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2874 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 06:23 · PVG 14:23 · LAX 23:23 · JFK 02:23
    ♥ Do have faith in what you're doing.