Repository navigation
Commit bc92d3b
authored
Fix N+1 query in evolution chain serializer (#1688)
* Fix N+1 query in evolution chain serializer
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.
* Skip evolution query for single-species chains and pin detail order
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).1 parent 89289e3 commit bc92d3b
2 files changed
Lines changed: 96 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
3487 | 3488 | | |
3488 | 3489 | | |
3489 | 3490 | | |
| 3491 | + | |
| 3492 | + | |
| 3493 | + | |
| 3494 | + | |
| 3495 | + | |
| 3496 | + | |
| 3497 | + | |
| 3498 | + | |
| 3499 | + | |
3490 | 3500 | | |
3491 | | - | |
| 3501 | + | |
3492 | 3502 | | |
3493 | 3503 | | |
3494 | 3504 | | |
| |||
3525 | 3535 | | |
3526 | 3536 | | |
3527 | 3537 | | |
3528 | | - | |
| 3538 | + | |
| 3539 | + | |
| 3540 | + | |
| 3541 | + | |
3529 | 3542 | | |
3530 | 3543 | | |
3531 | 3544 | | |
3532 | 3545 | | |
3533 | 3546 | | |
3534 | | - | |
3535 | | - | |
3536 | | - | |
| 3547 | + | |
3537 | 3548 | | |
3538 | 3549 | | |
3539 | 3550 | | |
| |||
3543 | 3554 | | |
3544 | 3555 | | |
3545 | 3556 | | |
3546 | | - | |
| 3557 | + | |
| 3558 | + | |
| 3559 | + | |
3547 | 3560 | | |
3548 | 3561 | | |
3549 | 3562 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
| |||
4976 | 4978 | | |
4977 | 4979 | | |
4978 | 4980 | | |
| 4981 | + | |
| 4982 | + | |
| 4983 | + | |
| 4984 | + | |
| 4985 | + | |
| 4986 | + | |
| 4987 | + | |
| 4988 | + | |
| 4989 | + | |
| 4990 | + | |
| 4991 | + | |
| 4992 | + | |
| 4993 | + | |
| 4994 | + | |
| 4995 | + | |
| 4996 | + | |
| 4997 | + | |
| 4998 | + | |
| 4999 | + | |
| 5000 | + | |
| 5001 | + | |
| 5002 | + | |
| 5003 | + | |
| 5004 | + | |
| 5005 | + | |
| 5006 | + | |
| 5007 | + | |
| 5008 | + | |
| 5009 | + | |
| 5010 | + | |
| 5011 | + | |
| 5012 | + | |
| 5013 | + | |
| 5014 | + | |
| 5015 | + | |
| 5016 | + | |
| 5017 | + | |
| 5018 | + | |
| 5019 | + | |
| 5020 | + | |
| 5021 | + | |
| 5022 | + | |
| 5023 | + | |
| 5024 | + | |
| 5025 | + | |
| 5026 | + | |
| 5027 | + | |
| 5028 | + | |
| 5029 | + | |
| 5030 | + | |
| 5031 | + | |
| 5032 | + | |
| 5033 | + | |
| 5034 | + | |
| 5035 | + | |
| 5036 | + | |
| 5037 | + | |
| 5038 | + | |
| 5039 | + | |
| 5040 | + | |
| 5041 | + | |
| 5042 | + | |
| 5043 | + | |
| 5044 | + | |
| 5045 | + | |
| 5046 | + | |
| 5047 | + | |
| 5048 | + | |
| 5049 | + | |
| 5050 | + | |
| 5051 | + | |
| 5052 | + | |
| 5053 | + | |
| 5054 | + | |
| 5055 | + | |
4979 | 5056 | | |
4980 | 5057 | | |
4981 | 5058 | | |
| |||
0 commit comments