From 2bf160ec39e2657df3fd91e14515a6db37719fe6 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 5 Aug 2026 20:44:29 -0400 Subject: [PATCH] perf: Avoid N+1 queries when deleting a document subtree (#13311) Deleting a document walked its subtree with one query per document and destroyed each in a serial post-order recursion, so large subtrees could exceed the request statement_timeout. Resolve the subtree with the existing recursive CTE helper and load the children in a single query instead. Documents are still destroyed one at a time so model hooks run for each, and deepest-first ordering is kept. The subtree lookup also dropped the options it was passed, so it ran outside the request transaction. Co-authored-by: Claude Opus 5 (1M context) --- server/models/Collection.test.ts | 30 ++++++++++++++++++++++++++++++ server/models/Collection.ts | 27 +++++++++++++++------------ 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/server/models/Collection.test.ts b/server/models/Collection.test.ts index 6c4d2aadb0..7f888c87c7 100644 --- a/server/models/Collection.test.ts +++ b/server/models/Collection.test.ts @@ -376,6 +376,36 @@ describe("#removeDocument", () => { expect(collectionDocuments.count).toBe(0); }); + it("should remove a document with deeply nested child documents", async () => { + const collection = await buildCollection(); + const document = await buildDocument({ collectionId: collection.id }); + await collection.reload(); + + let parentDocumentId = document.id; + for (let depth = 0; depth < 5; depth++) { + const child = await buildDocument({ + parentDocumentId, + collectionId: collection.id, + teamId: collection.teamId, + lastModifiedById: collection.createdById, + createdById: collection.createdById, + title: `Child document ${depth}`, + text: "content", + }); + await collection.addDocumentToStructure(child); + parentDocumentId = child.id; + } + + await collection.deleteDocument(document); + expect(collection.documentStructure!.length).toBe(0); + const collectionDocuments = await Document.findAndCountAll({ + where: { + collectionId: collection.id, + }, + }); + expect(collectionDocuments.count).toBe(0); + }); + it("should remove a child document", async () => { const collection = await buildCollection(); const document = await buildDocument({ collectionId: collection.id }); diff --git a/server/models/Collection.ts b/server/models/Collection.ts index afda6b30a4..8ecf9bff3f 100644 --- a/server/models/Collection.ts +++ b/server/models/Collection.ts @@ -1,6 +1,6 @@ /* oxlint-disable lines-between-class-members */ import fractionalIndex from "fractional-index"; -import { find, findIndex, isNil, remove, uniq } from "es-toolkit/compat"; +import { find, findIndex, isNil, keyBy, remove, uniq } from "es-toolkit/compat"; import type { Identifier, Transaction, @@ -860,24 +860,27 @@ class Collection extends ParanoidModel< deleteDocument = async (document: Document, options?: FindOptions) => { await this.removeDocumentInStructure(document, options); - // Helper to destroy all child documents for a document - const loopChildren = async ( - documentId: string, - opts?: FindOptions - ) => { + // IDs come back breadth-first so reversing them destroys the deepest + // descendants first. + const childDocumentIds = ( + await document.findAllChildDocumentIds(undefined, options) + ).reverse(); + + if (childDocumentIds.length) { const childDocuments = await Document.findAll({ + ...options, where: { - parentDocumentId: documentId, + id: childDocumentIds, }, }); + const childDocumentsById = keyBy(childDocuments, (child) => child.id); - for (const child of childDocuments) { - await loopChildren(child.id, opts); - await child.destroy(opts); + // Destroyed one at a time to ensure model hooks run for each document. + for (const childDocumentId of childDocumentIds) { + await childDocumentsById[childDocumentId]?.destroy(options); } - }; + } - await loopChildren(document.id, options); await document.destroy(options); };