Skip to content

Taxonomy: Improve AND term query performance - #12535

Open
Sukhendu2002 wants to merge 3 commits into
WordPress:trunkfrom
Sukhendu2002:perf/16706-tax-query-and-join
Open

Taxonomy: Improve AND term query performance#12535
Sukhendu2002 wants to merge 3 commits into
WordPress:trunkfrom
Sukhendu2002:perf/16706-tax-query-and-join

Conversation

@Sukhendu2002

@Sukhendu2002 Sukhendu2002 commented Jul 15, 2026

Copy link
Copy Markdown

This PR improves the performance of taxonomy queries using the AND operator, including category__and.

Small term intersections now use one indexed wp_term_relationships join 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.

Database Scenario Terms JOIN Subquery Improvement
MySQL 8.4 Selective 3 0.725 ms 140.919 ms 194.36×
MySQL 8.4 Common 3 108.164 ms 142.594 ms 1.32×
MySQL 8.4 No match 3 49.135 ms 139.557 ms 2.84×
MariaDB 10.11 Selective 3 0.562 ms 127.070 ms 226.03×
MariaDB 10.11 Common 3 108.215 ms 130.360 ms 1.20×
MariaDB 10.11 No match 3 50.801 ms 127.312 ms 2.51×

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.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@Sukhendu2002
Sukhendu2002 marked this pull request as ready for review July 15, 2026 09:44
@github-actions

Copy link
Copy Markdown

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props sukhendu2002.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

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.

1 participant