Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP_Query: sort more arrays to improve cache hits. #7497

Open
wants to merge 24 commits into
base: trunk
Choose a base branch
from

Conversation

peterwilsoncc
Copy link
Contributor

@peterwilsoncc peterwilsoncc commented Oct 4, 2024

Lighter touch alternative to #5347

What

Improves the cache hits for WP_Query

How

It sorts query variable whenever possible. This results in the SQL queries being normalised so that it doesn't produce different SQL for the same effective arguments. For example category__in => [1,2] and category__in => [2,1] produce the same IN() clause.

Why

Currently the SQL queries and the generated cache key differ for the same effective arguments.

Trac ticket: https://core.trac.wordpress.org/ticket/59516


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.

Copy link

github-actions bot commented Oct 4, 2024

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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • 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.

@kt-12
Copy link
Member

kt-12 commented Dec 10, 2024

@peterwilsoncc I was concerned that sorting might effect the order of the output too, looks like it doesn't. Can we also ensure, https://core.trac.wordpress.org/ticket/59492#comment:3 is handled (if not), please. If there is only one element in the array it should be converted into string. I remember getting that scenario from a live site, but I can't recall the details.

@peterwilsoncc
Copy link
Contributor Author

I was concerned that sorting might effect the order of the output too, looks like it doesn't.

The order should only have an effect for items that can be used for the orderby clause, for example post__in. It's fine to sort for the where clause so the key differs only in the instances where the item is used for ordering.

Can we also ensure, https://core.trac.wordpress.org/ticket/59492#comment:3 is handled (if not), please. If there is only one element in the array it should be converted into string. I remember getting that scenario from a live site, but I can't recall the details.

My recollection is that in some instances the use of a string vs an array can result in a different where clause beyond just the use of = vs IN. At the very least, I know it is complicated so I'm trying to get something lighter touch in initially so it's possible to spend additional time comparing the effect of passing a string or an array.

@peterwilsoncc peterwilsoncc force-pushed the fix/59516-improve-wp-query-cache-hits branch 2 times, most recently from 881c8fa to c8afd28 Compare January 17, 2025 03:13
@peterwilsoncc peterwilsoncc force-pushed the fix/59516-improve-wp-query-cache-hits branch from 8ab71fb to a7243ad Compare January 19, 2025 22:10
Comment on lines 409 to 416
'term queries order (array)' => array(
'query_vars_1' => array( 'cat' => array( '1', '2' ) ),
'query_vars_2' => array( 'cat' => array( '2', '1' ) ),
),
'term queries order (string)' => array(
'query_vars_1' => array( 'cat' => '1,2' ),
'query_vars_2' => array( 'cat' => '2,1' ),
),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two are failing. cat is normalised in the generated SQL query but not in the arguments passed to ::generate_cache_key().

I can't figure out why as the query vars are passed to ::parse_term_query() by reference so sorting it there should work. @joemcgill Can you see what I am missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two are failing. cat is normalised in the generated SQL query but not in the arguments passed to ::generate_cache_key().

I can't figure out why as the query vars are passed to ::parse_term_query() by reference so sorting it there should work. @joemcgill Can you see what I am missing?

nvm, the tests were testing the wrong thing so I've started testing the cache key generated by WP_Query in bc24627

@peterwilsoncc peterwilsoncc marked this pull request as ready for review January 22, 2025 01:49
Copy link

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 peterwilsoncc, thekt12.

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.

2 participants