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

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 3 times, most recently from 8ab71fb to a7243ad Compare January 19, 2025 22:10
Comment on lines 409 to 608
'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

github-actions bot commented Jan 22, 2025

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, spacedmonkey, joemcgill, thekt12.

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

@peterwilsoncc peterwilsoncc force-pushed the fix/59516-improve-wp-query-cache-hits branch from 133929b to 009ecd5 Compare January 28, 2025 04:03
spacedmonkey
spacedmonkey previously approved these changes Jan 28, 2025
Copy link
Member

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

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

LGTM. Reviewed all parameters and this PR look solid to me.

@peterwilsoncc peterwilsoncc force-pushed the fix/59516-improve-wp-query-cache-hits branch from 908dbe8 to 7135775 Compare January 28, 2025 21:47
@peterwilsoncc
Copy link
Contributor Author

@spacedmonkey I've pushed a few additional tests and bugs they highlighted since your last review so I'll dismiss it as stale. Because WP_Query is such a fundamental and low level feature, I don't want to yolo even the smallest of changes.

@peterwilsoncc peterwilsoncc dismissed spacedmonkey’s stale review January 28, 2025 23:08

State: code changes pushed since review.

Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

Thanks for the additional test coverage here. I left one question but am pre-approving as everything looks good to me.

src/wp-includes/class-wp-query.php Show resolved Hide resolved
@peterwilsoncc peterwilsoncc force-pushed the fix/59516-improve-wp-query-cache-hits branch from d7a7f6d to 62db25a Compare February 2, 2025 22:26
Copy link
Member

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

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

We can't sort post__in, post_name__in or post_parent__in. These can be orderby these values. This would mean the sort of the orderby would be wrong.

@peterwilsoncc
Copy link
Contributor Author

We can't sort post__in, post_name__in or post_parent__in. These can be orderby these values. This would mean the sort of the orderby would be wrong.

These are only sorted for the WHERE clause and cache key generation, they are untouched for the orderby clause. If used for ordering the SQL query will differ, generating a different hash for the cache key.

See

// Duplicate array before sorting to allow for the orderby clause.
$post__in_for_where = $q['post__in'];
$post__in_for_where = array_unique( array_map( 'absint', $post__in_for_where ) );
sort( $post__in_for_where );
$post__in = implode( ',', array_map( 'absint', $post__in_for_where ) );
$where .= " AND {$wpdb->posts}.ID IN ($post__in)";

$q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] );
// Duplicate array before sorting to allow for the orderby clause.
$post_name__in_for_where = array_unique( $q['post_name__in'] );
sort( $post_name__in_for_where );
$post_name__in = "'" . implode( "','", $post_name__in_for_where ) . "'";
$where .= " AND {$wpdb->posts}.post_name IN ($post_name__in)";

// Duplicate array before sorting to allow for the orderby clause.
$post_parent__in_for_where = $q['post_parent__in'];
$post_parent__in_for_where = array_unique( array_map( 'absint', $post_parent__in_for_where ) );
sort( $post_parent__in_for_where );
$post_parent__in = implode( ',', array_map( 'absint', $post_parent__in_for_where ) );
$where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";

The test Test_Query_CacheResults::test_orderby_clauses_are_not_normalized() has been introduced in this pull request to ensure the cache keys differ.

@peterwilsoncc peterwilsoncc force-pushed the fix/59516-improve-wp-query-cache-hits branch from acfd9a4 to 08e9d63 Compare February 4, 2025 22:30
@peterwilsoncc peterwilsoncc force-pushed the fix/59516-improve-wp-query-cache-hits branch from 08e9d63 to fbd5c6b Compare February 4, 2025 22:35
@peterwilsoncc
Copy link
Contributor Author

@spacedmonkey I've pushed a change to the orderby tests to include tests that the order of posts returned is as expected. As the post IDs change for each test run, I needed to split the tests in to three rather than use the data provider.

Copy link
Member

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

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

Thanks for the clarification @peterwilsoncc

This pr looks great

@peterwilsoncc peterwilsoncc added the props-bot Adding this label triggers the Props Bot workflow for a PR. label Feb 6, 2025
@github-actions github-actions bot removed the props-bot Adding this label triggers the Props Bot workflow for a PR. label Feb 6, 2025
Copy link

github-actions bot commented Feb 6, 2025

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 59766
GitHub commit: c22e267

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions bot closed this Feb 6, 2025
@peterwilsoncc peterwilsoncc deleted the fix/59516-improve-wp-query-cache-hits branch February 6, 2025 21:11
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.

4 participants