From ce335df9d1c169430b2a56ca914253a8c79df401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Thu, 11 May 2023 22:50:08 +0200 Subject: [PATCH] improve the query for orphan pruning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/five-lies-confess.md | 5 +++++ .../operations/util/deleteOrphanedEntities.ts | 15 +++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 .changeset/five-lies-confess.md diff --git a/.changeset/five-lies-confess.md b/.changeset/five-lies-confess.md new file mode 100644 index 0000000000..eefd2bd6fa --- /dev/null +++ b/.changeset/five-lies-confess.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Improve the query for orphan pruning diff --git a/plugins/catalog-backend/src/database/operations/util/deleteOrphanedEntities.ts b/plugins/catalog-backend/src/database/operations/util/deleteOrphanedEntities.ts index 920acae7bd..eeab85f59e 100644 --- a/plugins/catalog-backend/src/database/operations/util/deleteOrphanedEntities.ts +++ b/plugins/catalog-backend/src/database/operations/util/deleteOrphanedEntities.ts @@ -33,13 +33,16 @@ export async function deleteOrphanedEntities(options: { // Limit iterations for sanity for (let i = 0; i < 100; ++i) { const candidates = await tx - .with('orphans', orphans => + .with('orphans', ['entity_id', 'entity_ref'], orphans => orphans - .from('refresh_state') - .select('entity_id', 'entity_ref') - .whereNotIn('entity_ref', keep => - keep.distinct('target_entity_ref').from('refresh_state_references'), - ), + .from('refresh_state') + .select('refresh_state.entity_id', 'refresh_state.entity_ref') + .leftOuterJoin( + 'refresh_state_references', + 'refresh_state_references.target_entity_ref', + 'refresh_state.entity_ref', + ) + .whereNull('refresh_state_references.target_entity_ref'), ) .select({ entityId: 'orphans.entity_id',