Make WordPress Core

Changeset 56513


Ignore:
Timestamp:
09/05/2023 12:21:27 PM (13 months ago)
Author:
spacedmonkey
Message:

Query: Use split queries in WP_Query if persistent object caching is enabled.

Prior to this commit, the WP_Query class split the query for posts into two database queries. First, it initiated an ID-based query to retrieve post IDs, followed by a call to _prime_post_caches to fetch post objects if they weren't already in memory. This splitting of queries was limited to cases where fewer than 500 posts were being requested, to prevent a potentially large database query within the IN statement in _prime_post_caches.

However, this limitation becomes unnecessary when a persistent object cache is enabled, as the post objects can be efficiently retrieved from the fast object cache. This commit transfers the responsibility of fetching posts to the object cache, which not only speeds up the process but also reduces the strain on the database server.

Props peterwilsoncc, spacedmonkey, SergeyBiryukov.
Fixes #57296.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r56434 r56513  
    32723272
    32733273        if ( null === $this->posts ) {
    3274             $split_the_query = ( $old_request == $this->request && "{$wpdb->posts}.*" === $fields && ! empty( $limits ) && $q['posts_per_page'] < 500 );
     3274            $split_the_query = (
     3275                $old_request == $this->request
     3276                && "{$wpdb->posts}.*" === $fields
     3277                && (
     3278                    wp_using_ext_object_cache()
     3279                    || ( ! empty( $limits ) && $q['posts_per_page'] < 500 )
     3280                )
     3281            );
    32753282
    32763283            /**
  • trunk/tests/phpunit/tests/post/nav-menu.php

    r55979 r56513  
    343343
    344344        $start_num_queries = get_num_queries();
    345         wp_get_nav_menu_items( $this->menu_id );
     345        wp_get_nav_menu_items( $this->menu_id, array( 'nopaging' => false ) );
    346346        $queries_made = get_num_queries() - $start_num_queries;
    347         $this->assertSame( 6, $queries_made, 'Only does 6 database queries when running wp_get_nav_menu_items.' );
     347        $this->assertSame( 7, $queries_made, 'Only does 7 database queries when running wp_get_nav_menu_items.' );
    348348
    349349        $args = $action->get_args();
    350350        $this->assertSameSets( $menu_nav_ids, $args[0][1], '_prime_post_caches() was not executed.' );
    351         $this->assertSameSets( $post_ids, $args[1][1], '_prime_post_caches() was not executed.' );
     351        $this->assertSameSets( $post_ids, $args[2][1], '_prime_post_caches() was not executed.' );
    352352    }
    353353
     
    384384
    385385        $start_num_queries = get_num_queries();
    386         wp_get_nav_menu_items( $this->menu_id );
     386        wp_get_nav_menu_items( $this->menu_id, array( 'nopaging' => false ) );
    387387        get_term_meta( $term_ids[0] );
    388388        $queries_made = get_num_queries() - $start_num_queries;
    389         $this->assertSame( 6, $queries_made, 'Only does 6 database queries when running wp_get_nav_menu_items.' );
     389        $this->assertSame( 7, $queries_made, 'Only does 7 database queries when running wp_get_nav_menu_items.' );
    390390
    391391        $args       = $action_terms->get_args();
  • trunk/tests/phpunit/tests/post/query.php

    r55745 r56513  
    776776        $this->assertIsInt( $q->found_posts );
    777777    }
     778
     779    /**
     780     * @ticket 57296
     781     * @covers WP_Query::get_posts
     782     */
     783    public function test_split_the_query_object_cache() {
     784        $filter = new MockAction();
     785        add_filter( 'split_the_query', array( $filter, 'filter' ) );
     786
     787        $q = new WP_Query(
     788            array(
     789                'posts_per_page' => 501,
     790            )
     791        );
     792
     793        $this->assertSame( (bool) wp_using_ext_object_cache(), $filter->get_args()[0][0] );
     794    }
    778795}
Note: See TracChangeset for help on using the changeset viewer.