Skip to content

Limit combined attachment eager loading to requested fields - #678

Merged
daftspunk merged 4 commits into
octobercms:developfrom
samuelpatro:perf/attachment-fields
Sep 24, 2026
Merged

daftspunk merged 4 commits into
octobercms:developfrom
samuelpatro:perf/attachment-fields

Conversation

@samuelpatro

@samuelpatro samuelpatro commented Sep 5, 2026 •

Copy link
Copy Markdown
Member

Eager-loading one attachment field currently fetches every attachment for the selected owners, then filters the hydrated models by field. An entry with one cover and 100 gallery files therefore hydrates 101 file records for with('cover').

Restrict the combined attachment query to the requested fields that share the same related model, while keeping one query for multiple fields. The combined result cache is reset at the start of each eager-load pass so a reused builder loads the correct owners and fields. Scoped/conditional relations, combineEager => false, and first-constraint behavior are preserved.

10 regression tests cover cover-only hydration (101 → 1 file, one attachment query), multiple owners and fields, distinct related classes, opt-outs, nested loads, custom constraints, relation hooks, and reused builders. Full suite on PHP 8.4: 240 tests, 1493 assertions.

Rechecked on 2026-09-18 with PHP 8.4 / SQLite. Seven samples of ten warm cover-only loads (fixture setup excluded): with 100 unrelated gallery files, hydrated files drop from 101 to 1 and median time from 0.618 ms to 0.100 ms; with 1000 gallery files, from 1001 to 1 and 5.740 ms to 0.130 ms. Both versions issue two total queries (owner plus attachments). These are local illustrative timings; work-count tests are the regression gate. Full suite remains 240 tests / 1493 assertions.

Rechecked after the 2026-09-21 upstream merge: qualified the requested-field filter so eager constraints joining another table with a field column remain valid. The joined-query regression fails before the fix and passes after it, retaining one attachment query. PHP 8.4 branch suite: 242 tests / 1501 assertions; one pre-existing risky test without assertions. Combined integration with current 4.x and all seven open PRs passes 318 tests / 1762 assertions after retaining both sides of two merge conflicts (DbDatasource tests and Database Builder methods). This combined checkout is validation only; other PRs and 4.x were not merged into this branch.

@samuelpatro
samuelpatro marked this pull request as ready for review September 18, 2026 07:42
@daftspunk
daftspunk merged commit 31354a7 into octobercms:develop Sep 24, 2026
4 checks passed
@daftspunk

Copy link
Copy Markdown
Member

Wow! 🤯 Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants