简体   繁体   中英

How to handle parallel bulk update requests in SQL Server?

Let's say I have a table storing tasks, which can have a state like 'to do', 'processing' and 'done'. As I have multiple clients taking care of processing those tasks, I want to make sure that there's just one client grabbing a certain task to process it.

In order to avoid too many database requests a client gets multiple (10,100,...) tasks to be processed in one request.

What's the best way to acheive this in SQL Server?

Can I somehow select tasks to be done and update them at the same time to mark them as 'processing'?

Let's assume your table for storing tasks looks similar to this:

CREATE TABLE [TASKQUEUE]
(
    [TASKID] INT ,
    [CLIENTID] INT ,
    [STATE] TINYINT ,
    [DESCRIPTION] VARCHAR(100)
)
GO

TASKID is a primary key , CLIENTID will mark a row as taken for processing by a specific client , STATE will contain one of your possible states ('to do', 'processing', 'done').

Then your stored procedure for grabbing tasks can be:

CREATE PROCEDURE [GETTASK]
    @MAX INT = 100 ,
    @CLIENTID INT ,
    @STATE TINYINT 
    -- @STATE:
    -- 1: to do
    -- 2: processing
    -- 3: done
AS
BEGIN
    UPDATE [TASKQUEUE]
    SET [CLIENTID] = @CLIENTID ,
        [STATE] = 2
    WHERE [TASKID] IN (
        SELECT TOP(@MAX) [TASKID]
        FROM [TASKQUEUE]
        WHERE [STATE] = 1 AND [CLIENTID] IS NULL);

    SELECT [TASKID] , [DESCRIPTION]
    FROM [TASKQUEUE]
    WHERE [CLIENTID] = @CLIENTID AND [STATE] = 2;
END;

If you are using READ COMMITTED isolation level (default) or higher - UPDATE will take care of synchronization of multiple simultaneous requests. No two requests will be able to update the same TASKQUEUE rows at the same time. If the 2nd request tries to call UPDATE immediately after the 1st request's UPDATE - the rows are already modified, and will not be retrieved by the WHERE [STATE] = 1 AND [CLIENTID] IS NULL clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM