Don't eager load when plucking with includes/limit #36273
Closed
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.
Summary
Consider a
Company
c
that has manyContract
s, and the following statement:The
pluck
runs two queries, one to get distinct values of the main record primary key, which has theLIMIT
applied, and a second to get the pluck values, with is filtered by the IDs retrieved in the first query:The limit is applied only to the number of records retrieved only from the first query, which means that in the
pluck
above, multiple results are returned, despite asking for 1.As well, running a second query has a performance impact, especially if the query isn't hitting an appropriate index (as I found in my production app). In the best case scenario, it's still a second query for no benefit. Since a
pluck
statement isn't going to instantiate any AR objects, nothing will be preloaded anyways.This change makes
includes.limit.pluck
behave identically toleft_joins.limit.pluck
(same queries, same results).Fixes #36217.