Make WordPress Core

Changeset 56763


Ignore:
Timestamp:
10/03/2023 02:59:22 PM (12 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

Location:
trunk
Files:
1 added
4 edited

Legend:

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

    r56656 r56763  
    31803180
    31813181                if ( $cached_results ) {
    3182                     if ( 'ids' === $q['fields'] ) {
    3183                         /** @var int[] */
    3184                         $this->posts = array_map( 'intval', $cached_results['posts'] );
    3185                     } else {
    3186                         _prime_post_caches( $cached_results['posts'], $q['update_post_term_cache'], $q['update_post_meta_cache'] );
    3187                         /** @var WP_Post[] */
    3188                         $this->posts = array_map( 'get_post', $cached_results['posts'] );
    3189                     }
    3190 
    3191                     $this->post_count    = count( $this->posts );
     3182                    /** @var int[] */
     3183                    $post_ids = array_map( 'intval', $cached_results['posts'] );
     3184
     3185                    $this->post_count    = count( $post_ids );
    31923186                    $this->found_posts   = $cached_results['found_posts'];
    31933187                    $this->max_num_pages = $cached_results['max_num_pages'];
    31943188
    31953189                    if ( 'ids' === $q['fields'] ) {
     3190                        $this->posts = $post_ids;
     3191
    31963192                        return $this->posts;
    31973193                    } elseif ( 'id=>parent' === $q['fields'] ) {
     3194                        _prime_post_parents_caches( $post_ids );
     3195
    31983196                        /** @var int[] */
    3199                         $post_parents = array();
    3200 
    3201                         foreach ( $this->posts as $key => $post ) {
     3197                        $post_parents = wp_cache_get_multiple( $post_ids, 'post_parent' );
     3198
     3199                        foreach ( $post_parents as $id => $post_parent ) {
    32023200                            $obj              = new stdClass();
    3203                             $obj->ID          = (int) $post->ID;
    3204                             $obj->post_parent = (int) $post->post_parent;
    3205 
    3206                             $this->posts[ $key ] = $obj;
    3207 
    3208                             $post_parents[ $obj->ID ] = $obj->post_parent;
     3201                            $obj->ID          = (int) $id;
     3202                            $obj->post_parent = (int) $post_parent;
     3203
     3204                            $this->posts[] = $obj;
    32093205                        }
    32103206
    32113207                        return $post_parents;
     3208                    } else {
     3209                        _prime_post_caches( $post_ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
     3210                        /** @var WP_Post[] */
     3211                        $this->posts = array_map( 'get_post', $post_ids );
    32123212                    }
    32133213                }
     
    32573257                $post_ids[]                      = (int) $post->ID;
    32583258            }
     3259            // Prime post parent caches, so that on second run, there is not another database query.
     3260            wp_cache_add_multiple( $post_parents, 'post_parent' );
    32593261
    32603262            if ( $q['cache_results'] && $id_query_is_cacheable ) {
  • trunk/src/wp-includes/functions.php

    r56753 r56763  
    71707170
    71717171    foreach ( $cache_values as $id => $value ) {
    7172         if ( ! $value ) {
     7172        if ( false === $value ) {
    71737173            $non_cached_ids[] = (int) $id;
    71747174        }
  • trunk/src/wp-includes/post.php

    r56711 r56763  
    72637263    wp_cache_delete( $post->ID, 'posts' );
    72647264    wp_cache_delete( $post->ID, 'post_meta' );
     7265    wp_cache_delete( $post->ID, 'post_parent' );
    72657266
    72667267    clean_object_term_cache( $post->ID, $post->post_type );
     
    77977798
    77987799/**
     7800 * Prime post parent caches.
     7801 *
     7802 * @global wpdb $wpdb WordPress database abstraction object.
     7803 *
     7804 * @param int[] $ids ID list.
     7805 */
     7806function _prime_post_parents_caches( array $ids ) {
     7807    global $wpdb;
     7808
     7809    $non_cached_ids = _get_non_cached_ids( $ids, 'post_parent' );
     7810    if ( ! empty( $non_cached_ids ) ) {
     7811        $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.ID, $wpdb->posts.post_parent FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) );
     7812
     7813        if ( $fresh_posts ) {
     7814            $post_parent_data = array();
     7815            foreach ( $fresh_posts as $fresh_post ) {
     7816                $post_parent_data[ (int) $fresh_post->ID ] = (int) $fresh_post->post_parent;
     7817            }
     7818
     7819            wp_cache_add_multiple( $post_parent_data, 'post_parent' );
     7820        }
     7821    }
     7822}
     7823
     7824/**
    77997825 * Adds a suffix if any trashed posts have a given slug.
    78007826 *
  • 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.