From f1cf01b554a0d44931d0422d7fc7772c3994fa6a Mon Sep 17 00:00:00 2001 From: Kameron Smith Date: Mon, 23 Nov 2026 23:14:13 -0500 Subject: [PATCH] perf(server): index thread messages by updated_at for the shell's latest visible text The thread shell's latest_visible_text subquery ordered every message of a thread by updated_at with no matching index (only user messages had one), so each shell build sorted the thread's full history and parsed each user message's JSON. Measured on the live database: 6.6s across all 486 threads before, 31ms after. Migration 100. Co-Authored-By: Claude Sonnet 5.5 --- .../src/orchestration-v2/ProjectionStore.ts | 1 + apps/server/src/persistence/Migrations.ts | 2 ++ .../100_MessagesLatestVisibleIndex.test.ts | 30 +++++++++++++++++++ .../100_MessagesLatestVisibleIndex.ts | 16 ++++++++++ 4 files changed, 49 insertions(+) create mode 100644 apps/server/src/persistence/Migrations/100_MessagesLatestVisibleIndex.test.ts create mode 100644 apps/server/src/persistence/Migrations/100_MessagesLatestVisibleIndex.ts diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index 359e517c5e5c9..e314acb6488ab 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -8228,6 +8228,7 @@ export const layer: Layer.Layer = ( SELECT substr(json_extract(message.payload_json, '$.text'), 1, ${ATTENTION_MENTION_TEXT_LIMIT}) FROM orchestration_v2_projection_messages message + INDEXED BY orchestration_v2_projection_messages_thread_updated_idx WHERE message.thread_id = t.thread_id AND NOT ${isIdleCompactionMessage("message")} ORDER BY message.updated_at DESC, message.message_id DESC diff --git a/apps/server/src/persistence/Migrations.ts b/apps/server/src/persistence/Migrations.ts index 08e41f32541db..7501d27fab0d6 100644 --- a/apps/server/src/persistence/Migrations.ts +++ b/apps/server/src/persistence/Migrations.ts @@ -107,6 +107,7 @@ import Migration0096 from "./Migrations/096_RepairStaleTitleRegeneration.ts"; import Migration0097 from "./Migrations/097_AuthSessionFounder.ts"; import Migration0098 from "./Migrations/098_FounderEnrollmentBindings.ts"; import Migration0099 from "./Migrations/099_FounderEnrollmentCorrections.ts"; +import Migration0100 from "./Migrations/100_MessagesLatestVisibleIndex.ts"; const migrationEntries = [ [1, "OrchestrationEvents", Migration0001], @@ -209,6 +210,7 @@ const migrationEntries = [ [97, "AuthSessionFounder", Migration0097], [98, "FounderEnrollmentBindings", Migration0098], [99, "FounderEnrollmentCorrections", Migration0099], + [100, "MessagesLatestVisibleIndex", Migration0100], ] as const; export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const); diff --git a/apps/server/src/persistence/Migrations/100_MessagesLatestVisibleIndex.test.ts b/apps/server/src/persistence/Migrations/100_MessagesLatestVisibleIndex.test.ts new file mode 100644 index 0000000000000..148115e47733a --- /dev/null +++ b/apps/server/src/persistence/Migrations/100_MessagesLatestVisibleIndex.test.ts @@ -0,0 +1,30 @@ +import { assert, it } from "@effect/vitest"; +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +import { runMigrations } from "../Migrations.ts"; + +const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" })); + +layer("100_MessagesLatestVisibleIndex", (it) => { + it.effect("serves a thread's newest message of any role in index order, with no sort", () => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* runMigrations(); + const plan = (yield* sql.unsafe<{ readonly detail: string }>(` + EXPLAIN QUERY PLAN + SELECT message.message_id + FROM orchestration_v2_projection_messages message + INDEXED BY orchestration_v2_projection_messages_thread_updated_idx + WHERE message.thread_id = 'thread' AND NOT (message.role = 'user' AND message.streaming = 2) + ORDER BY message.updated_at DESC, message.message_id DESC + LIMIT 1 + `)) + .map((row) => row.detail) + .join("\n"); + assert.include(plan, "USING INDEX orchestration_v2_projection_messages_thread_updated_idx"); + assert.notInclude(plan, "TEMP B-TREE"); + }), + ); +}); diff --git a/apps/server/src/persistence/Migrations/100_MessagesLatestVisibleIndex.ts b/apps/server/src/persistence/Migrations/100_MessagesLatestVisibleIndex.ts new file mode 100644 index 0000000000000..bac247046d77f --- /dev/null +++ b/apps/server/src/persistence/Migrations/100_MessagesLatestVisibleIndex.ts @@ -0,0 +1,16 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +/** + * A thread shell reads its newest visible message by `updated_at`. Only user messages had a + * matching index, so every other read sorted the thread's whole history and parsed each user + * message's JSON to filter it. + */ +export default Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + + yield* sql` + CREATE INDEX IF NOT EXISTS orchestration_v2_projection_messages_thread_updated_idx + ON orchestration_v2_projection_messages(thread_id, updated_at DESC, message_id DESC) + `; +});