Skip to content

fix(worker): Eliminate N+1 queries in ProcessFlakesTask#1031

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

fix(worker): Eliminate N+1 queries in ProcessFlakesTask#1031
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/worker-n-plus-1-flakes-task

Conversation

@sentry
Copy link
Copy Markdown
Contributor

@sentry sentry Bot commented Jun 6, 2026

This PR addresses an N+1 query issue within the app.tasks.flakes.ProcessFlakesTask by implementing two key optimizations:

  1. Batch-fetching Testruns: Previously, process_single_upload would execute a separate database query for Testrun objects for each ReportSession (upload) within a loop. This has been refactored to batch-fetch all relevant Testrun objects for all uploads in a single query using get_testruns_for_uploads before iterating through uploads. The process_single_upload function now receives pre-fetched testruns, eliminating the N+1 query pattern.
  2. Bulk-updating Expired Flakes: The handle_pass function previously called flake.save() individually when a flake reached 30 recent passes. This has been changed to collect these 'expired' flakes into a list, which are then bulk-updated using Flake.objects.bulk_update in process_flakes_for_commit after all processing is complete. This reduces individual database write operations.

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

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