From 84ebd7e959ae9c82a81bb2ee3393049746cc8c80 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Mon, 30 Dec 2024 15:49:00 -0500 Subject: [PATCH] call_date should be the first index column. This results in a huge speedup --- sql/postgres/migrations/001_initial.up.sql | 2 +- sql/postgres/queries/calls.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sql/postgres/migrations/001_initial.up.sql b/sql/postgres/migrations/001_initial.up.sql index b9bc94c..0a8e2a8 100644 --- a/sql/postgres/migrations/001_initial.up.sql +++ b/sql/postgres/migrations/001_initial.up.sql @@ -104,7 +104,7 @@ CREATE TABLE IF NOT EXISTS calls( ) PARTITION BY RANGE (call_date); CREATE INDEX IF NOT EXISTS calls_transcript_idx ON calls USING GIN (to_tsvector('english', transcript)); -CREATE INDEX IF NOT EXISTS calls_call_date_tg_idx ON calls(system, talkgroup, call_date); +CREATE INDEX IF NOT EXISTS calls_call_date_tg_idx ON calls(call_date, talkgroup, system); CREATE TABLE swept_calls ( id UUID PRIMARY KEY, diff --git a/sql/postgres/queries/calls.sql b/sql/postgres/queries/calls.sql index 25a2d34..353d61c 100644 --- a/sql/postgres/queries/calls.sql +++ b/sql/postgres/queries/calls.sql @@ -76,6 +76,7 @@ VALUES SELECT pg_size_pretty(pg_database_size(current_database())); -- name: SweepCalls :execrows +-- This is used to sweep calls that are part of an incident prior to pruning a partition. WITH to_sweep AS ( SELECT id, submitter, system, talkgroup, calls.call_date, audio_name, audio_blob, duration, audio_type, audio_url, frequency, frequencies, patches, tg_label, tg_alpha_tag, tg_group, source, transcript @@ -85,7 +86,6 @@ WITH to_sweep AS ( ) INSERT INTO swept_calls SELECT * FROM to_sweep; -- name: CleanupSweptCalls :execrows --- This is used to sweep calls that are part of an incident prior to pruning a partition. WITH to_sweep AS ( SELECT id FROM calls JOIN incidents_calls ic ON ic.call_id = calls.id