随着时间的推移,SQL close close 数据差距

发布于 2024-12-28 21:48:49 字数 784 浏览 2 评论 0原文

我有一个用于原型的游戏数据表。我在工作时生成数据,但是当我离开并且我的机器进入睡眠状态时,数据生成就会停止。这导致我的收藏品出现了很大的缺口。

我希望能够移动表的 DateTimeCreated 列中每个项目的值,以便任何项目与下一个生成的项目之间的间隔不超过 10 分钟。

表的结构是这样的:

CREATE TABLE [dbo].[Items](
    [Id] [uniqueidentifier] NOT NULL,
    [DateTimeCreated] [datetimeoffset](7) NOT NULL,
    [AuthorId] [uniqueidentifier] NOT NULL,
    [Source] [varchar](max) NOT NULL,       
    [FullText] [varchar](max) NOT NULL,
 CONSTRAINT [PK_Items] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

我正在考虑在 L2S 中执行此操作,但我有超过 100 万条记录,所以我不知道这是否是最佳解决方案(迭代每个项目)。我知道必须有某种方法可以在 SQL 中更快地完成此操作。

I have a table of play data that I'm using for a prototype. I'm generating the data while I'm at work, but when I leave and my machine goes to sleep, the data generation stops. This has cause large gaps in my collection of items.

I would like to be able to shift the values of each item in the DateTimeCreated collumn of my table so that there isn't a gap of more than 10 minutes between any item and the next generated item.

The structure of the table is like this:

CREATE TABLE [dbo].[Items](
    [Id] [uniqueidentifier] NOT NULL,
    [DateTimeCreated] [datetimeoffset](7) NOT NULL,
    [AuthorId] [uniqueidentifier] NOT NULL,
    [Source] [varchar](max) NOT NULL,       
    [FullText] [varchar](max) NOT NULL,
 CONSTRAINT [PK_Items] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

I was thinking about doing this in L2S, but I have over 1 million records, so IDK if that is the best solution (iterating over each item). I know there has to be some way to do this in SQL that will be much faster.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦爱 2025-01-04 21:48:49

另一种排名函数方法(未经 100% 测试):

DECLARE @tenMinutes AS INT = 600;


WITH StartingPoints AS
(
    SELECT DateTimeCreated, ROW_NUMBER() OVER(ORDER BY DateTimeCreated) AS rownum
    FROM dbo.Items AS A
    WHERE NOT EXISTS(
        SELECT * FROM dbo.Items AS B
        WHERE B.DateTimeCreated < A.DateTimeCreated 
          AND DATEDIFF(SECOND,B.DateTimeCreated, A.DateTimeCreated) BETWEEN 0 AND @tenMinutes
    )
),
EndingPoints AS
(
    SELECT DateTimeCreated, ROW_NUMBER() OVER(ORDER BY DateTimeCreated) AS rownum
    FROM dbo.Items AS A
    WHERE NOT EXISTS(
        SELECT * FROM dbo.Items AS B
        WHERE A.DateTimeCreated < B.DateTimeCreated 
          AND DATEDIFF(SECOND,A.DateTimeCreated, B.DateTimeCreated) BETWEEN 0 AND @tenMinutes
    )
),
Islands AS
(
    SELECT S.DateTimeCreated AS start_range,
           E.DateTimeCreated AS end_range,
           ROW_NUMBER() OVER(ORDER BY S.DateTimeCreated) AS row_num
    FROM StartingPoints AS S
    JOIN EndingPoints AS E on E.rownum = S.rownum
),
Ofs AS
(
    SELECT I2.start_range, 
           I2.end_range,  
           I1.end_range AS prev,
           DATEDIFF(SECOND, I1.end_range, I2.start_range) AS offset 
    FROM Islands AS I1
    JOIN Islands AS I2 ON I2.row_num = I1.row_num + 1 OR I2.row_num IS NULL
),
CmlOfs AS
(
    SELECT O1.start_range,
           O1.end_range,
           O1.prev,
           O1.offset,
           (SELECT SUM(O2.offset) FROM Ofs AS O2
            WHERE O2.start_range <= O1.start_range) AS cum_offset
    FROM Ofs AS O1
),
UpdateQ AS
(
    SELECT Items.*, DATEADD(SECOND, -1 * CmlOfs.cum_offset, Items.DateTimeCreated) AS new_value
    FROM Items
    JOIN CmlOfs ON Items.DateTimeCreated BETWEEN CmlOfs.start_range AND CmlOfs.end_range
)
UPDATE UpdateQ
SET DateTimeCreated = new_value;

An alternative Ranking-Functions Approach (not 100% tested):

DECLARE @tenMinutes AS INT = 600;


WITH StartingPoints AS
(
    SELECT DateTimeCreated, ROW_NUMBER() OVER(ORDER BY DateTimeCreated) AS rownum
    FROM dbo.Items AS A
    WHERE NOT EXISTS(
        SELECT * FROM dbo.Items AS B
        WHERE B.DateTimeCreated < A.DateTimeCreated 
          AND DATEDIFF(SECOND,B.DateTimeCreated, A.DateTimeCreated) BETWEEN 0 AND @tenMinutes
    )
),
EndingPoints AS
(
    SELECT DateTimeCreated, ROW_NUMBER() OVER(ORDER BY DateTimeCreated) AS rownum
    FROM dbo.Items AS A
    WHERE NOT EXISTS(
        SELECT * FROM dbo.Items AS B
        WHERE A.DateTimeCreated < B.DateTimeCreated 
          AND DATEDIFF(SECOND,A.DateTimeCreated, B.DateTimeCreated) BETWEEN 0 AND @tenMinutes
    )
),
Islands AS
(
    SELECT S.DateTimeCreated AS start_range,
           E.DateTimeCreated AS end_range,
           ROW_NUMBER() OVER(ORDER BY S.DateTimeCreated) AS row_num
    FROM StartingPoints AS S
    JOIN EndingPoints AS E on E.rownum = S.rownum
),
Ofs AS
(
    SELECT I2.start_range, 
           I2.end_range,  
           I1.end_range AS prev,
           DATEDIFF(SECOND, I1.end_range, I2.start_range) AS offset 
    FROM Islands AS I1
    JOIN Islands AS I2 ON I2.row_num = I1.row_num + 1 OR I2.row_num IS NULL
),
CmlOfs AS
(
    SELECT O1.start_range,
           O1.end_range,
           O1.prev,
           O1.offset,
           (SELECT SUM(O2.offset) FROM Ofs AS O2
            WHERE O2.start_range <= O1.start_range) AS cum_offset
    FROM Ofs AS O1
),
UpdateQ AS
(
    SELECT Items.*, DATEADD(SECOND, -1 * CmlOfs.cum_offset, Items.DateTimeCreated) AS new_value
    FROM Items
    JOIN CmlOfs ON Items.DateTimeCreated BETWEEN CmlOfs.start_range AND CmlOfs.end_range
)
UPDATE UpdateQ
SET DateTimeCreated = new_value;
等数载,海棠开 2025-01-04 21:48:49

如果您希望它是猪以外的任何东西,请确保在 DateTimeCreated 上有一个索引。

它还假设(正如您在评论中所说)与记录总数相比几乎没有差距。

WITH
  gap (Start,Finish)
AS
(
  SELECT
    DateTimeCreated,
    (SELECT MIN(DateTimeCreated) FROM items AS lookup WHERE DateTimeCreated > DateTimeCreated)
  FROM
    items
  WHERE
    DATEADD(second, 600, DateTimeCreated) < (SELECT MIN(DateTimeCreated) FROM items AS lookup WHERE DateTimeCreated > DateTimeCreated)

  UNION ALL

  SELECT
    MAX(DateTimeCreated),
    MAX(DateTimeCreated)
  FROM
    items
)
,
  offset (Start,Finish,Offset)
AS
(
  SELECT
    [current].Start,
    (SELECT MIN(Start) FROM gap WHERE Start > [current].Start),
    DATEDIFF(second, Start, Finish) - 600
  FROM
    gap      AS [current]
)
,
  cumulative_offset (Start,Finish,Offset)
AS
(
  SELECT
    [current].Start,
    [current].Finish,
    SUM([cumulative].Offset)
  FROM
    offset    AS [current]
  INNER JOIN
    offset    AS [cumulative]
      ON [cumulative].Start <= [current].Start
)

UPDATE
  items
FROM
  cumulative_offset
SET
  DateTimeCreated = DATEADD(second, -Offset, DateTimeCreated)
INNER JOIN
  items
    ON  items.DateTimeCreated >  cumulative.Start
    AND items.DateTimeCreated <= cumulative.Finish

Make sure to have an index on DateTimeCreated if you want this to be anything other than a pig.

It also assumes (as you said in your comment) there are few gaps compared to total number of records.

WITH
  gap (Start,Finish)
AS
(
  SELECT
    DateTimeCreated,
    (SELECT MIN(DateTimeCreated) FROM items AS lookup WHERE DateTimeCreated > DateTimeCreated)
  FROM
    items
  WHERE
    DATEADD(second, 600, DateTimeCreated) < (SELECT MIN(DateTimeCreated) FROM items AS lookup WHERE DateTimeCreated > DateTimeCreated)

  UNION ALL

  SELECT
    MAX(DateTimeCreated),
    MAX(DateTimeCreated)
  FROM
    items
)
,
  offset (Start,Finish,Offset)
AS
(
  SELECT
    [current].Start,
    (SELECT MIN(Start) FROM gap WHERE Start > [current].Start),
    DATEDIFF(second, Start, Finish) - 600
  FROM
    gap      AS [current]
)
,
  cumulative_offset (Start,Finish,Offset)
AS
(
  SELECT
    [current].Start,
    [current].Finish,
    SUM([cumulative].Offset)
  FROM
    offset    AS [current]
  INNER JOIN
    offset    AS [cumulative]
      ON [cumulative].Start <= [current].Start
)

UPDATE
  items
FROM
  cumulative_offset
SET
  DateTimeCreated = DATEADD(second, -Offset, DateTimeCreated)
INNER JOIN
  items
    ON  items.DateTimeCreated >  cumulative.Start
    AND items.DateTimeCreated <= cumulative.Finish
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文