Skip to content

fix(worker): N+1 Query in ProcessFlakesTask#1028

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

fix(worker): N+1 Query in ProcessFlakesTask#1028
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/worker-n-plus-1-flakes-cyrNOo

Conversation

@sentry
Copy link
Copy Markdown
Contributor

@sentry sentry Bot commented Jun 6, 2026

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

Problem:
Previously, process_flakes_for_commit iterated over each upload and called process_single_upload. Inside process_single_upload, get_testruns would perform a SELECT query for that specific upload, and then Testrun.objects.bulk_update would perform an UPDATE query for the testruns associated with that upload. This resulted in 2N database queries (N SELECTs + N UPDATEs) where N is the number of uploads for a given commit.

Solution:

  1. Refactored get_testruns: It now accepts a list of upload_ids and fetches all relevant testruns in a single SELECT query using upload_id__in.
  2. Pre-fetched Testruns: In process_flakes_for_commit, all testruns for all uploads are fetched once at the beginning.
  3. Updated process_single_upload: This function now receives the pre-fetched testruns relevant to its specific upload, eliminating its internal SELECT call.
  4. Single bulk_update: All modified testruns are collected, and a single Testrun.objects.bulk_update is performed after the loop, updating all outcomes in one go.

Benefit:
This change reduces the database queries from 2N to a constant 2 (one batched SELECT and one batched UPDATE), significantly improving performance for commits with many 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-YQ5

@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 (b899a12).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1028   +/-   ##
=======================================
  Coverage   91.90%   91.90%           
=======================================
  Files        1318     1318           
  Lines       50711    50715    +4     
  Branches     1625     1625           
=======================================
+ Hits        46604    46608    +4     
  Misses       3801     3801           
  Partials      306      306           
Flag Coverage Δ
workerintegration 58.54% <9.09%> (-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