- 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

@@ -7,6 +7,7 @@ WHERE state_desc = 'ONLINE'
AND is_read_only = 0
AND source_database_id IS NULL
AND [database_id]>4 --ignore system dbs
AND [is_query_store_on] = 0
;
OPEN db_cursor;

View File

@@ -11,72 +11,75 @@ will be purged.
All satellite tables are purged in order.
Deletion is made in 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_Documedis_VaccinationCheck_LogData] AS
CREATE OR ALTER PROCEDURE [purge_Documedis_ClinicalDecisionSupport_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 (DocumedisClinicalDecisionSupportLogData_PK BIGINT NOT NULL );
WHILE @row_count > 0
BEGIN
TRUNCATE TABLE @tbl_to_del;
BEGIN TRANSACTION
-- region populate filter table
INSERT INTO @tbl_to_del([DocumedisClinicalDecisionSupportLogData_PK])
SELECT TOP (@batch)
[s].[DocumedisClinicalDecisionSupportLogData_PK]
FROM [dbo].[Documedis_ClinicalDecisionSupport_LogData] [s]
WHERE [s].[LogDateTime] > @cutoff
AND EXISTS(
SELECT 1
FROM [archivedRowsJournal] [a]
WHERE [a].[tableName] = 'Documedis_ClinicalDecisionSupport_LogData'
AND [a].[tableRowId] = [s].[DocumedisClinicalDecisionSupportLogData_PK]
);
-- region populate filter table
INSERT INTO @tbl_to_del([DocumedisClinicalDecisionSupportLogData_PK])
SELECT TOP (@batch)
[s].[DocumedisClinicalDecisionSupportLogData_PK]
FROM [dbo].[Documedis_ClinicalDecisionSupport_LogData] [s]
WHERE [s].[LogDateTime] > @cutoff
AND EXISTS(
SELECT 1
FROM [archivedRowsJournal] [a]
WHERE [a].[tableName] = 'Documedis_ClinicalDecisionSupport_LogData'
AND [a].[tableRowId] = [s].[DocumedisClinicalDecisionSupportLogData_PK]
);
-- endregion
SELECT @row_count = COUNT(1)
FROM @tbl_to_del;
-- endregion
-- region logcheck products
DELETE [lcp]
FROM @tbl_to_del [d]
INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheck] [lc] ON [lc].[DocumedisClinicalDecisionSupportLogData_FK] = [d].[DocumedisClinicalDecisionSupportLogData_PK]
INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheckProduct] [lcp] ON [lc].[DocumedisClinicalDecisionSupportLogCheck_PK] = [lcp].[DocumedisClinicalDecisionSupportLogCheck_FK];
PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - Deleted from Documedis_ClinicalDecisionSupport_LogCheckProduct. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';
-- endregion
-- region logcheck products
DELETE [lcp]
FROM @tbl_to_del [d]
INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheck] [lc] ON [lc].[DocumedisClinicalDecisionSupportLogData_FK] = [d].[DocumedisClinicalDecisionSupportLogData_PK]
INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheckProduct] [lcp] ON [lc].[DocumedisClinicalDecisionSupportLogCheck_PK] = [lcp].[DocumedisClinicalDecisionSupportLogCheck_FK];
-- endregion
-- region logcheck interactions
DELETE [lci]
FROM @tbl_to_del [d]
INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheck] [lc] ON [lc].[DocumedisClinicalDecisionSupportLogData_FK] = [d].[DocumedisClinicalDecisionSupportLogData_PK]
INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheckInteraction] [lci] ON [lc].[DocumedisClinicalDecisionSupportLogCheck_PK] = [lci].[DocumedisClinicalDecisionSupportLogCheck_FK];
PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - Deleted from Documedis_ClinicalDecisionSupport_LogCheckInteraction. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';
-- endregion
-- region logcheck interactions
DELETE [lci]
FROM @tbl_to_del [d]
INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheck] [lc] ON [lc].[DocumedisClinicalDecisionSupportLogData_FK] = [d].[DocumedisClinicalDecisionSupportLogData_PK]
INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheckInteraction] [lci] ON [lc].[DocumedisClinicalDecisionSupportLogCheck_PK] = [lci].[DocumedisClinicalDecisionSupportLogCheck_FK];
-- endregion
-- region logcheck
DELETE [lc]
FROM @tbl_to_del [d]
INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheck] [lc] ON [lc].[DocumedisClinicalDecisionSupportLogData_FK] = [d].[DocumedisClinicalDecisionSupportLogData_PK];
PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - Deleted from Documedis_ClinicalDecisionSupport_LogCheck. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';
-- endregion
-- region logcheck
DELETE [lc]
FROM @tbl_to_del [d]
INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheck] [lc] ON [lc].[DocumedisClinicalDecisionSupportLogData_FK] = [d].[DocumedisClinicalDecisionSupportLogData_PK];
-- endregion
-- region patientRisk
DELETE [lr]
FROM @tbl_to_del [d]
INNER JOIN [Documedis_ClinicalDecisionSupport_LogDataPatientRisk] [lr] ON [lr].[DocumedisClinicalDecisionSupportLogData_FK] = [d].[DocumedisClinicalDecisionSupportLogData_PK];
PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - Deleted from Documedis_ClinicalDecisionSupport_LogDataPatientRisk. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';
-- endregion
-- region patientRisk
DELETE [lr]
FROM @tbl_to_del [d]
INNER JOIN [Documedis_ClinicalDecisionSupport_LogDataPatientRisk] [lr] ON [lr].[DocumedisClinicalDecisionSupportLogData_FK] = [d].[DocumedisClinicalDecisionSupportLogData_PK];
-- endregion
-- region logData
DELETE ld
FROM @tbl_to_del [d]
INNER JOIN [Documedis_ClinicalDecisionSupport_LogData] [ld] ON [ld].[DocumedisClinicalDecisionSupportLogData_PK] = [d].[DocumedisClinicalDecisionSupportLogData_PK];
PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - Deleted from Documedis_ClinicalDecisionSupport_LogData. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';
-- endregion
-- region logData
DELETE ld
FROM @tbl_to_del [d]
INNER JOIN [Documedis_ClinicalDecisionSupport_LogData] [ld] ON [ld].[DocumedisClinicalDecisionSupportLogData_PK] = [d].[DocumedisClinicalDecisionSupportLogData_PK];
-- endregion
-- region archivedRowsJournal
DELETE j
FROM [archivedRowsJournal] j
INNER JOIN @tbl_to_del [d] ON [d].[DocumedisClinicalDecisionSupportLogData_PK] = [j].[tableRowId]
WHERE [j].[tableName]='Documedis_ClinicalDecisionSupport_LogData';
PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - deleted from archivedRowsJournal. '+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].[DocumedisClinicalDecisionSupportLogData_PK] = [j].[tableRowId]
WHERE [j].[tableName]='Documedis_ClinicalDecisionSupport_LogData';
-- endregion
END
COMMIT TRANSACTION
END

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;
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]
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]
-- 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

