From b229ece914f1ae59106b3b9e04707cba4b218c85 Mon Sep 17 00:00:00 2001 From: Johan Brandhorst-Satzkorn Date: Fri, 19 Jan 2024 10:59:18 -0800 Subject: [PATCH] internal/db: use more efficient index for host listing (#4256) In tests, the index including the catalog_id performs significantly better. --- .../oss/postgres/81/09_host_table_updates.up.sql | 16 ++++++++-------- internal/db/sqltest/tests/pagination/host.sql | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/internal/db/schema/migrations/oss/postgres/81/09_host_table_updates.up.sql b/internal/db/schema/migrations/oss/postgres/81/09_host_table_updates.up.sql index f5eef1a04..038731ff6 100644 --- a/internal/db/schema/migrations/oss/postgres/81/09_host_table_updates.up.sql +++ b/internal/db/schema/migrations/oss/postgres/81/09_host_table_updates.up.sql @@ -4,18 +4,18 @@ begin; -- Add new indexes for the create time and update time queries. - create index host_plugin_host_create_time_public_id_idx - on host_plugin_host (create_time desc, public_id desc); - create index host_plugin_host_update_time_public_id_idx - on host_plugin_host (update_time desc, public_id desc); + create index host_plugin_host_catalog_id_create_time_public_id_idx + on host_plugin_host (catalog_id, create_time desc, public_id desc); + create index host_plugin_host_catalog_id_update_time_public_id_idx + on host_plugin_host (catalog_id, update_time desc, public_id desc); analyze host_plugin_host; -- Add new indexes for the create time and update time queries. - create index static_host_create_time_public_id_idx - on static_host (create_time desc, public_id desc); - create index static_host_update_time_public_id_idx - on static_host (update_time desc, public_id desc); + create index static_host_catalog_id_create_time_public_id_idx + on static_host (catalog_id, create_time desc, public_id desc); + create index static_host_catalog_id_update_time_public_id_idx + on static_host (catalog_id, update_time desc, public_id desc); analyze static_host; diff --git a/internal/db/sqltest/tests/pagination/host.sql b/internal/db/sqltest/tests/pagination/host.sql index fab9f8b01..6049d88d4 100644 --- a/internal/db/sqltest/tests/pagination/host.sql +++ b/internal/db/sqltest/tests/pagination/host.sql @@ -4,11 +4,11 @@ begin; select plan(4); - select has_index('host_plugin_host', 'host_plugin_host_create_time_public_id_idx', array['create_time', 'public_id']); - select has_index('host_plugin_host', 'host_plugin_host_update_time_public_id_idx', array['update_time', 'public_id']); + select has_index('host_plugin_host', 'host_plugin_host_catalog_id_create_time_public_id_idx', array['catalog_id', 'create_time', 'public_id']); + select has_index('host_plugin_host', 'host_plugin_host_catalog_id_update_time_public_id_idx', array['catalog_id', 'update_time', 'public_id']); - select has_index('static_host', 'static_host_create_time_public_id_idx', array['create_time', 'public_id']); - select has_index('static_host', 'static_host_update_time_public_id_idx', array['update_time', 'public_id']); + select has_index('static_host', 'static_host_catalog_id_create_time_public_id_idx', array['catalog_id', 'create_time', 'public_id']); + select has_index('static_host', 'static_host_catalog_id_update_time_public_id_idx', array['catalog_id', 'update_time', 'public_id']); select * from finish();