2011-02-08 40 views
0

嘿,我現在只是變得很懶。但是,我知道你們都可以幫助我,這是件好事。如何更改此過程以便在SQL Server 2005中逐日刪除記錄

我有一個表清理和刪除過程,每晚運行。它已經足夠基本,並且可以刪除數據庫中超過3天的任何記錄。

由於當我們每次刪除超過1天的記錄時,我們得到的事務日誌已滿錯誤。更改事務日誌設置會很麻煩。

SOO,有人可以告訴我如何更新proc,以便它從3天前刪除所有內容,可以說通過7天。含義返回3天,然後在過去7天內每次刪除一個表中的數據。

我們是一個全天候監控的能源商店,我可以肯定它是否會在7天內有人會注意到。

ALTER PROCEDURE [dbo].[PruneData] 
    (
    @cutoffDate DateTime 
    ) 
AS 
BEGIN 
    declare @threeDayCutoffDate DATETIME 
    set @threeDayCutoffDate = dateadd(hh, 5, DATEADD(dd, -3,dbo.DateOnly(getutcdate()))) 
    delete from LMP_DayAhead where interval < @threeDayCutoffDate 
    delete from LMP_RealTime where interval < @threeDayCutoffDate 
    delete from LMP_RealTimeIntegrated where interval < @threeDayCutoffDate 
    delete from ZonalMCP where interval < @threeDayCutoffDate 
    delete from SyncJob where synctime < @threeDayCutoffDate 
    RETURN 
END 
+0

嘿,你在一家電力公司工作!涼。 – 2011-02-08 19:38:21

+0

調查分區...這是要走的路。 – 2011-02-08 20:00:20

回答

2

爲了減少日誌壓力,您需要批量刪除。這樣的事情:

ALTER PROCEDURE [dbo].[PruneData] 
    (
    @cutoffDate DateTime 
    ) 
AS 
BEGIN 
    declare @threeDayCutoffDate DATETIME 
    declare @rows bigint; 
    declare @batchsize int; 
    set @threeDayCutoffDate = dateadd(hh, 5, DATEADD(dd, -3,dbo.DateOnly(getutcdate()))) 
    set @batchsize = 1000; 
    while (1=1) 
    begin 
     set @rows = 0; 
     delete top (@batchsize) from LMP_DayAhead where interval < @threeDayCutoffDate; 
     set @rows = @rows + @@ROWCOUNT; 
     delete top (@batchsize) from LMP_RealTime where interval < @threeDayCutoffDate 
     set @rows = @rows + @@ROWCOUNT; 
     delete top (@batchsize) from LMP_RealTimeIntegrated where interval < @threeDayCutoffDate 
     set @rows = @rows + @@ROWCOUNT; 
     delete top (@batchsize) from ZonalMCP where interval < @threeDayCutoffDate 
     set @rows = @rows + @@ROWCOUNT; 
     delete top (@batchsize) from SyncJob where synctime < @threeDayCutoffDate 
     set @rows = @rows + @@ROWCOUNT; 

     if 0 = @rows 
     begin 
      break; 
     end 
    end 
    RETURN 
END 

有很多方法來優化這個,例如。一旦表被修剪,在下一次循環迭代時跳過它。最終的優化是完全避免刪除。取而代之的是使用滾動窗口分區切換方案:每天對錶格進行分區,並且每天都切換出最後一個分區並切換到新分區。這幾乎是瞬間的。見Transferring Data Efficiently by Using Partition SwitchingPartitioned Tables and Indexes in SQL Server 2005How to Implement an Automatic Sliding Window in a Partitioned Table on SQL Server 2005

0

即使你把它弄壞了成批次,從開始到結束,你需要在同一時間內刪除的記錄相同量(秒/分鐘)。事務日誌需要保留所有這些記錄,直到您完成完整備份或日誌備份。

您需要做的是提高事務日誌備份頻率,並將日誌備份之間的批次刪除運行。因此,假設您預計每天大約有100萬條記錄被刪除。即使您已經說過要刪除7天(由3補償),讓我們設計系統以便一次處理3天。所以如果你沒有運行7天(不應該發生,因爲你說每天運行),它需要3天的時間才能趕上(每天做3次)。

下一步是將其分成批次。

delete top(10000) from LMP_DayAhead where interval < @threeDayCutoffDate 
delete top(30000) from LMP_RealTime where interval < @threeDayCutoffDate 
delete top(20000) from LMP_RealTimeIntegrated where interval < @threeDayCutoffDate 
delete top(50000) from ZonalMCP where interval < @threeDayCutoffDate 
delete top(1000) from SyncJob where synctime < @threeDayCutoffDate 

其中每個TOP(X)被設計爲覆蓋大約1天的價值。

最後,在凌晨1點和凌晨1點20分安排額外的事務日誌備份,並且這項工作在上午12點50分,凌晨1點10分,凌晨1點30分運行,每批處理1天的數據。之間的事務日誌備份將保持日誌清理以便下次運行。

這是一般策略,但您可以調整它以適應您的需求。

相關問題