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.

git-svn-id: https://develop.svn.wordpress.org/trunk@56513 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris
2023-09-05 12:21:27 +00:00
parent 6be588c72f
commit 92994bd336
3 changed files with 30 additions and 6 deletions
+8 -1
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.