From 387ea7dd75c7078bf286cd88be630ddaa60d4bdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Mon, 11 May 2026 22:06:55 +0200 Subject: [PATCH] fix(catalog-backend): simplify facets COUNT(DISTINCT) to COUNT(*) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The UNIQUE constraint on (entity_id, key, value) from the search indices migration guarantees each entity appears at most once per (key, original_value) group, making DISTINCT unnecessary. Removing it lets the database use a simpler aggregation plan. Co-Authored-By: Claude Opus 4.6 (1M context) Signed-off-by: Fredrik Adelöw --- .changeset/facets-count-optimization.md | 5 +++++ .../catalog-backend/src/service/DefaultEntitiesCatalog.ts | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .changeset/facets-count-optimization.md diff --git a/.changeset/facets-count-optimization.md b/.changeset/facets-count-optimization.md new file mode 100644 index 0000000000..5c251b8aef --- /dev/null +++ b/.changeset/facets-count-optimization.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Simplified the entity facets aggregation from `COUNT(DISTINCT entity_id)` to `COUNT(*)`. The unique constraint on `(entity_id, key, value)` guarantees each entity appears at most once per search row group, making the `DISTINCT` unnecessary. This allows the database to use a simpler aggregation plan. diff --git a/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts b/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts index b5a65d6d05..90866e3f56 100644 --- a/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts +++ b/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts @@ -685,7 +685,7 @@ export class DefaultEntitiesCatalog implements EntitiesCatalog { .select({ facet: 'search.key', value: 'search.original_value', - count: this.database.raw('count(DISTINCT search.entity_id)'), + count: this.database.raw('count(*)'), }) .groupBy(['search.key', 'search.original_value']);