From 93dba136abc3e51994e291ca8ff0b2b671dc351c Mon Sep 17 00:00:00 2001 From: sugar Date: Sat, 11 Jan 2025 21:56:59 +0100 Subject: [PATCH] Replace notes(userId) index with (userId, id) This improves performance of queries searching posts by a given user, optionally with a provided range of dates, and then using ORDER BY to sort posts by date. Examples of such queries include: viewing posts by a given user, as well as checking when the last post by a given user was written for Mastodon API. --- .../migration/1736888704471-NoteUserIdIdIndex.js | 11 +++++++++++ packages/backend/src/models/Note.ts | 1 + 2 files changed, 12 insertions(+) create mode 100644 packages/backend/migration/1736888704471-NoteUserIdIdIndex.js diff --git a/packages/backend/migration/1736888704471-NoteUserIdIdIndex.js b/packages/backend/migration/1736888704471-NoteUserIdIdIndex.js new file mode 100644 index 000000000..35ae87288 --- /dev/null +++ b/packages/backend/migration/1736888704471-NoteUserIdIdIndex.js @@ -0,0 +1,11 @@ +export class NoteUserIdIdIndex1736888704471 { + name = 'NoteUserIdIdIndex1736888704471' + + async up(queryRunner) { + await queryRunner.query(`CREATE INDEX "IDX_note_userId_id" ON "note" ("userId", "id") `); + } + + async down(queryRunner) { + await queryRunner.query(`DROP INDEX "public"."IDX_note_userId_id"`); + } +} diff --git a/packages/backend/src/models/Note.ts b/packages/backend/src/models/Note.ts index 7803a68c7..7288bf432 100644 --- a/packages/backend/src/models/Note.ts +++ b/packages/backend/src/models/Note.ts @@ -15,6 +15,7 @@ import type { MiDriveFile } from './DriveFile.js'; @Index('IDX_NOTE_MENTIONS', { synchronize: false }) @Index('IDX_NOTE_FILE_IDS', { synchronize: false }) @Index('IDX_NOTE_VISIBLE_USER_IDS', { synchronize: false }) +@Index('IDX_note_userId_id', ['userId', 'id']) export class MiNote { @PrimaryColumn(id()) public id: string;