Skip to content

fix(test_analytics): Eliminate N+1 queries in flake processing#1210

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

fix(test_analytics): Eliminate N+1 queries in flake processing#1210
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/ta-flakes-n1-query-h9baI5

Conversation

@sentry

@sentry sentry Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

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

Problem:

  1. Previously, process_flakes_for_commit would iterate over each upload and call get_testruns(upload), resulting in a separate database query for Testrun objects for every single upload. This led to an N+1 query pattern where N is the number of uploads.
  2. Additionally, handle_pass would call .save() individually for each Flake object that reached 30 recent passes, leading to multiple single-row updates.

Solution:

  1. Introduced get_testruns_for_uploads to fetch all relevant Testrun objects for all uploads in a single batched query using upload_id__in. These testruns are then grouped by upload_id and passed to process_single_upload.
  2. Modified handle_pass to collect expired Flake objects into a list. After processing all uploads, these collected flakes are updated in a single Flake.objects.bulk_update call.

Impact:
This change significantly reduces the number of database queries executed during flake processing, improving the performance and efficiency 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-YTE

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