Skip to content

Commit 5347c69

Browse files
committed
REST API: Improve performance of X-WP-Total/X-WP-TotalPages queries.
Improve the performance of fallback queries to determine the total number of objects available on various REST endpoints for out of bounds queries. The database queries are modified to return the minimum amount of data required for determining the count and bypass priming of meta a term caches where appropriate. Props adamsilverstein, joehoyle, johnbillion, jorbin, kadamwhite, spacedmonkey, sukhendu2002, westonruter. Fixes #62801. git-svn-id: https://develop.svn.wordpress.org/trunk@61002 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 36177da commit 5347c69

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,13 @@ public function get_items( $request ) {
323323
$max_pages = (int) $query->max_num_pages;
324324

325325
if ( $total_comments < 1 ) {
326-
// Out-of-bounds, run the query again without LIMIT for total count.
326+
// Out-of-bounds, run the query without pagination/offset to get the total count.
327327
unset( $prepared_args['number'], $prepared_args['offset'] );
328328

329-
$query = new WP_Comment_Query();
330-
$prepared_args['count'] = true;
331-
$prepared_args['orderby'] = 'none';
329+
$query = new WP_Comment_Query();
330+
$prepared_args['count'] = true;
331+
$prepared_args['orderby'] = 'none';
332+
$prepared_args['update_comment_meta_cache'] = false;
332333

333334
$total_comments = $query->query( $prepared_args );
334335
$max_pages = (int) ceil( $total_comments / $request['per_page'] );

src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,15 @@ public function get_items( $request ) {
203203
$total_revisions = $revisions_query->found_posts;
204204

205205
if ( $total_revisions < 1 ) {
206-
// Out-of-bounds, run the query again without LIMIT for total count.
206+
// Out-of-bounds, run the query without pagination/offset to get the total count.
207207
unset( $query_args['paged'], $query_args['offset'] );
208-
$count_query = new WP_Query();
208+
209+
$count_query = new WP_Query();
210+
$query_args['fields'] = 'ids';
211+
$query_args['posts_per_page'] = 1;
212+
$query_args['update_post_meta_cache'] = false;
213+
$query_args['update_post_term_cache'] = false;
214+
209215
$count_query->query( $query_args );
210216

211217
$total_revisions = $count_query->found_posts;

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,10 +487,15 @@ static function ( $format ) {
487487
$total_posts = $posts_query->found_posts;
488488

489489
if ( $total_posts < 1 && $page > 1 ) {
490-
// Out-of-bounds, run the query again without LIMIT for total count.
490+
// Out-of-bounds, run the query without pagination/offset to get the total count.
491491
unset( $query_args['paged'] );
492492

493-
$count_query = new WP_Query();
493+
$count_query = new WP_Query();
494+
$query_args['fields'] = 'ids';
495+
$query_args['posts_per_page'] = 1;
496+
$query_args['update_post_meta_cache'] = false;
497+
$query_args['update_post_term_cache'] = false;
498+
494499
$count_query->query( $query_args );
495500
$total_posts = $count_query->found_posts;
496501
}

src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,16 @@ public function get_items( $request ) {
308308
$total_revisions = $revisions_query->found_posts;
309309

310310
if ( $total_revisions < 1 ) {
311-
// Out-of-bounds, run the query again without LIMIT for total count.
311+
// Out-of-bounds, run the query without pagination/offset to get the total count.
312312
unset( $query_args['paged'], $query_args['offset'] );
313313

314-
$count_query = new WP_Query();
315-
$count_query->query( $query_args );
314+
$count_query = new WP_Query();
315+
$query_args['fields'] = 'ids';
316+
$query_args['posts_per_page'] = 1;
317+
$query_args['update_post_meta_cache'] = false;
318+
$query_args['update_post_term_cache'] = false;
316319

320+
$count_query->query( $query_args );
317321
$total_revisions = $count_query->found_posts;
318322
}
319323

src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,13 @@ static function ( $column ) use ( $search_columns_mapping ) {
399399
$total_users = $query->get_total();
400400

401401
if ( $total_users < 1 ) {
402-
// Out-of-bounds, run the query again without LIMIT for total count.
402+
// Out-of-bounds, run the query without pagination/offset to get the total count.
403403
unset( $prepared_args['number'], $prepared_args['offset'] );
404-
$count_query = new WP_User_Query( $prepared_args );
405-
$total_users = $count_query->get_total();
404+
405+
$prepared_args['number'] = 1;
406+
$prepared_args['fields'] = 'ID';
407+
$count_query = new WP_User_Query( $prepared_args );
408+
$total_users = $count_query->get_total();
406409
}
407410

408411
$response->header( 'X-WP-Total', (int) $total_users );

0 commit comments

Comments
 (0)