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.
Built from https://develop.svn.wordpress.org/trunk@56513


git-svn-id: http://core.svn.wordpress.org/trunk@56025 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2023-09-05 12:23:21 +00:00
parent 5e21330e7d
commit ed6e84c280
2 changed files with 9 additions and 2 deletions

View File

@ -3271,7 +3271,14 @@ class WP_Query {
}
if ( null === $this->posts ) {
$split_the_query = ( $old_request == $this->request && "{$wpdb->posts}.*" === $fields && ! empty( $limits ) && $q['posts_per_page'] < 500 );
$split_the_query = (
$old_request == $this->request
&& "{$wpdb->posts}.*" === $fields
&& (
wp_using_ext_object_cache()
|| ( ! empty( $limits ) && $q['posts_per_page'] < 500 )
)
);
/**
* Filters whether to split the query.

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.4-alpha-56512';
$wp_version = '6.4-alpha-56513';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.