我正在使用后端的应用程序中的SQL Server创建排队系统。该计划是创建一种类似于快餐店的FIFO排队系统。客户将进入队列,并将由X个工人提供服务。处理客户后,可用的工人将为下一个客户提供服务。我认为我的问题类似于此post中的问题,但是我不确定是否是完全相同的情况。
我查看了Rusano的“将表用作队列” article,我的理解是,我需要做一些类似于下面所示的FIFO示例的事情:
create table FifoQueue
(
Id bigint not null identity(1,1),
Payload varbinary(max)
);
go
create clustered index cdxFifoQueue on FifoQueue (Id);
go
create procedure usp_enqueueFifo
@payload varbinary(max)
as
set nocount on;
insert into FifoQueue (Payload) values (@Payload);
go
create procedure usp_dequeueFifo
as
set nocount on;
with cte as
(
select top(1) Payload
from FifoQueue with (rowlock, readpast)
order by Id
)
delete from cte
output deleted.Payload;
go
队列结构示例
状态表:
1 = Waiting
2 = Getting served
3 = Processed
EnterQueue Time | LeaveQueue Time | Worker | Status
----------------+-----------------+--------+--------
11:22 | 11:24 | 1 | 3
11:23 | | 2 | 2
11:26 | | 1 | 2
11:27 | | NULL | 1
通过存储过程将客户添加到队列中。服务的下一个客户基于他们的进入时间。
我可以使用基于FIFO示例的存储过程来处理客户的处理/出队吗?