-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
fix(deletions): Performance issue when selecting rows #102960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -275,27 +275,31 @@ def update_group_hash_metadata_in_batches(hash_ids: Sequence[int]) -> None: | |
|
|
||
| # Use cursor-based pagination with the primary key to efficiently | ||
| # process large datasets without loading all IDs into memory or | ||
| # creating large NOT IN clauses | ||
| # creating large NOT IN clauses. We fetch IDs without ORDER BY to avoid | ||
| # database sorting overhead, then sort the small batch in Python. | ||
| last_max_id = 0 | ||
| while True: | ||
| # Note: hash_ids is bounded to ~100 items (deletions.group-hashes-batch-size) | ||
| # from the caller, so this IN clause is intentionally not batched | ||
| batch_metadata_ids = list( | ||
| GroupHashMetadata.objects.filter( | ||
| seer_matched_grouphash_id__in=hash_ids, id__gt=last_max_id | ||
| ) | ||
| .order_by("id") | ||
| .values_list("id", flat=True)[:batch_size] | ||
| ).values_list("id", flat=True)[:batch_size] | ||
| ) | ||
| if not batch_metadata_ids: | ||
| break | ||
|
|
||
| # Sort in Python to ensure we process lowest IDs first and can safely | ||
| # advance the cursor. Sorting a small batch (e.g., 1000 items) in Python | ||
| # is trivial and avoids database ORDER BY overhead. | ||
| batch_metadata_ids.sort() | ||
|
|
||
| updated = GroupHashMetadata.objects.filter(id__in=batch_metadata_ids).update( | ||
| seer_matched_grouphash=None | ||
| ) | ||
| metrics.incr("deletions.group_hash_metadata.rows_updated", amount=updated, sample_rate=1.0) | ||
|
|
||
| last_max_id = max(batch_metadata_ids) | ||
| last_max_id = batch_metadata_ids[-1] # Last element after sorting | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're very right. I will fix it today. |
||
|
|
||
|
|
||
| def update_group_hash_metadata_in_batches_old(hash_ids: Sequence[int]) -> int: | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Cursor-based pagination broken by missing ORDER BY
Removing
.order_by("id")from the query breaks cursor-based pagination. WithoutORDER BY, the database returns rows in arbitrary order, not necessarily the lowest IDs. After sorting in Python and advancing the cursor withlast_max_id = batch_metadata_ids[-1], any IDs between the previous cursor and the new max ID that weren't in the arbitrary batch get permanently skipped, leaving orphanedGroupHashMetadatarows that reference deletedGroupHashrows.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will follow-up on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bug mentioned here is true, however, even if we're missing some hashes we should catch it as part of the ORM deletion here:
sentry/src/sentry/deletions/defaults/group.py
Line 389 in 45c8473
If this is not the case, we can have another function that does the update with sorted IDs after this one completes.
So far I have the deletion of a group with over 1M hashes humming along for the last 20 minutes.