Make WordPress Core


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.

File:
1 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            /**
Note: See TracChangeset for help on using the changeset viewer.