From da3b155ae85af2b6eb889b0c712723b40eb94378 Mon Sep 17 00:00:00 2001 From: poveyd <32199157+poveyd@users.noreply.github.com> Date: Mon, 4 May 2020 12:18:25 +0100 Subject: [PATCH] Create cycle_notification_history_in_batches.sql --- .../cycle_notification_history_in_batches.sql | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 database_maintenance/cycle_notification_history_in_batches.sql diff --git a/database_maintenance/cycle_notification_history_in_batches.sql b/database_maintenance/cycle_notification_history_in_batches.sql new file mode 100644 index 000000000..82297434f --- /dev/null +++ b/database_maintenance/cycle_notification_history_in_batches.sql @@ -0,0 +1,31 @@ +-- Call once at the start of the process + +CREATE TABLE notification_history_pivot AS SELECT * from notification_history WHERE 1=2; + +SELECT id +INTO nh_temp +FROM notification_history; + +SELECT COUNT(*) AS 'Total number of rows in nh' FROM nh_temp; + +DELETE FROM nh_temp t +USING notification_history_pivot p +WHERE t.id = p.id; + +SELECT COUNT(*) AS 'Number of rows remaining that need moving across from nh to nh_pivot' FROM nh_temp; + +-- In each function call, using same database connection as used for the above SQL (needs to be in a transaction; this can be inside a stored function or in a transaction from the code) + +INSERT INTO notification_history_pivot +SELECT n.* +FROM notification_history n, + nh_temp t +WHERE n.id = t.id; + +DELETE FROM nh_temp t +USING notification_history_pivot p +WHERE t.id = p.id; + +SELECT COUNT(*) from nh_temp; + +-- Loop until this result is zero.