[6.x] Resolve Entry::descendants() N+1 query#14773
Merged
jasonvarga merged 4 commits intoJun 3, 2026
Merged
Conversation
descendants() recursed into every localization, calling directDescendants() (one query) per node, which is O(number of localizations). It now walks the tree breadth-first, fetching each level in a single batched whereIn.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR optimizes entry descendant resolution to avoid N+1 queries across localizations, and adds tests to enforce the new query behavior and correctness across origin chains.
Changes:
- Reworked
Entry::descendants()to traverse localization descendants breadth-first with one query per depth level. - Added tests to assert the query count is level-based (not per-localization) and to validate nested origin-chain descendants are included.
- Added a test helper for faking descendant queries.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/Entries/Entry.php |
Switches descendants traversal to breadth-first batched queries to reduce query count. |
tests/Data/Entries/EntryTest.php |
Adds regression tests for query batching and origin-chain descendant inclusion. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1634
to
+1637
| $query = Mockery::mock(QueryBuilder::class); | ||
| $query->shouldReceive('where')->andReturnSelf(); | ||
| $query->shouldReceive('whereIn')->andReturnSelf(); | ||
| $query->shouldReceive('get')->andReturn($results); |
Track visited IDs so cyclic or duplicate origin data cannot loop forever, and assert the batched whereIn receives the expected origin IDs per level.
The batched levels use whereIn, not where, so stubbing where('origin')
on those mocks was misleading.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Member
|
Very nice, thanks. |
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.
Resolves #14767.
Entry::descendants()resolved the localization tree by recursing into every node and callingdirectDescendants()(one query) per localization, which is O(number of localizations) in queries. BecauseRouting\ResolveRedirectcalls$entry->in($site)(which goes throughdescendants()) for every entry-link field, a page on a heavily localized multi-site can fire hundreds of queries just resolving link targets.This walks the tree breadth-first instead, fetching each level in a single batched
whereIn('origin', ...).directDescendants()is left unchanged, so its Blink cache and the existing invalidation continue to work, and the returned set is identical.Measured on a ~70-locale site (v6.20.0)
A root entry with 41 localizations:
descendants()issued 44 queriesTests
Added to
tests/Data/Entries/EntryTest.php:it_resolves_descendants_with_one_query_per_level_rather_than_one_per_localizationasserts a flat tree takes a fixed number of queries no matter how many localizations exist. It fails on the previous one-query-per-node implementation.it_includes_descendants_nested_via_an_origin_chainasserts a grandchild reachable only through a nested origin chain is still included, so the flattened result is unchanged.