Skip to content

perf(bundle_analysis): Batch delete bundles to avoid N+1 queries#875

Open
sentry[bot] wants to merge 1 commit intomainfrom
seer/perf/batch-bundle-delete
Open

perf(bundle_analysis): Batch delete bundles to avoid N+1 queries#875
sentry[bot] wants to merge 1 commit intomainfrom
seer/perf/batch-bundle-delete

Conversation

@sentry
Copy link
Copy Markdown
Contributor

@sentry sentry Bot commented Apr 20, 2026

Fixes WORKER-Y98. The issue was that: Bundle deletion iterates, querying each non-cached bundle individually, causing N+1 database queries.

  • Added a new method delete_bundles_by_names to BundleReport for efficient batch deletion of multiple bundles and their associated sessions, assets, chunks, and modules.
  • Refactored the _process_bundle_report method in the worker service to collect bundle names for deletion and then use the new batch deletion method.
  • Improved performance by eliminating N+1 query issues when deleting multiple bundles that are no longer cached.

This fix was generated by Seer in Sentry, triggered automatically. 👁️ Run ID: 13573979

Not quite right? Click here to continue debugging with Seer.

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. In 2022 this entity acquired Codecov and as result Sentry is going to need some rights from me in order to utilize my contributions in this PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.


Note

Medium Risk
Changes bundle-analysis deletion logic to perform multi-row deletes in one DB session, which reduces query load but increases blast radius if the bundle name filter or session linkage is wrong. Data integrity assumptions are enforced via a new exception when bundles and sessions don’t match 1:1.

Overview
Speeds up bundle-analysis report carry-forward by batch deleting bundles that no longer have caching enabled, instead of deleting each bundle individually.

Adds BundleAnalysisReport.delete_bundles_by_names() to delete matching Bundles and their associated Session/Asset/Chunk/Module rows in a single database session, and updates the worker’s _attempt_init_from_previous_report to collect bundle names and call this new method.

Reviewed by Cursor Bugbot for commit 328e0f0. Bugbot is set up for automated code reviews on this repo. Configure here.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 328e0f0. Configure here.

if len(sessions_to_delete) != len(bundles_to_delete):
raise Exception(
"Data integrity error - cannot have Bundles without Sessions"
)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aggregate count check weakens data integrity validation

Medium Severity

The integrity check len(sessions_to_delete) != len(bundles_to_delete) only compares aggregate counts, unlike the original one_or_none() which validated each bundle individually. If one bundle has two sessions and another has zero, the counts balance out (e.g., 2 sessions == 2 bundles) and the check passes — silently masking a real data integrity issue where a bundle exists without a session. The original per-bundle one_or_none() would have caught both the missing-session and the multiple-sessions cases independently.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 328e0f0. Configure here.

@sentry
Copy link
Copy Markdown
Contributor Author

sentry Bot commented Apr 20, 2026

Codecov Report

❌ Patch coverage is 9.52381% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.22%. Comparing base (0ad8a0c) to head (328e0f0).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
libs/shared/shared/bundle_analysis/report.py 9.52% 19 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #875      +/-   ##
==========================================
- Coverage   92.25%   92.22%   -0.04%     
==========================================
  Files        1307     1307              
  Lines       48017    48037      +20     
  Branches     1636     1642       +6     
==========================================
+ Hits        44299    44300       +1     
- Misses       3407     3426      +19     
  Partials      311      311              
Flag Coverage Δ
apiunit 96.35% <ø> (ø)
sharedintegration 36.85% <9.52%> (-0.04%) ⬇️
sharedunit 84.79% <9.52%> (-0.11%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@codecov-notifications
Copy link
Copy Markdown

codecov-notifications Bot commented Apr 20, 2026

Codecov Report

❌ Patch coverage is 9.52381% with 19 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
libs/shared/shared/bundle_analysis/report.py 9.52% 19 Missing ⚠️

📢 Thoughts on this report? Let us know!

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 20, 2026

Merging this PR will not alter performance

✅ 9 untouched benchmarks


Comparing seer/perf/batch-bundle-delete (328e0f0) with main (0ad8a0c)

Open in CodSpeed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants