perf(drizzle): 2x faster document deletion #13618
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, db.deleteOne will always result in 2 db calls: finding the document, then deleting it.
However, if
returningisfalseand 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
returningisfalsebut joins are required (e.g. due to complex where query referencing other tables), this also slightly optimizes performance by using a narrowerselect.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.findOneto find the document instead of manually calling drizzle APIs. This does add minimal overhead (buildQuerybeing called twice), but should not introduce additional db calls.Local API Performance Impact
The speeds up the
payload.delete()local API operation, which usespayload.db.deleteOne+returning: falseinternally. It does not make use ofpayload.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