Skip to content

perf(doctrine): keep to-many joins out of the translation filters - #93

Merged
paullla merged 2 commits into
Locastic:masterfrom
paullla:perf/translation-loading
Nov 18, 2026
Merged

paullla merged 2 commits into
Locastic:masterfrom
paullla:perf/translation-loading

Conversation

@paullla

@paullla paullla commented Nov 18, 2026 •

Copy link
Copy Markdown
Member
Q A
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Issues -
License MIT

Problem

Ordering a large collection by a translated field takes seconds, and no index helps. Measured on PostgreSQL 16 with 200,000 resources and 580,000 translation rows: the count query takes about 1.1 s and the identifier query 0.7 to 0.8 s.

The cause is the join of TranslationOrderFilter. It joins the to-many translations association, and the collection query also fetch-joins the translations. API Platform then runs Doctrine's Paginator with output walkers, which wrap the whole query in a subquery, so the database sorts every joined row before it applies the limit.

Changes

  • TranslationOrderFilter joins the translation entity itself (LEFT JOIN Translation t WITH IDENTITY(t.translatable) = o.id AND t.locale = :locale), not the association. The ordering is no longer on a to-many join, so the Paginator runs its count and identifier queries directly on the tables. Resources with no translation in the locale stay in the collection.
  • TranslationSearchFilter constrains through an EXISTS subquery. A join on the entity is not possible there: API Platform's FilterEagerLoadingExtension does not carry such a join into the subquery it moves the WHERE clause to.
  • When the translation entity has no field that points back to the resource (a unidirectional mapping), both filters keep the association join they used before.
  • On the same data, the count query plus the identifier query of an ordered request go from 1.8 to 2.0 s down to 0.25 to 0.29 s, with a unique index on the translatable and locale. With default indexes they take 0.60 to 0.75 s.
  • Both filters now rely on one translation per resource and locale, as the indexBy: 'locale' mapping does. The README recommends a unique constraint on the translatable and locale.
  • Eager loading, state providers and the service configuration are not changed.
  • One visible difference: when translations is embedded and not mapped EXTRA_LAZY, the current locale is no longer the first key of translations in a filtered or ordered collection. The values are the same.

Ordering a large collection by a translated field took seconds, and no
index helped. The eager-loading extension fetch-joined the translations
of all locales into the collection query, and the order filter joined
the to-many translations association. API Platform then paginates
through Doctrine's fetch-join Paginator, whose count and identifier
queries wrap the whole query, so the database sorted every joined row
before it applied the limit. Unfiltered collections paid for the join
in their count query too.

Collections now load the translations of the provided page in a second
step, as one query: a decorator of the Doctrine ORM collection provider
calls the new TranslationsLoader and tells the extension through the
context to leave the query alone. A provider that applies the query
extensions without the decorator still gets the fetch join, and item
operations keep it.

TranslationOrderFilter joins the translation entity itself, constrained
to the translatable and the locale, instead of the association. API
Platform does not see a to-many join there and paginates with a plain
LIMIT query. A scalar subquery in ORDER BY was measured too and
rejected: the database runs it once per resource. TranslationSearchFilter
constrains through an EXISTS subquery, because API Platform does not
carry a join on an entity into the subquery it moves the WHERE clause
to.

On PostgreSQL 16 with 200,000 resources and 580,000 translation rows an
ordered request goes from about 3 s (count plus identifiers) to about
0.2 s. A collection still issues three queries whatever the page size,
and responses are unchanged.
Remove the collection provider decorator and the translations loader. The
new filter joins alone give most of the gain, and they do not change how
providers, extensions and other filters work.

Use the association join when the translation entity has no field that
points back to the resource, as the filters did before.
@paullla paullla changed the title perf(doctrine): load collection translations outside the paginated query perf(doctrine): keep to-many joins out of the translation filters Nov 18, 2026
@paullla
paullla merged commit 4810eee into Locastic:master Nov 18, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant