Fix N+1 query in evolution chain serializer - #1688
Merged
FallenDeity merged 3 commits intoSep 30, 2026
Merged
Conversation
build_chain_link_entry ran a fresh PokemonEvolution query (with ~13 select_related joins) for every non-root species while recursing through the evolution tree, so branching chains like Eevee's fired one query per branch instead of one query for the whole chain. Fetch all PokemonEvolution rows for the chain once, group them by evolved_species_id, and look them up from that dict during the recursion instead of querying per node.
FallenDeity
requested changes
Sep 28, 2026
Chains with no evolutions (199 of 540 in the current data) don't need the batched PokemonEvolution query at all; the old per-node code ran zero queries for them, so the unconditional batch query made those requests one query heavier. Only run it when some species in the chain evolves from another. Also make the pk ordering explicit on the batched query. PokeApiManager already orders by pk, so this doesn't change behavior; it just keeps the ordering visible at the call site. Add tests for both: a single-species chain issues no PokemonEvolution query, and evolution_details keeps creation order for a species with several evolution rows (e.g. Milcery -> Alcremie).
FallenDeity
approved these changes
Sep 30, 2026
|
A PokeAPI/api-data refresh has started. In ~45 minutes the staging branch of PokeAPI/api-data will be pushed with the new generated data. |
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.
Problem
EvolutionChainDetailSerializer.build_chain_link_entryrecurses over the evolution tree and, for every non-root species, ran its ownPokemonEvolution.objects.filter(evolved_species=...)query with ~13select_relatedjoins. A linear 3-stage chain fires 2 of these queries; Eevee's 9-species chain fires 8.Fix
Fetch all
PokemonEvolutionrows for the whole chain in a single query (filter(evolved_species__evolution_chain=obj)), group them in memory byevolved_species_id, and look them up from that dict during the recursion instead of querying per node. The query only runs when some species in the chain evolves from another, so single-species chains still issue no evolution query at all. The pk ordering (already applied byPokeApiManager) is spelled out explicitly at the call site.Performance impact
Measured against the full dataset (DB built from the CSVs, all 540 evolution chains serialized with the old and new code):
PokemonEvolutionSerializer(condition_expression/allowed_natures), handled separately in Avoid per-row queries for evolution condition variables and natures #1690.Test plan
test_evolution_chain_api_query_count_does_not_scale_with_chain_size: a 1-branch and a 6-branch chain issue the same number of queries (fails with8 != 3without the fix).test_evolution_chain_api_single_species_chain_skips_evolution_query: a single-species chain issues noPokemonEvolutionquery.test_evolution_chain_api_evolution_details_order_with_many_rows_for_same_species:evolution_detailskeeps creation order for a species with several evolution rows.manage.py test pokemon_v2passes (66 tests);ruff check,ruff format --checkandtypass.