Make WordPress Core


Ignore:
Timestamp:
09/05/2023 12:21:27 PM (11 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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();
Note: See TracChangeset for help on using the changeset viewer.