Make WordPress Core


Ignore:
Timestamp:
10/03/2023 02:59:22 PM (17 months ago)
Author:
spacedmonkey
Message:

Query: Improve caching behavior for WP_Query when retrieving id=>parent fields

In [53941], the addition of query caching to WP_Query brought about an unintended issue when querying for fields equal to id=>parent. Specifically, on websites with object caching enabled and a substantial number of pages, the second run of this query triggered the _prime_post_caches function for id=>parent. This led to the unnecessary priming of post, meta, and term caches, even when only id and parent information were requested.

This commit addresses this issue by introducing a new function, _prime_post_parents_caches, which primes a dedicated cache for post parents. This cache is primed during the initial query execution. Subsequently, the wp_cache_get_multiple function is employed to retrieve all post parent data in a single object cache request, optimizing performance.

Additionally, this commit extends the coverage of existing unit tests to ensure the reliability of the changes.

Props kevinfodness, joemcgill, peterwilsoncc, LinSoftware, thekt12, spacedmonkey.
Fixes #59188

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/query/cacheResults.php

    r56656 r56763  
    711711        $queries_after = get_num_queries();
    712712
    713         $this->assertSame( $queries_before, $queries_after );
     713        $this->assertSame( 1, $queries_after - $queries_before );
    714714        $this->assertCount( 5, $query1->posts );
    715715        $this->assertCount( 5, $query2->posts );
     
    725725         */
    726726        $this->assertNotEquals( $query1->posts, $query2->posts );
     727    }
     728
     729
     730    /**
     731     * @ticket 59188
     732     */
     733    public function test_query_cache_unprimed_parents() {
     734        $args   = array(
     735            'cache_results' => true,
     736            'fields'        => 'id=>parent',
     737        );
     738        $query1 = new WP_Query();
     739        $query1->query( $args );
     740
     741        $post_ids = wp_list_pluck( $query1->posts, 'ID' );
     742        wp_cache_delete_multiple( $post_ids, 'post_parent' );
     743
     744        $queries_before = get_num_queries();
     745        $query2         = new WP_Query();
     746        $query2->query( $args );
     747        $queries_after = get_num_queries();
     748
     749        $this->assertSame( 1, $queries_after - $queries_before, 'There should be only one query to prime parents' );
     750        $this->assertCount( 5, $query1->posts, 'There should be only 5 posts returned on first query' );
     751        $this->assertCount( 5, $query2->posts, 'There should be only 5 posts returned on second query' );
     752        $this->assertSame( $query1->found_posts, $query2->found_posts, 'Found posts should match on second query' );
     753    }
     754
     755    /**
     756     * @ticket 59188
     757     */
     758    public function test_query_cache_update_parent() {
     759        $page_id = self::factory()->post->create(
     760            array(
     761                'post_type'   => 'page',
     762                'post_parent' => self::$pages[0],
     763            )
     764        );
     765        $args    = array(
     766            'cache_results' => true,
     767            'post_type'     => 'page',
     768            'fields'        => 'id=>parent',
     769            'post__in'      => array(
     770                $page_id,
     771            ),
     772        );
     773        $query1  = new WP_Query();
     774        $query1->query( $args );
     775
     776        wp_update_post(
     777            array(
     778                'ID'          => $page_id,
     779                'post_parent' => self::$pages[1],
     780            )
     781        );
     782
     783        $queries_before = get_num_queries();
     784        $query2         = new WP_Query();
     785        $query2->query( $args );
     786        $queries_after = get_num_queries();
     787
     788        $this->assertSame( self::$pages[0], $query1->posts[0]->post_parent, 'Check post parent on first query' );
     789        $this->assertSame( self::$pages[1], $query2->posts[0]->post_parent, 'Check post parent on second query' );
     790        $this->assertSame( 2, $queries_after - $queries_before, 'There should be 2 queries, one for id=>parent' );
     791        $this->assertSame( $query1->found_posts, $query2->found_posts, 'Found posts should match on second query' );
     792    }
     793
     794    /**
     795     * @ticket 59188
     796     */
     797    public function test_query_cache_delete_parent() {
     798        $parent_page_id = self::factory()->post->create(
     799            array(
     800                'post_type' => 'page',
     801            )
     802        );
     803        $page_id        = self::factory()->post->create(
     804            array(
     805                'post_type'   => 'page',
     806                'post_parent' => $parent_page_id,
     807            )
     808        );
     809        $args           = array(
     810            'cache_results' => true,
     811            'post_type'     => 'page',
     812            'fields'        => 'id=>parent',
     813            'post__in'      => array(
     814                $page_id,
     815            ),
     816        );
     817        $query1         = new WP_Query();
     818        $query1->query( $args );
     819
     820        wp_delete_post( $parent_page_id, true );
     821
     822        $queries_before = get_num_queries();
     823        $query2         = new WP_Query();
     824        $query2->query( $args );
     825        $queries_after = get_num_queries();
     826
     827        $this->assertSame( $parent_page_id, $query1->posts[0]->post_parent, 'Check post parent on first query' );
     828        $this->assertSame( 0, $query2->posts[0]->post_parent, 'Check post parent on second query' );
     829        $this->assertSame( 2, $queries_after - $queries_before, 'There should be 2 queries, one for id=>parent' );
     830        $this->assertSame( $query1->found_posts, $query2->found_posts, 'Found posts should match on second query' );
    727831    }
    728832
Note: See TracChangeset for help on using the changeset viewer.