Skip to content

Avoid querying localizations for each alternate locale - #694

Open
joshuablum wants to merge 3 commits into
7.xfrom
fix/alternate-locales-n-plus-one
Open

joshuablum wants to merge 3 commits into
7.xfrom
fix/alternate-locales-n-plus-one

Conversation

@joshuablum

@joshuablum joshuablum commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

This pull request reduces the number of queries needed to build the alternate locales (hreflang tags), which could result in hundreds of queries on multisite installs using the Eloquent driver.

This was happening because the cascade called $entry->in($locale) twice for every other site: once to check whether the localization is published, and once to get its URL. Since Statamic 6.20.2 (statamic/cms#14773), Entry::descendants() loads the localization tree with one query per level, but the result isn't cached. So every in() call now queries the entry's descendants again, which makes the number of queries grow with the number of sites and the depth of the localization tree.

This PR fixes it by resolving all of the entry's localizations once per cascade and looking each locale up from there. Terms still use in(), since that doesn't query anything.

Measured with the Eloquent driver (queries per cascade):

Sites Localization tree Before After
10 flat 21 4
10 nested 43 9
30 nested 133 19

Related: statamic/cms#15645

@duncanmcclean
duncanmcclean marked this pull request as draft October 14, 2026 10:10
@joshuablum
joshuablum marked this pull request as ready for review October 14, 2026 16:01

This branch has not been deployed

No deployments
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.

2 participants