Experiencing performance issues I tried to dig into possible reasons, and this is what I found:
I had added an Options field to a template and checked 'Autojoin' because I wanted to sort by this field.
This template is used by several thousand pages in my site.
When one single page is rendered to the frontend, one database query for each of these 'sibling' pages is run. I.e. rendering a single page causes thousands of database queries.
The queries select the current option value for every sibling page of the current page. This is a snippet from the query log with the queries in question:
SELECT * FROM fieldtype_options WHERE fields_id=:fields_id AND option_id IN(4)
SELECT * FROM fieldtype_options WHERE fields_id=:fields_id AND option_id IN(2)
SELECT * FROM fieldtype_options WHERE fields_id=:fields_id AND option_id IN(3)
SELECT * FROM fieldtype_options WHERE fields_id=:fields_id AND option_id IN(5)
SELECT * FROM fieldtype_options WHERE fields_id=:fields_id AND option_id IN(5)
SELECT * FROM fieldtype_options WHERE fields_id=:fields_id AND option_id IN(5)
SELECT * FROM fieldtype_options WHERE fields_id=:fields_id AND option_id IN(4)
SELECT * FROM fieldtype_options WHERE fields_id=:fields_id AND option_id IN(3)
...and thousands more...
:fields_id never changes, is the id of the Options field. The queries are issued by ProcessWire\SelectableOptionManager::getOptions() .
Steps to reproduce:
Start with the stock 'site-default' site profile from the devns branch. 2.x might also work, I haven't tried.
Install TracyDebugger.
Set $config->debug to true.
Add an Options-type field (e.g. Select) with a handful of selectable options to the 'basic-page' template.
Create a few new pages using the 'basic-page' template.
View one of these pages in the frontend, open Tracey's 'PW Debug mode' panel and expand the 'PDO Queries ($database)' section.
Find one query per 'basic-page' page stating something like so: SELECT * FROM fieldtype_options WHERE fields_id=:fields_id AND option_id IN(13)
I'd like to gather feedback from experienced developers with deeper understanding of the system:
Why does PW need the current option value of every other sibling page to render a single page?
Wouldn't it be possible to cache these redundant queries if they are needed in the first place?
How does this scale when the site grows?
How can I achieve sorting by an options field without causing such excessive query counts?
Please advise.