Fix N+1 query in bulk task instance delete endpoint#67304
Open
ColtenOuO wants to merge 2 commits into
Open
Conversation
vincbeck
approved these changes
May 22, 2026
jason810496
reviewed
May 22, 2026
| } | ||
| ] | ||
|
|
||
| def test_bulk_delete_does_not_requery_each_task_instance(self, test_client, session): |
Member
There was a problem hiding this comment.
It would be nice to follow the same test pattern in #66222 .
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
BulkTaskInstanceService.handle_bulk_deletere-queried the database once per task instance inside the delete loop, even though_categorize_task_instanceshad already loaded every matched task instance intotask_instances_map.The "all map index" branch of the same method already batches its lookup correctly — only the "specific
(task_id, map_index)" branch had the N+1.Database round trips (N = task instances deleted)
Each redundant
SELECTis one extra DB round trip. The fix reuses the in-memorytask_instances_map, so those N re-fetch round trips are removed:Before:
5 + 2Nqueries — N of them redundant re-fetches.After:
5 + Nqueries — exactly N round trips eliminated per bulk delete.Fix
Capture the
task_instances_mapreturned by_categorize_task_instances(previously discarded with_) and look each task instance up in it instead of issuing a freshSELECTin the loop. Every key inmatched_task_keysis a key of that map, so the lookup is guaranteed to hit.Validation plan
test_bulk_delete_does_not_requery_each_task_instance: deletes twodifferently-sized batches (5 and 15) and asserts each extra task instance
adds exactly one query — the removed re-query would roughly double the delta.
TestBulkTaskInstancessuite (27 tests) — all pass; bulk delete / update / wildcard / authorization behaviour is unchanged.Was generative AI tooling used to co-author this PR?