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']);