Skip to content

fix(worker): Resolve N+1 query in ProcessFlakesTask#1208

Open
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/worker-n-plus-1-flakes-gVleKx
Open

fix(worker): Resolve N+1 query in ProcessFlakesTask#1208
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/worker-n-plus-1-flakes-gVleKx

Conversation

@sentry

@sentry sentry Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

This PR addresses an N+1 query issue identified in the app.tasks.flakes.ProcessFlakesTask.

Root Cause:
The process_flakes_for_commit function was iterating over each ReportSession upload and, for each upload, calling process_single_upload. Inside process_single_upload, get_testruns(upload) was being called, which executed a separate database query (SELECT ... FROM Testrun WHERE upload_id = ?) for every single upload. This resulted in N+1 queries, where N is the number of uploads associated with a commit.

Solution:
To eliminate the N+1 query, the following changes were implemented in apps/worker/services/test_analytics/ta_process_flakes.py:

  1. A new function, get_testruns_for_uploads, was added to fetch all relevant Testrun objects for all uploads associated with a commit in a single bulk query using upload_id__in.
  2. The fetched Testrun objects are then grouped by their upload_id into a dictionary.
  3. The process_single_upload function was modified to accept a pre-fetched list of Testrun objects for a specific upload, removing its internal database query.
  4. All modified Testrun objects are now collected throughout the processing loop, and a single Testrun.objects.bulk_update() is performed after the loop completes, further reducing database interactions.

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-YTC

@codecov

codecov Bot commented Jun 19, 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 (a23289b).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1208      +/-   ##
==========================================
- Coverage   91.89%   91.89%   -0.01%     
==========================================
  Files        1325     1325              
  Lines       50862    50873      +11     
  Branches     1626     1626              
==========================================
+ Hits        46741    46750       +9     
- Misses       3815     3817       +2     
  Partials      306      306              
Flag Coverage Δ
workerintegration 58.53% <13.33%> (-0.03%) ⬇️
workerunit 90.37% <100.00%> (-0.01%) ⬇️

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