View File

@@ -10,46 +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_Documedis_VaccinationCheck_LogData] AS
CREATE OR ALTER 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;
BEGIN TRANSACTION
-- 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]
);
-- 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]
);
-- endregion
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];
PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - Deleted from Documedis_VaccinationCheck_LogData. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';
-- 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';
PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - Deleted from archivedRowsJournal. '+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 [j].[tableRowId] = [d].[DocumedisVaccinationCheckLogData_PK]
WHERE [j].[tableName]='Documedis_VaccinationCheck_LogData';
-- endregion
END
COMMIT TRANSACTION
END

View File

@@ -0,0 +1,124 @@
USE [msdb]
GO
IF EXISTS(
SELECT 2
FROM dbo.[sysjobs] j
WHERE j.[name] = N'Logs cleanup'
)
BEGIN
exec dbo.sp_delete_job @job_name=N'Logs cleanup', @delete_unused_schedule=1;
END
GO
/****** Object: Job [Logs cleanup] Script Date: 03.09.2025 11:22:46 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object: JobCategory [Database Maintenance] Script Date: 03.09.2025 11:22:46 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'Database Maintenance' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'Database Maintenance'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'Logs cleanup',
@enabled=1,
@notify_level_eventlog=0,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'https://galenica.atlassian.net/browse/MDDOC-878
Implement calls to the cleanup procedures in documedisLogs',
@category_name=N'Database Maintenance',
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [empty step] Script Date: 03.09.2025 11:22:46 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'empty step',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=3,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'--nothing',
@database_name=N'master',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [call purge_documedisSecurityLogs] Script Date: 03.09.2025 11:22:46 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'call DocumedisLogs.purge_documedisSecurityLogs',
@step_id=2,
@cmdexec_success_code=0,
@on_success_action=3,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'exec dbo.purge_documedisSecurityLogs',
@database_name=N'DocumedisLogs',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [call purge_Documedis_VaccinationCheck_LogData] Script Date: 03.09.2025 11:22:46 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'call DocumedisLogs.purge_Documedis_VaccinationCheck_LogData',
@step_id=3,
@cmdexec_success_code=0,
@on_success_action=3,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'exec dbo.purge_Documedis_VaccinationCheck_LogData',
@database_name=N'DocumedisLogs',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [call purge_Documedis_ClinicalDecisionSupport_LogData] Script Date: 03.09.2025 11:22:46 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'call DocumedisLogs.purge_Documedis_ClinicalDecisionSupport_LogData',
@step_id=4,
@cmdexec_success_code=0,
@on_success_action=3,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'exec dbo.purge_Documedis_ClinicalDecisionSupport_LogData',
@database_name=N'DocumedisLogs',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [empty closing step] Script Date: 03.09.2025 11:22:46 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'empty closing step',
@step_id=5,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'--nothing',
@database_name=N'master',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO

View File

@@ -4,9 +4,6 @@ SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET XACT_ABORT ON;
SET NOCOUNT ON;
GO
IF OBJECT_ID('dbo.sp_DBPermissions') IS NULL
EXECUTE sp_executesql N'CREATE PROCEDURE dbo.sp_DBPermissions AS PRINT ''Stub'';';
GO
/*********************************************************************************************
sp_DBPermissions V7.0
@@ -175,7 +172,7 @@ Data is ordered as follows
-- 08/15/2023 - Add orphan functionality with @ShowOrphans parameter.
*********************************************************************************************/
ALTER PROCEDURE [dbo].sp_DBPermissions
CREATE OR ALTER PROCEDURE [dbo].sp_DBPermissions
(
@DBName sysname = NULL,
@Principal sysname = NULL,