I am trying to create an SQL event that updates n rows every 5 minutes in a table where id between 1 and 100. However I would like the query not to repeat the same rows on each update; for example if I update rows 1 to 10, the next update will be different e.g rows 29 to 39 etc. This is what I have but I do not know how to select random ranges to update e.g 1-10, 12-22 etc. How can I achieve this?
UPDATE table
SET timer = CURRENT_TIMESTAMP - INTERVAL FLOOR(RAND() * 245) SECOND
WHERE id > 1 AND id < 12;
2
Answers
Add a column with the last update time of the row, and check that the time since the last update is more than a threshold.
You could make the range of ids a function of the time.
This splits the hour into 12 approximately equal ID ranges, and updates a different range every 5 minutes.
use a transaction and a lock to update non-overlapping ranges of IDs, while also checking for conflicts with other updates: