From cc79cd600b634130ae7cf71bc450d98bd8a24f10 Mon Sep 17 00:00:00 2001 From: Georg Bremer Date: Thu, 6 Jun 2024 17:05:48 +0200 Subject: [PATCH] fix: Add teamId index to SuggestedAction Removing a team would filter through all SuggestedActions which results in read spikes and is very slow. --- packages/server/database/rethinkDriver.ts | 2 +- ...685812675_addSuggestedActionTeamIdIndex.ts | 21 +++++++++++++++++++ .../server/safeMutations/safeArchiveTeam.ts | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 packages/server/postgres/migrations/1717685812675_addSuggestedActionTeamIdIndex.ts diff --git a/packages/server/database/rethinkDriver.ts b/packages/server/database/rethinkDriver.ts index 40621c4db4c..e52e2dd4ea8 100644 --- a/packages/server/database/rethinkDriver.ts +++ b/packages/server/database/rethinkDriver.ts @@ -166,7 +166,7 @@ export type RethinkSchema = { // tryRetroMeeting = 'tryRetroMeeting', // tryActionMeeting = 'tryActionMeeting' type: SuggestedActionCreateNewTeam | SuggestedActionInviteYourTeam | SuggestedActionTryTheDemo - index: 'userId' + index: 'userId' | 'teamId' } Task: { type: Task diff --git a/packages/server/postgres/migrations/1717685812675_addSuggestedActionTeamIdIndex.ts b/packages/server/postgres/migrations/1717685812675_addSuggestedActionTeamIdIndex.ts new file mode 100644 index 00000000000..adbdc0e3534 --- /dev/null +++ b/packages/server/postgres/migrations/1717685812675_addSuggestedActionTeamIdIndex.ts @@ -0,0 +1,21 @@ +import {r} from 'rethinkdb-ts' +import connectRethinkDB from '../../database/connectRethinkDB' + +export async function up() { + await connectRethinkDB() + try { + await r.table('SuggestedAction').indexCreate('teamId').run() + await r.table('SuggestedAction').indexWait().run() + } catch { + // index already exists + } +} + +export async function down() { + await connectRethinkDB() + try { + await r.table('SuggestedAction').indexDrop('teamId').run() + } catch { + // index already dropped + } +} diff --git a/packages/server/safeMutations/safeArchiveTeam.ts b/packages/server/safeMutations/safeArchiveTeam.ts index 5283349838d..a0c2522e4ff 100644 --- a/packages/server/safeMutations/safeArchiveTeam.ts +++ b/packages/server/safeMutations/safeArchiveTeam.ts @@ -26,7 +26,7 @@ const safeArchiveTeam = async (teamId: string, dataLoader: DataLoaderWorker) => })) as unknown as null, removedSuggestedActionIds: r .table('SuggestedAction') - .filter({teamId}) + .getAll(teamId, {index: 'teamId'}) .update( { removedAt: now