Performance and monitoring
Diagnose slow LEFT JOIN queries in WordPress
On large installations, a WordPress query that filters by categories or taxonomies can take a long time when it combines posts and term_relationships, sorts and limits results. The presence of LEFT JOIN does not prove on its own that the query is wrong: data, filters, indexes and the execution plan all matter.
Background to the historical case
This guide came out of incidents with hundreds of thousands of posts and refers to WordPress ticket 54346, which studies category queries and alternatives using subqueries.
The results observed on certain installations do not guarantee millisecond times for any data volume. Nor do they turn a patch from the ticket into an official fix that applies to every version.
Identify the real query
- Reproduce a slow URL and record whether there was a session, cache and filters.
- Use PHP X-Ray to find the complete query and the component that generates it.
- Note the versions of WordPress, MySQL or MariaDB and the plugins involved.
- On a copy with representative data, review the plan with
EXPLAINand the existing indexes.
Compare how many rows are examined, where filtering happens and how results are sorted. A different plan after an update deserves analysis, but it does not automatically identify a single culprit.
Assess an alternative
The developer can try a subquery, an equivalent expression, an index or a rework of how WP_Query is used. Check that it keeps the same posts, order, exclusions, duplicates and pagination, especially with combinations of taxonomies.
Do not replace every JOIN or remove GROUP BY globally. A change that speeds up one category can return wrong results in another query.
Apply and maintain
Avoid editing the core files directly: updates overwrite the changes and an old patch may stop fitting. If a temporary adaptation is essential, the developer must maintain it with version control, tests and a way to remove it.
After the change, compare several runs and check categories, searches, pagination and the admin area. Keep the previous plan and revert if other operations get worse. For queries that also calculate totals, see the SQL_CALC_FOUND_ROWS guide.
You may also find useful
- Improve WordPress database performance with Index WP MySQL For Speed Assess WordPress indexes with Index WP MySQL For Speed, review the changes before applying them and...
- Optimise the wp_options table and "autoload" data Measure and reduce autoloaded data in wp_options, find large options, cron, transients and sessions,...
- SQL queries to clean orphaned data and purge the WordPress database Clean orphaned data, revisions and transients in WordPress with verified queries, a recoverable back...
- Slow "SELECT SQL_CALC_FOUND_ROWS" SQL queries Diagnose slow WordPress queries with SQL_CALC_FOUND_ROWS, find where they come from and validate any...
Something wrong or out of date? Tell us and we will review it.