Repository navigation
Pick of-many rows with a grouped max or min join - #614
Conversation
Eager loading a latest_of_many relation kept the row whose key equals a subquery correlated with each related row. MySQL runs that subquery once for every related row, so the load read (related rows) x (related rows per parent): 57 seconds for 40 parents with 2,000 rows each. Loading, counting and lazily querying the relation now number the rows of the parents being loaded with row_number(), once, and keep each parent's first. has() and where_has() still correlate with the parent table, as they did. Refs #534 Co-authored-by: Taylor Otwell <taylor@laravel.com>
Every of_many path now picks the way Eloquent does: a grouped subquery finds each parent's largest or smallest value of the column, the related rows join it, and row_number() breaks ties by key among the rows left. The subquery never refers to the outer row, so it runs once per query. This replaces both the ranked pick that loading used and the correlated pick that has(), where_has(), exists_where(), count(), and count_where() still used. On MySQL with 40 parents and 1,000 rows each, has() on an of_many relation drops from 7.6 s to under 2 ms, and eager loading from about 24 ms to about 2.5 ms. PostgreSQL has no max() for uuid, so ModelKey::AGGREGATES lets a Uuid key, and a newtype over one, rank every related row when picking by key. Amp-Thread-ID: https://ampcode.com/threads/T-b3ca1dbf-3c50-4286-a702-945a9f477ede Co-authored-by: Taylor Otwell <taylor@laravel.com>
|
This is an automated, agent-powered review. The bug is confirmed. On MySQL with 40 environments and 1,000 deployments each, indexed on Two things to look at in the human review. |
Fixes #534. Supersedes #546, whose commit this keeps.
Eager loading a
latest_of_many,oldest_of_many, orof_manyrelation kept each related row whose key equals a subquery correlated with that row. MySQL runs that subquery once for every related row the outerinmatches, so a load read (related rows) × (related rows per parent): about 80 seconds for 40 parents with 2,000 rows each.has,where_has,exists_where,count, andcount_whereused the same correlated pick.Change
Every
of_manypath now picks the way Eloquent'sofManydoes. A grouped subquery finds each parent's largest or smallest value of the column, and the related rows join it.row_number()then breaks ties by key among the few rows left:The subquery never refers to the outer row, so a database runs it once per query. Loading, aggregates, and the relation query from one parent pass the parents' keys into the grouped subquery.
hasandcountleave the keys out, as Eloquent does. NULL skipping, the key tie-break, conditions that apply after the pick, and through, morph, filtered, and self relations behave as before.PostgreSQL has no
maxforuuid. The newModelKey::AGGREGATESconstant istrueby default andfalseforUuidand newtypes over it. For those keys, a relation that picks by key ranks all of a parent's rows instead of grouping.Numbers
MySQL 8.4, 40 parents × 1,000 rows, index on
(environment_id, size), the SQL the framework emits, 3 runs each:0.xof_many(size, max)has(of_many(size, max))of_many(size, max).count().ge(1)Eloquent's SQL for the same eager load and
hastook 1.7–2.1 ms and 1.1 ms on the same data.Tests
of_many_picks_among_the_parents_rows_at_once(from Pick of-many rows with one ranked subquery when loading #546) checks loading,with_count, and the relation query pick with one ranked query.of_many_picks_by_uuid_keys_and_in_self_relationscovers aUuid-keyedlatest_of_manyandof_many(max)with a tie, throughwith,has_where, andcount, andhas/has_whereon a self-relation. WithUuid'sAGGREGATESset totrue, it fails on PostgreSQL withfunction max(uuid) does not exist.The
model_tests pass on SQLite, MySQL, and PostgreSQL.