3

プロトタイプに使用しているプレイデータのテーブルがあります。仕事中にデータを生成していますが、離れてマシンがスリープ状態になると、データ生成が停止します。これは私のアイテムのコレクションに大きなギャップを引き起こしました。

テーブルの列にある各アイテムの値をシフトして、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万を超えるレコードがあるので、それが最善の解決策である場合はIDKです(各項目を繰り返します)。SQLでこれを行うには、はるかに高速な方法が必要であることを私は知っています。

4

2 に答える 2

1

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
于 2012-01-23T21:09:41.940 に答える
1

別のランキング関数アプローチ (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;
于 2012-01-24T02:06:04.993 に答える