我有27张机器过程数据表,其中包含cols:id,timestamp,col1..col70 +和最多400万行。 采样率从每10秒1次变为每30秒1次。 如何删除每个表中的行(独立),其中任何行的时间戳在前一个时间戳的21秒内(有效地将所有行的采样率更改为1/30秒)? 不要假设“id”或“timestamp”是任何顺序,没有外键,“id”是标识和主键列。 这些是非关系的历史数据表。 这些是来自一个表的一些行作为示例。 在样本间隔的早期是10秒,现在是30秒。
id Timestamp
949046 2015-03-02 02:54:49.740 --(arbitrarily starting here)
949047 2015-03-02 02:54:59.757 --delete - within 21 secs of 949046
949048 2015-03-02 02:55:09.757 --delete - within 21 secs of 949046
949049 2015-03-02 02:55:19.757 --keep - > 21 secs from 949046
949050 2015-03-02 02:55:29.727 --delete - within 21 secs of **949049**
949051 2015-03-02 02:55:39.727 --delete - within 21 secs of 949049
949052 2015-03-02 02:55:49.743 --keep - > 21 secs from 949049
949053 2015-03-02 02:55:59.727 --delete - within 21 secs of **949052**
.
.
3370919 2016-10-22 10:26:23.510 --keep assuming > 21 secs from previous row
3370920 2016-10-22 10:26:53.570 --keep - > 21 secs from 3370919
3370921 2016-10-22 10:27:23.617 --keep - > 21 secs from 3370920
答案 0 :(得分:0)
您是否尝试过使用?
DECLARE @vdate VARCHAR(24);
SELECT @vdate=CONVERT(VARCHAR(10),TimeStamp,103)+' '+CONVERT(VARCHAR(5),TimeStamp,108)+':30';
DELETE FROM TABLE WHERE TimeStamp BETWEEN @TimeStampCurrent AND @vdate
答案 1 :(得分:0)
为了不让这个开放,这就是我用TT作为测试表和SQL Server 2012的想法:
Drop table TT
Select *, row_number() over (order by timestamp) RN
into TT
from MyTable
使col RN成为主键列(执行时间减少30 +)
循环解决方案:
declare @LowWindow datetime = (Select timestamp from TT where RN=1 ),
@HiWindow datetime = (dateadd(ss,21,(Select timestamp from TT where RN=1))),
@MaxLoop int = (select top (1) count(id) from TT),
@LoopCount int = 1
Loop1:
if ((select timestamp from TT where RN = @LoopCount+1) > @LowWindow and (select timestamp from TT where RN = @LoopCount+1)< @HiWindow)
Begin
Delete from TT where rn = @LoopCount + 1
Set @LoopCount = @LoopCount + 1
End
Else
begin
set @LoopCount = @LoopCount + 1
set @LowWindow = (select timestamp from TT where RN = @LoopCount)
set @HiWindow = (dateadd(ss,21, @LowWindow))
end
if (@LoopCount > @Maxloop)
goto EndQuery
Else
GOTO Loop1
EndQuery:
从TT删除PK约束并从TT中删除col RN,将MyTable替换为TT,在MyTable中恢复对col ID的PK约束。
在3,367,000行,980MB的表上执行1小时54分钟。 随意评论。
我很想看到一套理论解决方案!