Skip to content

fix(test_analytics): Resolve N+1 query in ProcessFlakesTask#1214

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

fix(test_analytics): Resolve N+1 query in ProcessFlakesTask#1214
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/process-flakes-n1-query

Conversation

@sentry

@sentry sentry Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

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

Problem:
Previously, for each commit, the task would iterate through individual uploads. For every upload, it would perform a separate SELECT query to fetch testruns and then a separate UPDATE query to modify their outcomes. This led to N SELECTs and N UPDATEs for N uploads, causing an N+1 query pattern and increased database load.

Solution:

  1. Batched Testrun Fetching: Replaced the per-upload get_testruns function with a new get_testruns_for_uploads function. This new function fetches all relevant testruns for all uploads associated with a commit in a single SELECT query using upload_id__in.
  2. Inlined Logic: The logic from process_single_upload has been inlined directly into process_flakes_for_commit to streamline the processing flow.
  3. Single Bulk Update: The Testrun.objects.bulk_update call is now performed once for all collected testruns after their outcomes have been determined, rather than being executed individually for each upload.
  4. Eager Upload Evaluation: The uploads QuerySet is now evaluated eagerly using list() to prevent redundant database hits when iterating or passing it to the batched testrun fetching function.

This change transforms the N SELECT + N UPDATE operations into a single SELECT + single UPDATE, significantly reducing database interactions and 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-YTJ

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

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1214      +/-   ##
==========================================
- Coverage   91.89%   91.89%   -0.01%     
==========================================
  Files        1325     1325              
  Lines       50862    50857       -5     
  Branches     1626     1626              
==========================================
- Hits        46741    46736       -5     
  Misses       3815     3815              
  Partials      306      306              
Flag Coverage Δ
workerintegration 58.57% <6.25%> (+<0.01%) ⬆️
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