[英]SQL Server - 2 sec inserts and 5 min archiving - How to avoid table locks?
我有一个每 2 秒执行一次的插入(20 列,15000 行,在 SQL 服务器 [传感器数据] 之外),它在 200 毫秒内运行。 我想在这个表中只保留 10 分钟的数据(总和 ~4.500.000 行),然后将最早的 5 分钟移动(存档)到另一个存档表(将存储 50 天,数十亿行)。 归档的存储过程:
begin tran
declare @UTC_min datetime2(2) = (select TOP 1 UTCLogDateTime from table1 order by UTCLogDateTime asc)
declare @UTC_copy datetime2(2) = dateadd(minute,5,@UTC_min)
INSERT INTO archive_table
SELECT *
FROM table1
where UTCLogDateTime<@UTC_copy
delete top(100000) from table1 where UTCLogDateTime<@UTC_copy
WHILE @@rowcount > 0
BEGIN
delete top(100000) from table1 where UTCLogDateTime<@UTC_copy
END
commit
我想确保插件完美无缺地运行,并且尽可能快地运行,而不会锁定在这个归档过程中。 归档开始时,插入的运行时间增加到 4-5 秒。 我还将有一个 2 秒的实时查询 (Power BI) 来读取此表。
目前,我在两个表的 UTCLogDateTime 列上都有一个聚集索引。
这些所有进程都必须无缝运行,而不会相互锁定表。 您对我如何实现这一目标有什么建议吗?
如果您使用 SQL Server 2016 及更高版本,您可以使用TRUNCATE WITH partitions 。 与 DELETE 相比,这使用更少的锁。 值得尝试先在 TEST 环境中对表进行分区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.