I have a single table that I store information that I need to batch and send out every few hours. Each row in this table corresponds to one item I need to batch. Every few hours, I do a query for all new rows where the state = 'New'
.
When I start batching these rows, I want to set the state of the rows to 'Batching', and once I've finished batching them, I want to set the state to 'Finished'.
Will the following SQL be more efficient:
UPDATE BatchTable SET state = <random number> where state = 'New';
SELECT * from BatchTable where state = <random number>;
By doing an UPDATE
first, would it avoid the situation where two threads do a SELECT and then possibly have to deal with being blocked while another thread is doing a batching/updating of the rows? If I do an UPDATE
, will it avoid blocking from other threads?
Is there an entirely different, better way to do this?