XF 2.1 Phrases and queries

Valhalla

Well-known member
I have a 'template_setup' listener to create a custom template function.

Part of the logic of this function fetches phrases to build a specifically generated string. I am seeing that, in cases where this function is used, this is generating a high number of phrase queries that is being maintained (there are cases where I use the fuction through a template, and cases where I return the results of the function through my code).

It is my understanding that unknown phrases would be fetched (and this would explaining a high query count on an initial page load), only to drop back down once the phrases had been. But the phrases being referenced are XF stock phrases - they should surely be known already, and should they be being fetched consistently?

(I am also not seeing one query to fetch the phrases, but rather 14 queries for each phrase title.)

SELECT title, phrase_text FROM xf_phrase_compiled WHERE language_id = ? AND title IN ('a') SELECT title, phrase_text FROM xf_phrase_compiled WHERE language_id = ? AND title IN ('b')
 
Phrases used directly in templates (i.e. using {{ phrase() }}) are actually saved into the compiled template at compile time, so no queries there.

But each time a phrase is called in code (i.e. using \XF::phrase()) then unless the phrase is preloaded or coming from a cache, it will generate a query per phrase.

This is one use case where phrase groups are useful because they are compiled to a file, e.g. button.attach, button.cancel get compiled to the button phrase group cache (the bit before the dot is the name of the phrase group). These don't cost a query either.

However, you would have to be using your own phrases in your own phrase group.

The other alternative is you would have to take control of pre-loading the phrases yourself. I think you can get away with doing that in your Templater setup event before those phrases are used:

PHP:
\XF::language()->requirePhrases([
   'posts', 'threads', 'forum'
]);

That will generate a single additional query to load those phrases so subsequent uses of them don't cost any queries individually.
 
Top Bottom