Repository navigation
perf(doctrine): keep to-many joins out of the translation filters - #93
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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-manytranslationsassociation, 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
TranslationOrderFilterjoins 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.TranslationSearchFilterconstrains through anEXISTSsubquery. A join on the entity is not possible there: API Platform'sFilterEagerLoadingExtensiondoes not carry such a join into the subquery it moves theWHEREclause to.locale. With default indexes they take 0.60 to 0.75 s.indexBy: 'locale'mapping does. The README recommends a unique constraint on the translatable andlocale.translationsis embedded and not mappedEXTRA_LAZY, the current locale is no longer the first key oftranslationsin a filtered or ordered collection. The values are the same.