且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

仅选择mysql的未锁定行

更新时间:2023-02-07 18:26:53

MySQL无法忽略SELECT中的锁定行.您将必须找到另一种方式来将行预留为已处理".

MySQL does not have a way to ignore locked rows in a SELECT. You'll have to find a different way to set a row aside as "already processed".

最简单的方法是在第一个查询中短暂地锁定该行,以仅将其标记为已处理",然后将其解锁并再次锁定以进行其余处理-第二个查询将等待短的标记"查询完成,您可以添加一个显式的WHERE条件以忽略已标记的行.如果您不希望第一个操作能够成功完成,则可能需要增加时间戳的复杂性,以便在失败的操作后进行清除.

The simplest way is to lock the row briefly in the first query just to mark it as "already processed", then unlock it and lock it again for the rest of the processing - the second query will wait for the short "marker" query to complete, and you can add an explicit WHERE condition to ignore already-marked rows. If you don't want to rely on the first operation being able to complete successfully, you may need to add a bit more complexity with timestamps and such to clean up after those failed operations.