From 224230eaa090a73dd97bf0de056b517e54e4c181 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 30 May 2026 18:11:00 -0400 Subject: [PATCH] perf: Remove N+1 query in documents.search (#12540) --- server/models/Document.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/server/models/Document.ts b/server/models/Document.ts index 5d8c1371cc..a25ef5dfcf 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -987,16 +987,17 @@ class Document extends ArchivableModel< const findAllChildDocumentIds = async ( ...parentDocumentId: string[] ): Promise => { - const childDocuments = await ( - this.constructor as typeof Document - ).findAll({ - attributes: ["id"], - where: { - parentDocumentId, - ...where, - }, - ...options, - }); + // Unscoped as this method only ever reads the id column + const childDocuments = await (this.constructor as typeof Document) + .unscoped() + .findAll({ + attributes: ["id"], + where: { + parentDocumentId, + ...where, + }, + ...options, + }); const childDocumentIds = childDocuments.map((doc) => doc.id);