- adapted the procedures to not loop, but only do 1 batch of deletion.

The job will be called in a loop, every minutes and will handle the enforcement of not starting during working hours.
- Added the job that will call all cleanup procs in sequence
This commit is contained in:
Thierry Schork
2025-09-03 16:17:57 +02:00
parent 60007cd7df
commit b19a4c4bdd
6 changed files with 259 additions and 135 deletions

View File

@@ -10,45 +10,45 @@ Only records that are more than 7 months old and present as archived in [archive
will be purged.
Deletion is made in small batches to avoid locking.
Only 1 iteration is made, the job calling this procedure will handle the calls and be executed several times.
it will also handle the hours the deletion should run from / to
*/
CREATE PROCEDURE [purge_documedisSecurityLogs] AS
CREATE OR ALTER PROCEDURE [purge_documedisSecurityLogs] AS
BEGIN
DECLARE @cutoff DATE = DATEADD(MONTH, -7, CURRENT_TIMESTAMP);
DECLARE @batch INT = 5000;
DECLARE @row_count INT = 1
DECLARE @tbl_to_del TABLE([DocumedisSecurityLogId] BIGINT NOT NULL);
WHILE @row_count > 0 BEGIN
TRUNCATE TABLE @tbl_to_del;
-- region populate filter table
INSERT INTO @tbl_to_del([DocumedisSecurityLogId])
SELECT TOP (@batch)
[s].[DocumedisSecurityLogId]
FROM [DocumedisSecurityLogs] [s]
WHERE [s].[LogDateTime] > @cutoff
AND EXISTS(
SELECT 1
FROM [archivedRowsJournal] [j]
WHERE [j].[tableName] = 'DocumedisSecurityLogs'
AND [j].[tableRowId] = [s].[DocumedisSecurityLogId]
);
SELECT @row_count = COUNT(1)
FROM @tbl_to_del;
-- endregion
-- region DocumedisSecurityLogs
DELETE [s]
FROM @tbl_to_del [d]
INNER JOIN [dbo].[DocumedisSecurityLogs] [s] ON [s].[DocumedisSecurityLogId] = [d].[DocumedisSecurityLogId];
-- endregion
-- region archivedRowsJournal
DELETE [j]
BEGIN TRANSACTION;
-- region populate filter table
INSERT INTO @tbl_to_del([DocumedisSecurityLogId])
SELECT TOP (@batch)
[s].[DocumedisSecurityLogId]
FROM [DocumedisSecurityLogs] [s]
WHERE [s].[LogDateTime] > @cutoff
AND EXISTS(
SELECT 1
FROM [archivedRowsJournal] [j]
INNER JOIN @tbl_to_del [d] ON [d].[DocumedisSecurityLogId] = [j].[tableRowId]
WHERE [tableName] = 'DocumedisSecurityLogs'
-- endregion
END
WHERE [j].[tableName] = 'DocumedisSecurityLogs'
AND [j].[tableRowId] = [s].[DocumedisSecurityLogId]
);
-- endregion
-- region DocumedisSecurityLogs
DELETE [s]
FROM @tbl_to_del [d]
INNER JOIN [dbo].[DocumedisSecurityLogs] [s] ON [s].[DocumedisSecurityLogId] = [d].[DocumedisSecurityLogId];
PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - Deleted from DocumedisSecurityLogs. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';
-- endregion
-- region archivedRowsJournal
DELETE [j]
FROM [archivedRowsJournal] [j]
INNER JOIN @tbl_to_del [d] ON [d].[DocumedisSecurityLogId] = [j].[tableRowId]
WHERE [tableName] = 'DocumedisSecurityLogs';
PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - Deleted from archivedRowsJournal. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';
-- endregion
COMMIT TRANSACTION;
END