From 6ba0a102cbbdec0e23a4265926a9304bf0e9927a Mon Sep 17 00:00:00 2001 From: Adam Schmalhofer Date: Wed, 22 Apr 2026 15:01:19 +0200 Subject: [PATCH] fix: Speedup v_host_templates from 2min to 1,3s on large Postgres DBs. --- lufa/schema.sql | 4 ++++ tests/integration/lufa/drop_tables.sql | 1 + 2 files changed, 5 insertions(+) diff --git a/lufa/schema.sql b/lufa/schema.sql index ec1b2bd..73cf8f1 100644 --- a/lufa/schema.sql +++ b/lufa/schema.sql @@ -4,6 +4,7 @@ DROP VIEW IF EXISTS v_host_templates_ordered; DROP VIEW IF EXISTS v_template_compliance; DROP VIEW IF EXISTS v_host_compliance; DROP VIEW IF EXISTS v_host_template_compliance; +DROP INDEX IF EXISTS v_host_templates_index; DROP MATERIALIZED VIEW IF EXISTS v_host_templates; DROP VIEW IF EXISTS v_job_status; @@ -153,6 +154,9 @@ CREATE MATERIALIZED VIEW v_host_templates AS JOIN job_templates ON jobs.tower_job_template_id = job_templates.tower_job_template_id; +CREATE INDEX v_host_templates_index + ON v_host_templates (tower_job_template_id, ansible_host); + CREATE VIEW v_host_template_compliance AS SELECT ansible_host, diff --git a/tests/integration/lufa/drop_tables.sql b/tests/integration/lufa/drop_tables.sql index a9f0823..6e344f4 100644 --- a/tests/integration/lufa/drop_tables.sql +++ b/tests/integration/lufa/drop_tables.sql @@ -5,6 +5,7 @@ DROP INDEX IF EXISTS task_callbacks_task_uuid CASCADE; DROP INDEX IF EXISTS task_callbacks_ansible_host_index CASCADE; DROP INDEX IF EXISTS task_callbacks_timestamp_index CASCADE; DROP INDEX IF EXISTS stats_ansible_host_index CASCADE; +DROP INDEX IF EXISTS v_host_templates_index; DROP TRIGGER IF EXISTS delete_unreferenced_job_templates ON jobs; DROP FUNCTION IF EXISTS delete_unreferenced_job_templates();