Skip to content

perf(drizzle): 2x faster document deletion - #13618

Open
AlessioGr wants to merge 3 commits into
mainfrom
perf/deleteOne
Open

AlessioGr wants to merge 3 commits into
mainfrom
perf/deleteOne

Conversation

@AlessioGr

@AlessioGr AlessioGr commented Aug 27, 2025 •

Copy link
Copy Markdown
Member

Currently, db.deleteOne will always result in 2 db calls: finding the document, then deleting it.

However, if returning is false and no joins are required for fetching the document we want to delete, we can do this operation in one single db call: delete it while passing the original where query.

If returning is false but joins are required (e.g. due to complex where query referencing other tables), this also slightly optimizes performance by using a narrower select.

Related PR for deleteMany (already merged - simpler, as deleteMany does not return any documents, deleteOne does): #13255

In order to improve code readability, this also changes this PR to call this.findOne to find the document instead of manually calling drizzle APIs. This does add minimal overhead (buildQuery being called twice), but should not introduce additional db calls.

Local API Performance Impact

The speeds up the payload.delete() local API operation, which uses payload.db.deleteOne + returning: false internally. It does not make use of payload.db.deleteMany

=> Unlike the previous PR optimizing payload.deleteMany(), this PR will actually have a noticeable, positive performance impact when deleting documents in the admin panel - especially when using bulk delete


@github-actions

github-actions Bot commented Aug 27, 2025 •

Copy link
Copy Markdown
Contributor

📦 esbuild Bundle Analysis for payload

This analysis was generated by esbuild-bundle-analyzer. 🤖
This PR introduced no changes to the esbuild bundle! 🙌

@AlessioGr AlessioGr changed the title perf(drizzle): 2x faster db.deleteOne if not returning perf(drizzle): 2x faster document deletion Aug 27, 2025
@AlessioGr
AlessioGr enabled auto-merge (squash) August 27, 2025 22:14
@AlessioGr
AlessioGr requested a review from r1tsuu September 5, 2025 20:31
GermanJablo added a commit that referenced this pull request Sep 2, 2026
## Problem

`payload.delete({ where })` loads the matching documents, then issues
four writes *per document*: a lock cleanup, a version delete, a
scheduled-job delete and a `deleteOne`. Query count grows linearly with
N, which is what exhausts connection pools on large deletes, and turns
one call into hundreds of requests on HTTP-backed adapters. The
`deleteOne` also re-reads a row `db.find()` already loaded, for a result
nobody uses (`returning: false`).

## Change

Those writes are batched behind an `id in [...]` filter, so query count
no longer depends on N. `delete.ts` becomes three phases: per-document
pre-work (locks, `beforeDelete`, file cleanup), batched writes,
per-document post-work (`afterRead`, `afterDelete`). Hooks and
per-document error reporting still run per document; the result shape is
unchanged.

`deleteCollectionVersions` and `deleteScheduledPublishJobs` gain an
optional plural `ids`. Two batched lock helpers sit beside
`checkDocumentLockStatus`, which is itself untouched, so `update`,
`deleteByID` and globals are unaffected.
`bulkOperationsSingleTransaction` (`n: true`) keeps the per-document
path.

## Measured

Query counts from the logger already used by
`test/database/postgres-logs.int.spec.ts`:

| documents | before | after |
| --- | --- | --- |
| 2 | 13 | 8 |
| 20 | 85 | 8 |
| 200 | 805 | 8 |

## Error attribution

Unchanged for every framework-level failure: locks, `beforeDelete`, file
cleanup, `afterRead` and `afterDelete` all still report the exact
document. Access control never produced per-document errors here, since
`access.delete` is merged into the query before `db.find()`, so a denied
document never enters the batch.

Only the collection delete changes: it covers every id at once, so a
failure is reported against the batch with a message saying so, instead
of repeating a driver error against each id as though each failed
independently.

Related: #13618, #15517

---------

Co-authored-by: German Jablonski <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant