Repository navigation
Compute with_exists, with_max, and with_min with a subquery per parent like Eloquent - #623
Open
sulimanbenhalim wants to merge 2 commits into
Open
sulimanbenhalim wants to merge 2 commits into
sulimanbenhalim wants to merge 2 commits into
Conversation
…ery per parent instead of grouping every related row.
A belongs_to's grouped query reads one related row per key, while a subquery per parent reads every parent row that holds one of the keys. Amp-Thread-ID: https://ampcode.com/threads/T-01a23960-5dcd-74ba-a1c6-8a2250094514 Co-authored-by: Taylor Otwell <taylor@laravel.com>
Member
|
This is an automated, agent-powered review. Confirmed.
Thanks for your contribution, a human review will follow shortly. |
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.
with_existswith_maxandwith_minload with a grouped query over the related table so they read every related row of every parentEloquent selects a correlated subquery per parent instead
exists (select * from posts where posts.user_id = users.id)stops at the first row and(select max(votes) ...)is one index probe with an index on(user_id, votes)so now those three run like Eloquent's from the parent table one row per key
PostgreSQL 17 with 100 parents of 2000 posts each and an index on
(user_id, votes)0.xwith_exists24.0 ms0.70 mswith_max24.6 ms0.68 msthe gap grows with the number of related rows per parent
with_countwith_sumandwith_avgkeep the grouped query since they read every row either waythe subquery goes through the same
correlateandpicked(Some(keys))ascount()so self relations and through relations andof_manypicks behave as beforewhen the related query runs on another connection than the parents it keeps the grouped query since a subquery can't cross connections
exists now decodes as a bool on both paths
with_exists_max_and_min_read_one_row_per_parent_not_every_related_rowfails on0.xon the grouped statementsthe sqlite database suite and every postgres scenario in it pass