DQL JOIN FETCH or fetch=EAGER mapping for a collection-heavy entity that is sometimes only counted
0 reputation · 05 Jan 2026, 15:55 UTC
Scenario
An application built on Doctrine ORM has an entity with a large one-to-many collection. Some code paths iterate the full collection, while others only need its size. The default lazy loading triggers one extra query per access, and in detached states it risks a LazyInitializationException, so a more deliberate fetching strategy is needed.
The trade-off
Setting fetch: EAGER in the mapping removes the N+1 pattern everywhere but loads the collection even when only a count is required, increasing memory use and duplicating root rows in the SQL result set. Using DQL JOIN FETCH per query keeps the mapping neutral but must be repeated at every call site. The EXTRA_LAZY fetch mode appears to cover the count-only paths, since operations like count() can run as a SELECT COUNT without hydrating the collection.
Questions
- For an entity that is both fully iterated and only counted, is combining
EXTRA_LAZYmapping with explicitJOIN FETCHqueries preferable to a globalfetch: EAGER? - Does
EXTRA_LAZYcompose correctly withJOIN FETCHin the same query, or do the two modes conflict? - Besides enabling the SQL logger and comparing memory against a plain
find(), what practical checks confirm the chosen strategy actually reduces query count without hydrating the collection on count-only paths?