简体   繁体   English

SQL 服务器 - 2 秒插入和 5 分钟归档 - 如何避免表锁定?

[英]SQL Server - 2 sec inserts and 5 min archiving - How to avoid table locks?

I have an insert that is executed every 2 seconds (20 columns, 15000 rows, outside of SQL Server [sensor data]), it runs in 200 ms.我有一个每 2 秒执行一次的插入(20 列,15000 行,在 SQL 服务器 [传感器数据] 之外),它在 200 毫秒内运行。 I would like to keep only 10 minutes of data (sum ~4.500.000 rows) in this table then move (archive) the earliest 5 minutes to another archive table (that will store 50 days, billions of rows).我想在这个表中只保留 10 分钟的数据(总和 ~4.500.000 行),然后将最早的 5 分钟移动(存档)到另一个存档表(将存储 50 天,数十亿行)。 The stored procedure for archiving:归档的存储过程:

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

I would like to ensure that the insert runs flawlessly and as fast as possible, without locking out from this archiving process.我想确保插件完美无缺地运行,并且尽可能快地运行,而不会锁定在这个归档过程中。 When archiving starts, the runtime for insert grows to 4-5 sec.归档开始时,插入的运行时间增加到 4-5 秒。 I will also have a 2 sec live query (Power BI) to read this table.我还将有一个 2 秒的实时查询 (Power BI) 来读取此表。

Currently I have a clustered index on the UTCLogDateTime column for both tables.目前,我在两个表的 UTCLogDateTime 列上都有一个聚集索引

These all processes have to run seamlessly, without locking the table from each other.这些所有进程都必须无缝运行,而不会相互锁定表。 Do you have any suggestions how I can achieve this?您对我如何实现这一目标有什么建议吗?

If you are using SQL Server 2016 and above, you may use TRUNCATE WITH partitions .如果您使用 SQL Server 2016 及更高版本,您可以使用TRUNCATE WITH partitions This uses fewer locks compared to DELETE.与 DELETE 相比,这使用更少的锁。 Worth trying partitioning the table in TEST environment first.值得尝试先在 TEST 环境中对表进行分区。

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

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