Skip to content

Compute with_exists, with_max, and with_min with a subquery per parent like Eloquent - #623

Open
sulimanbenhalim wants to merge 2 commits into
laravel:0.xfrom
sulimanbenhalim:fix/aggregate-exists-subselect
Open

sulimanbenhalim wants to merge 2 commits into
laravel:0.xfrom
sulimanbenhalim:fix/aggregate-exists-subselect

Conversation

@sulimanbenhalim

Copy link
Copy Markdown
Contributor

with_exists with_max and with_min load with a grouped query over the related table so they read every related row of every parent

select posts.user_id, count(*) from posts where posts.user_id in (...) group by posts.user_id

Eloquent 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

select users.id, (exists (select * from posts where posts.user_id = users.id)) from users where users.id in (...) group by users.id

PostgreSQL 17 with 100 parents of 2000 posts each and an index on (user_id, votes)

0.x this branch
with_exists 24.0 ms 0.70 ms
with_max 24.6 ms 0.68 ms

the gap grows with the number of related rows per parent
with_count with_sum and with_avg keep the grouped query since they read every row either way

the subquery goes through the same correlate and picked(Some(keys)) as count() so self relations and through relations and of_many picks behave as before
when 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_row fails on 0.x on the grouped statements
the sqlite database suite and every postgres scenario in it pass

sulimanbenhalim and others added 2 commits December 3, 2026 02:40
…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>

Copy link
Copy Markdown
Member

This is an automated, agent-powered review.

Confirmed. with_exists, with_max, and with_min computed their value with a grouped query that reads every related row of every parent, so a page that calls User::query().with_exists(User::posts) slows down as each user's posts grow, even though the answer only needs the first matching row or one index entry. The PR's new test fails on 0.x with the grouped statements.

  • The fix is in the right place: aggregate_into now builds a correlated subquery per parent through the same correlate and picked that count() uses, and keeps the grouped query for with_count, with_sum, and with_avg, and across connections.
  • I kept the grouped query for #[belongs_to] relations. There the subquery runs once per parent row, so Post::query().with_exists(Post::editor) read every post that shares an editor instead of one user row per key. I added a test for it.
  • No breaking changes.

Thanks for your contribution, a human review will follow shortly.

@taylorotwell taylorotwell added bug p2 Priority 2: medium labels Dec 3, 2026 — with Amp for GitHub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug p2 Priority 2: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants