JBAS-1764 A/T purge sql speed improvements
authorBill Erickson <berickxx@gmail.com>
Tue, 2 Jan 2018 21:01:59 +0000 (13:01 -0800)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/sql/schema/deploy/at-purge-interval-data.sql
KCLS/sql/schema/deploy/at-purge-interval.sql

index 451350a..5300806 100644 (file)
@@ -154,5 +154,15 @@ SELECT CLOCK_TIMESTAMP(), 'action_trigger.event rebuilt',
 COMMIT;
 ANALYZE action_trigger.event;
 ANALYZE action_trigger.event_output;
+
+CREATE INDEX action_trigger_event_template_output_idx 
+    ON action_trigger.event (template_output);
+
+CREATE INDEX action_trigger_event_error_output_idx 
+    ON action_trigger.event (error_output);
+
+CREATE INDEX action_trigger_event_async_output_idx 
+    ON action_trigger.event (async_output);
+
 -- END_DEPLOYMENT --
 
index 2638c37..9289046 100644 (file)
@@ -75,8 +75,9 @@ BEGIN
             AND evt.update_time < (NOW() - def.retention_interval)
     );
 
-    WITH linked_outputs AS (
-        SELECT templates.id AS id FROM (
+    CREATE TEMPORARY TABLE tmp_purge_output ON COMMIT DROP AS
+        SELECT ateo.id FROM action_trigger.event_output ateo
+        LEFT JOIN (
             SELECT template_output AS id
                 FROM action_trigger.event WHERE template_output IS NOT NULL
             UNION
@@ -85,10 +86,12 @@ BEGIN
             UNION
             SELECT async_output AS id
                 FROM action_trigger.event WHERE async_output IS NOT NULL
-        ) templates
-    ) DELETE FROM action_trigger.event_output
-        WHERE id NOT IN (SELECT id FROM linked_outputs);
+        ) linked ON (linked.id = ateo.id)
+        WHERE linked.id IS NULL
+    ;
 
+    DELETE FROM action_trigger.event_output
+        WHERE id IN (SELECT id FROM tmp_purge_output);
 END;
 $_$ LANGUAGE PLPGSQL;