From 07d9311a6ccab3757f07de3c72ffed91c147e849 Mon Sep 17 00:00:00 2001 From: Igor Ohrimenko Date: Tue, 5 May 2026 17:40:07 +0300 Subject: [PATCH 1/2] chore: add compound index { tmid: 1, ts: -1 } to messages collection Speeds up paginated thread reply loads (chat.getThreadMessages) on large workspaces. Without this index the query planner can pick an IXSCAN over ts_1 and filter by tmid in memory, which becomes a near full collection scan once the messages collection grows large. --- .changeset/compound-index-thread-replies.md | 5 +++++ packages/models/src/models/Messages.ts | 1 + 2 files changed, 6 insertions(+) create mode 100644 .changeset/compound-index-thread-replies.md diff --git a/.changeset/compound-index-thread-replies.md b/.changeset/compound-index-thread-replies.md new file mode 100644 index 0000000000000..91e3f8ad71516 --- /dev/null +++ b/.changeset/compound-index-thread-replies.md @@ -0,0 +1,5 @@ +--- +'@rocket.chat/meteor': patch +--- + +Speed up paginated thread reply loads on large workspaces by adding a compound index `{ tmid: 1, ts: -1 }` on the messages collection. Without it, the query planner can fall back to scanning the `ts_1` index in time order and filtering by `tmid` in memory, which becomes very expensive on collections with millions of messages. diff --git a/packages/models/src/models/Messages.ts b/packages/models/src/models/Messages.ts index eaa7b08b60c54..c5f99b5971302 100644 --- a/packages/models/src/models/Messages.ts +++ b/packages/models/src/models/Messages.ts @@ -69,6 +69,7 @@ export class MessagesRaw extends BaseRaw implements IMessagesModel { // threads { key: { tmid: 1 }, sparse: true }, + { key: { tmid: 1, ts: -1 }, sparse: true }, // used for paginated thread reply loads (chat.getThreadMessages) { key: { tcount: 1, tlm: 1 }, sparse: true }, { key: { rid: 1, tlm: -1 }, partialFilterExpression: { tcount: { $exists: true } } }, // used for the List Threads { key: { rid: 1, tcount: 1 } }, // used for the List Threads Count From 79ac06a84a84b4f30dc4c92c8875520f7aee2d21 Mon Sep 17 00:00:00 2001 From: Igor Ohrimenko Date: Mon, 27 Jul 2026 20:15:27 +0300 Subject: [PATCH 2/2] chore: drop inline comment from tmid_1_ts_-1 index definition --- packages/models/src/models/Messages.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/models/src/models/Messages.ts b/packages/models/src/models/Messages.ts index c5f99b5971302..e375a8efd618f 100644 --- a/packages/models/src/models/Messages.ts +++ b/packages/models/src/models/Messages.ts @@ -69,7 +69,7 @@ export class MessagesRaw extends BaseRaw implements IMessagesModel { // threads { key: { tmid: 1 }, sparse: true }, - { key: { tmid: 1, ts: -1 }, sparse: true }, // used for paginated thread reply loads (chat.getThreadMessages) + { key: { tmid: 1, ts: -1 }, sparse: true }, { key: { tcount: 1, tlm: 1 }, sparse: true }, { key: { rid: 1, tlm: -1 }, partialFilterExpression: { tcount: { $exists: true } } }, // used for the List Threads { key: { rid: 1, tcount: 1 } }, // used for the List Threads Count