[英]How to create a queue like structure in SQL Server
Is there a good way to create a queue like structure in SQL Server? 有没有在SQL Server中创建类似队列的好方法?
Requirements: 要求:
A simple identity column would work for requirements 1 and 2, but how would I handle 3? 一个简单的标识列适用于需求1和2,但是我将如何处理3?
I ended up implementing the solution from @roger-wolf One difference, I used a trigger rather than a stored procedure to renumber. 我最终从@ roger-wolf实施了解决方案。一个区别是,我使用触发器而不是存储过程来重新编号。 Here's my trigger code:
这是我的触发代码:
CREATE TRIGGER [dbo].[TR_Queue]
ON [dbo].[Queue]
AFTER INSERT, DELETE, UPDATE
AS
BEGIN
SET NOCOUNT ON;
-- Get the current max value in priority
DECLARE @maxPriority INT = COALESCE((SELECT MAX([priority]) FROM [dbo].[Queue]), 0);
WITH newValues AS (
-- Renumber by priority, starting at 1
SELECT [queueID]
,ROW_NUMBER() OVER(ORDER BY [priority] ASC) AS [priority]
FROM (
-- Pretend all nulls are greater than previous max priority
SELECT [queueID]
,COALESCE([priority], @maxPriority+1) AS [priority]
FROM [dbo].[Queue]
) AS tbl
)
UPDATE q
SET q.[priority] = newValues.[priority]
FROM [dbo].[Queue] AS qroger-wolf
INNER JOIN newValues
ON q.[queueID] = newValues.[queueID]
END
This works well for me as the queue is always relatively small and infrequently updated, so I don't have to work about performance of the trigger. 这对我来说效果很好,因为队列总是相对较小且不经常更新,因此我不必处理触发器的性能。
Use a float
column for prioritisation and an approach similar to Celko trees: 使用
float
列进行优先级排序,并使用类似于Celko树的方法:
row_number()
instead of float values in UI; row_number()
而不是float值; The only deficiency I see here is that it becomes a bit more difficult to find N-th item in a middle of a queue, when it's neither first nor last. 我在这里看到的唯一缺陷是,当队列中的第N个项目既不是第一个也不是最后一个时,要找到它要困难一些。 If you don't need that, the approach should work.
如果您不需要它,则该方法应该有效。
您可以添加类型为DateTime
的Priority
列,然后将某行设置为优先级行时,在Priority
列中设置当前日期时间,然后order by
条件将其用作order by
一部分?
I had a similar requirement in a past project, what I did (and it worked): 我在过去的项目中有类似的要求,我做了什么(并且奏效了):
update_at_utc
of type datetime2
datetime2
update_at_utc
列 update_at_utc = GETDATEUTC()
update_at_utc = GETDATEUTC()
update_at_utc
update_at_utc
排序 update_at_utc
of these rows and use it to set update_at_utc
of the row being moved. update_at_utc
的平均值,然后使用它来设置要移动的行的update_at_utc
。 Note 1: Point 4 assumes that the frequency of inserts and of moving the rows up/down the queue is such that datetime2 type has sufficient resolution. 注1:第4点假设插入和上下移动队列的频率使得datetime2类型具有足够的分辨率。 For example, if you insert 2 rows 1 millisecond apart, and then try to move 1000 rows between these 2 rows, then datetime2 resolution will be insufficient ( https://docs.microsoft.com/en-us/sql/t-sql/data-types/datetime2-transact-sql?view=sql-server-2017 ).
例如,如果您以1毫秒的间隔插入2行,然后尝试在这2行之间移动1000行,则datetime2分辨率将不足( https://docs.microsoft.com/zh-cn/sql/t-sql / data-types / datetime2-transact-sql?view = sql-server-2017 )。 In such case, the moving of rows up/down the queue would need to be more complicated;
在这种情况下,行在队列中的上/下移动将更加复杂。 When moving a row N places lower down:
当移动N行时,请降低位置:
update_at_utc
to the preceding row's update_at_utc
update_at_utc
分配给上一行的update_at_utc
update_at_utc
of the row being moved to the date remembered in point 1 above. update_at_utc
分配到上面第1点中记住的日期。 Note 2: I suggest UTC dates instead of local dates to avoid issues during a daylight saving switch. 注意2:我建议使用UTC日期而不是本地日期,以避免在夏令时期间出现问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.