Skip to content

fix(test_analytics): eliminate N+1 query in ProcessFlakesTask#970

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

fix(test_analytics): eliminate N+1 query in ProcessFlakesTask#970
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/process-flakes-n-plus-1-ghuG0E

Conversation

@sentry
Copy link
Copy Markdown
Contributor

@sentry sentry Bot commented Jun 2, 2026

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

Root Cause:

  1. When processing flakes for a commit, the original implementation fetched uploads, then iterated through each upload, performing a separate database query to retrieve its associated testruns. This resulted in 1 (for uploads) + N (for testruns) queries.
  2. Additionally, when a flake expired (met the criteria for 30 recent passes), it triggered an individual .save() operation, leading to multiple single-row updates.

Solution:

  1. Batch Testrun Fetching: Introduced a new function get_testruns_for_uploads that fetches all relevant testruns for all uploads in a single batched query using upload_id__in. The results are then grouped by upload_id in Python.
  2. Refactored Processing: The process_single_upload function was updated to accept pre-fetched testruns, eliminating the need for per-upload database calls.
  3. Batch Flake Updates: Modified handle_pass to collect expired flakes into a list. Instead of individual .save() calls, process_flakes_for_commit now performs a single Flake.objects.bulk_update() operation for all expired flakes after processing all 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-YNZ

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