Ignoring locked row in a MySQL query

前端 未结 2 1795
暗喜
暗喜 2020-12-10 07:26

I have one table that is read at the same time by different threads.

Each thread must select 100 rows, execute some tasks on each row (unrelated to the database) th

2条回答
  •  失恋的感觉
    2020-12-10 08:20

    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.

    Hope this helps.

提交回复
热议问题