Skip to content

fix(worker): Eliminate N+1 query in ProcessFlakesTask#1220

Open
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/worker-n1-process-flakes-FOud2q
Open

fix(worker): Eliminate N+1 query in ProcessFlakesTask#1220
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/worker-n1-process-flakes-FOud2q

Conversation

@sentry

@sentry sentry Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

This PR addresses an N+1 query performance issue within the app.tasks.flakes.ProcessFlakesTask, specifically in the process_flakes_for_commit function.

Problem:
Previously, when processing test analytics flakes for a commit, the process_flakes_for_commit function iterated through each ReportSession (upload) and called process_single_upload for every one. Each call to process_single_upload would execute a SELECT query to retrieve testruns for that specific upload and then a bulk_update to save their outcomes. This resulted in an N+1 query pattern, leading to 2N database queries (N SELECTs + N UPDATEs) for N uploads, causing inefficient database usage and increased task execution time.

Solution:
This change refactors the flake processing logic to eliminate the N+1 query:

  1. A new helper function, get_testruns_for_uploads, was introduced to fetch all relevant Testrun objects for all uploads associated with a commit in a single SELECT query using upload_id__in.
  2. The process_flakes_for_commit function now fetches all testruns for the commit's uploads in one go.
  3. The logic to process each testrun (applying handle_pass or handle_failure) is performed in a single loop over the combined list of all fetched testruns.
  4. Finally, a single Testrun.objects.bulk_update is performed to persist all changes to the outcome field for all processed testruns.

This optimization reduces the database operations from 2N queries to just 2 queries (1 SELECT + 1 bulk_update), significantly improving the performance of the ProcessFlakesTask.

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.

Fixes WORKER-YTN

@codecov

codecov Bot commented Jun 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.89%. Comparing base (7b60783) to head (4a363f4).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1220      +/-   ##
==========================================
- Coverage   91.89%   91.89%   -0.01%     
==========================================
  Files        1325     1325              
  Lines       50862    50860       -2     
  Branches     1626     1626              
==========================================
- Hits        46741    46737       -4     
- Misses       3815     3817       +2     
  Partials      306      306              
Flag Coverage Δ
workerintegration 58.56% <6.25%> (+<0.01%) ⬆️
workerunit 90.36% <100.00%> (-0.02%) ⬇️

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

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

@codecov-notifications

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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