Make WordPress Core


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