This commit is contained in:
Thierry Schork
2025-07-09 16:43:53 +02:00
parent b9cfbda6dc
commit 1975962f2d
9 changed files with 322 additions and 7 deletions

View File

@@ -0,0 +1,55 @@
USE [DocumedisLogs]
GO
/*
MDDOC-878
Purge of the table documedisLogs.dbo.Documedis_VaccinationCheck_LogData
Only records that are more than 7 months old and present as archived in [archivedRowsJournal]
will be purged.
Deletion is made in small batches to avoid locking.
*/
CREATE PROCEDURE [purge_Documedis_VaccinationCheck_LogData] AS
BEGIN
DECLARE @cutoff DATE = DATEADD(MONTH, -7, CURRENT_TIMESTAMP);
DECLARE @batch INT = 5000;
DECLARE @row_count INT = 1
DECLARE @tbl_to_del TABLE (DocumedisVaccinationCheckLogData_PK BIGINT NOT NULL );
WHILE @row_count > 0 BEGIN
TRUNCATE TABLE @tbl_to_del;
-- region populate filter table
INSERT INTO @tbl_to_del([DocumedisVaccinationCheckLogData_PK])
SELECT TOP (@batch)
[s].[DocumedisVaccinationCheckLogData_PK]
FROM [dbo].[Documedis_VaccinationCheck_LogData] [s]
WHERE [s].[LogDateTime] > @cutoff
AND EXISTS(
SELECT 1
FROM [archivedRowsJournal] [a]
WHERE [a].[tableName] = 'Documedis_VaccinationCheck_LogData'
AND [a].[tableRowId] = [s].[DocumedisVaccinationCheckLogData_PK]
);
SELECT @row_count = COUNT(1)
FROM @tbl_to_del;
-- endregion
-- region Documedis_VaccinationCheck_LogData
DELETE [d]
FROM @tbl_to_del d
INNER JOIN [Documedis_VaccinationCheck_LogData] s ON [s].[DocumedisVaccinationCheckLogData_PK] = [d].[DocumedisVaccinationCheckLogData_PK];
-- endregion
-- region archivedRowsJournal
DELETE j
FROM [archivedRowsJournal] j
INNER JOIN @tbl_to_del d ON [j].[tableRowId] = [d].[DocumedisVaccinationCheckLogData_PK]
WHERE [j].[tableName]='Documedis_VaccinationCheck_LogData';
-- endregion
END
END