问题描述
我有一个由不同线程同时读取的表.
I have one table that is read at the same time by different threads.
每个线程必须选择100行,在每行上执行一些任务(与数据库无关),然后它们必须从表中删除选定的行.
Each thread must select 100 rows, execute some tasks on each row (unrelated to the database) then they must delete the selected row from the table.
使用以下查询选择行:
SELECT id FROM table_name FOR UPDATE;
我的问题是:如何忽略(或跳过)以前使用MySQL中的select语句锁定的行?
My question is: How can I ignore (or skip) rows that were previously locked using a select statement in MySQL ?
推荐答案
我通常创建一个默认为NULL的process_id列,然后让每个线程使用唯一的标识符来执行以下操作:
I typically create a process_id column that is default NULL and then have each thread use a unique identifier to do the following:
UPDATE table_name SET process_id = #{process.id} WHERE process_id IS NULL LIMIT 100;
SELECT id FROM table_name WHERE process_id = #{process.id} FOR UPDATE;
这可以确保每个线程从表中选择唯一的行集.
That ensures that each thread selects a unique set of rows from the table.
希望这会有所帮助.
这篇关于忽略MySQL查询中的锁定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!