Optional: Resolve job reference data via per-page id__in batch-fetch#9634
Open
camd wants to merge 1 commit into
Open
Optional: Resolve job reference data via per-page id__in batch-fetch#9634camd wants to merge 1 commit into
camd wants to merge 1 commit into
Conversation
Drop the job_type/job_group/machine_platform joins from the jobs-list query and instead resolve them from per-page id__in fetches scoped to the FK ids actually present on the page. Cost scales with the page's distinct reference values instead of the whole table, with no cache/TTL/invalidation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Drops the
job_type,job_group, andmachine_platformjoins from the jobs-listquery (
JobsViewSetandJobsProjectViewSet) and instead resolves those valuesfrom a per-page
id IN (...)batch-fetch scoped to the FK ids actually present onthe page. Output is unchanged.
This is filed as Optional on purpose — see "Scope / how to weigh this" below.
Why
The jobs-list query joins
job_type, which on production is a large,churny reference table (~70k live rows; the id sequence has advanced well past
that due to
cycle_datadeletions). Because of its size, the planner builds ahash over the entire
job_typetable to satisfy the join — even when thepage only contains a handful of rows. That full-table hash scan is a roughly
fixed ~20 ms cost paid on every request to one of the busiest read endpoints in
the app.
job_group(~625 rows) andmachine_platform(~256 rows, already resolved via aMemoize'd index scan) are cheap;
job_typeis the one that hurts. Resolving thereference data from the FK ids on the page instead keeps the cost proportional to
the page's distinct values (median ~27 per push) rather than the whole table.
Measured impact (server-side DB time, staging replica, per request)
The win is largest on the common small pushes, because master pays the
full-
job_type-table hash build regardless of page size. The cost added back isthree indexed
id IN (...)fetches (in-cluster, ~1 ms total).Correctness
The batch-fetch path was verified to produce byte-identical output to the
join-based path across the full push-size distribution (20 / 284 / 2000 / 2000
rows) on production-like data. Existing
tests/webapp/api/test_jobs_api.pycoverage exercises both endpoints.
Scope / how to weigh this
This is intentionally marked Optional. It is complementary to — not a
substitute for — routing these endpoints to the read replica: that change
relocates the load off the primary, while this one reduces the cost of the query
wherever it runs. Once replica routing is in place, the primary no longer runs
these queries, so the remaining benefit here is replica CPU headroom under peak
poll QPS and a small per-poll latency improvement. Reasonable to defer if the
review/maintenance cost outweighs that.
Note: this is deliberately the join-removal-only approach. An alternative that
caches the full reference tables in Redis was prototyped and rejected — for
job_typeit adds a ~5.8 MB per-request deserialize plus periodic full-tablerebuilds, which is a net regression on the small polling requests that dominate
traffic.