简体   繁体   English

SQL - 在插入时生成循环编号

[英]SQL - Generate Round robin number on insert

I am deploying an existing bespoke windows service (C#) multiple instances that will reads from a single queue table.我正在部署一个现有的定制 Windows 服务 (C#) 多个实例,这些实例将从单个队列表中读取。

The queue is based on the below simple SQL table.队列基于以下简单的 SQL 表。

Record-Id (int auto id)

Added-Date (Date)

Added-By (Text)

Data-To-be-Processed (Text)

**Pool-Number (int)**

How would I create a round robin sequence number for each insert given a max pool size?给定最大池大小,如何为每个插入创建循环序列号? here I use a pool size of 3 (This can be hard coded).这里我使用的池大小为 3(这可以硬编码)。 eg例如

1 | 31/10/2014 | DATA | Pool 1

2 | 31/10/2014 | DATA | Pool 2

3 | 31/10/2014 | DATA | Pool 3

4 | 31/10/2014 | DATA | Pool 1

5 | 31/10/2014 | DATA | Pool 2

6 | 31/10/2014 | DATA | Pool 3

7 | 31/10/2014 | DATA | Pool 1

I have thought about using a Sequence table and increment it on each insert and reset it to 1 when it gets to the max pool size eg我曾考虑使用序列表并在每次插入时将其递增,并在达到最大池大小时将其重置为 1,例如

TbSeq

  dbSeq (int) (Will contain 1-3 depending last insert)

Is there a better way to do this?有一个更好的方法吗?

If you can use the RecordId as an aid, then you could use Modulo ( % )如果您可以使用RecordId作为辅助,那么您可以使用Modulo ( % )

select *, 1+((RecordId-1)%3) as Pool
from t

rextester demo: http://rextester.com/WNEIQM50851 reextester 演示: http ://rextester.com/WNEIQM50851

returns:返回:

+----------+------------+-------------------+------+
| RecordId | AddedDate  | DataToBeProcessed | Pool |
+----------+------------+-------------------+------+
|        1 | 2014-10-31 | DATA              |    1 |
|        2 | 2014-10-31 | DATA              |    2 |
|        3 | 2014-10-31 | DATA              |    3 |
|        4 | 2014-10-31 | DATA              |    1 |
|        5 | 2014-10-31 | DATA              |    2 |
|        6 | 2014-10-31 | DATA              |    3 |
|        7 | 2014-10-31 | DATA              |    1 |
+----------+------------+-------------------+------+

You could add this as a computed column (persisted optional, but recommended)您可以将其添加为计算列(持久化可选,但推荐)

alter table t
  add Pool as (1+((RecordId-1)%3)) persisted;

I wouldn't depend on the RecordId per se .我不会依赖RecordId本身 It might have gaps.它可能有间隙。 The simplest way to do a real round-robin is to use row_number() and modulo arithmetic:进行真正循环的最简单方法是使用row_number()和模运算:

select 1 + (row_number() over (order by id) - 1) % 3 as poolnum
from t;

If you know that RecordId has no gaps, then you can use that instead.如果您知道RecordId没有间隙,那么您可以改用它。 Using RecordId is more efficient, because you can do the full calculation within one row, and even add a computed column:使用RecordId效率更高,因为您可以在一行内进行完整计算,甚至可以添加一个计算列:

alter table t add poolnum as (1 + (row_number() over (order by id) - 1) % 3)

You can use SECQUENCE as:您可以将SECQUENCE用作:

CREATE TABLE MyTable (
    Record_Id INT IDENTITY(1,1), Added_Date DATE,
    Added_By VARCHAR(50), Data_To_be_Processed VARCHAR(50), Pool_Number INT);
GO    
CREATE SEQUENCE Pool
AS INT
START WITH 1
MINVALUE 1
MAXVALUE 3
CYCLE
GO

INSERT INTO MyTable VALUES
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool);

SELECT *
FROM MyTable;

Result:结果:

+-----------+---------------------+----------+----------------------+-------------+
| Record_Id |     Added_Date      | Added_By | Data_To_be_Processed | Pool_Number |
+-----------+---------------------+----------+----------------------+-------------+
|         1 | 31.10.2014 00:00:00 | DATA     | Pool                 |           1 |
|         2 | 31.10.2014 00:00:00 | DATA     | Pool                 |           2 |
|         3 | 31.10.2014 00:00:00 | DATA     | Pool                 |           3 |
|         4 | 31.10.2014 00:00:00 | DATA     | Pool                 |           1 |
|         5 | 31.10.2014 00:00:00 | DATA     | Pool                 |           2 |
|         6 | 31.10.2014 00:00:00 | DATA     | Pool                 |           3 |
|         7 | 31.10.2014 00:00:00 | DATA     | Pool                 |           1 |
|         8 | 31.10.2014 00:00:00 | DATA     | Pool                 |           2 |
|         9 | 31.10.2014 00:00:00 | DATA     | Pool                 |           3 |
+-----------+---------------------+----------+----------------------+-------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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