簡體   English   中英

SQL 服務器 - 2 秒插入和 5 分鍾歸檔 - 如何避免表鎖定?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM