Skip to content

fix(N+1): Batch fetch testruns in ProcessFlakesTask#1029

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

fix(N+1): Batch fetch testruns in ProcessFlakesTask#1029
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/n-plus-1-process-flakes-task

Conversation

@sentry
Copy link
Copy Markdown
Contributor

@sentry sentry Bot commented Jun 6, 2026

This PR addresses an N+1 query issue identified in the app.tasks.flakes.ProcessFlakesTask, specifically within the process_flakes_for_commit function in apps/worker/services/test_analytics/ta_process_flakes.py.

Problem:
The process_flakes_for_commit function iterated over a list of uploads and, for each upload, called process_single_upload. Inside process_single_upload, get_testruns(upload) was invoked, which executed a separate database query to fetch testruns for that specific upload. This resulted in 1 + N database queries (1 for uploads, N for testruns) for each commit being processed.

Solution:
To eliminate the N+1 query, the following changes were implemented:

  1. A new function, get_testruns_for_uploads, was introduced to fetch all relevant testruns for a list of upload_ids in a single batched database query. The results are then grouped by upload_id.
  2. The process_single_upload function was refactored to accept a pre-fetched list of Testrun objects directly, removing its internal database call.
  3. In process_flakes_for_commit, uploads are now evaluated to a list, and get_testruns_for_uploads is called once with all upload_ids. The grouped testruns are then passed to process_single_upload within the loop.
  4. The old get_testruns function and an unused QuerySet import were removed.

This change reduces the database query count from 1 + N to a constant 2 queries per commit, significantly improving performance for commits with many associated uploads.

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

@sentry
Copy link
Copy Markdown
Contributor Author

sentry Bot commented Jun 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.90%. Comparing base (508927b) to head (c8e42c6).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1029   +/-   ##
=======================================
  Coverage   91.90%   91.90%           
=======================================
  Files        1318     1318           
  Lines       50711    50714    +3     
  Branches     1625     1625           
=======================================
+ Hits        46604    46607    +3     
  Misses       3801     3801           
  Partials      306      306           
Flag Coverage Δ
workerintegration 58.55% <11.11%> (-0.02%) ⬇️
workerunit 90.35% <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-notifications Bot commented Jun 6, 2026

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