Taxonomy: Improve AND term query performance - #12535
Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
This PR improves the performance of taxonomy queries using the
ANDoperator, includingcategory__and.Small term intersections now use one indexed
wp_term_relationshipsjoin per term instead of executing a correlated subquery for every candidate object. The optimization is limited to three total taxonomy joins. Larger intersections retain the existing correlated subquery because benchmarks showed that additional joins can regress queries where all terms are common.Trac ticket: https://core.trac.wordpress.org/ticket/16706
Benchmark results
Benchmarks used 50,000 synthetic published posts on MySQL 8.4.10 and MariaDB 10.11.18. Values are medians from five warmed runs after analyzing the tables. Both query forms returned identical post IDs.
Four joins were approximately neutral for common terms, while five or more regressed. This is why the automatic optimization stops at three total taxonomy joins.
Absolute timings are environment-specific, but the trend was consistent across both database engines.
Use of AI Tools
AI assistance: Yes
Tool(s): Pi coding agent
Model(s): GPT-5.6
Used for: Ticket validation, initial implementation, benchmark design, code review, and drafting this PR description. The final changes and test results were reviewed by me.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.