From 4baf0a1eda8df99c8f6c9c57ee4d796962f5626e Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Fri, 29 Sep 2023 17:11:21 +0000 Subject: [PATCH] Comments: Improve WP_Comment_Query count query performance by setting 'order by' to 'none'. In cases where `WP_Comment_Query` or `get_comments` is employed with the 'count' parameter set to true, specify 'order by' as 'none'. Since these queries serve solely to determine the count of comments matching specific query parameters, the 'order by' clause becomes redundant and places unnecessary strain on the database server, resulting in slower query execution. Given that count queries are executed on every admin request to retrieve comment counts, this change enhances the performance of the wp-admin interface. Props guss77, davidbaumwald, SergeyBiryukov, westonruter, peterwilsoncc, foliovision, hareesh-pillai, spacedmonkey. Fixes #58368 git-svn-id: https://develop.svn.wordpress.org/trunk@56747 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-wp-comments-list-table.php | 13 +++++++----- src/wp-admin/includes/meta-boxes.php | 2 +- src/wp-includes/comment.php | 2 ++ .../class-wp-rest-comments-controller.php | 9 +++++---- tests/phpunit/tests/comment/query.php | 20 ++++++++++--------- 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/wp-admin/includes/class-wp-comments-list-table.php b/src/wp-admin/includes/class-wp-comments-list-table.php index c4f288d8d0..b9eb0c2052 100644 --- a/src/wp-admin/includes/class-wp-comments-list-table.php +++ b/src/wp-admin/includes/class-wp-comments-list-table.php @@ -177,9 +177,10 @@ class WP_Comments_List_Table extends WP_List_Table { array_merge( $args, array( - 'count' => true, - 'offset' => 0, - 'number' => 0, + 'count' => true, + 'offset' => 0, + 'number' => 0, + 'orderby' => 'none', ) ) ); @@ -298,6 +299,7 @@ class WP_Comments_List_Table extends WP_List_Table { 'post_id' => $post_id ? $post_id : 0, 'user_id' => $current_user_id, 'count' => true, + 'orderby' => 'none', ) ); $link = add_query_arg( 'user_id', $current_user_id, $link ); @@ -518,8 +520,9 @@ class WP_Comments_List_Table extends WP_List_Table { foreach ( $comment_types as $type => $label ) { if ( get_comments( array( - 'number' => 1, - 'type' => $type, + 'count' => true, + 'orderby' => 'none', + 'type' => $type, ) ) ) { printf( diff --git a/src/wp-admin/includes/meta-boxes.php b/src/wp-admin/includes/meta-boxes.php index c63474b29a..5228076079 100644 --- a/src/wp-admin/includes/meta-boxes.php +++ b/src/wp-admin/includes/meta-boxes.php @@ -901,8 +901,8 @@ function post_comment_meta_box( $post ) { $total = get_comments( array( 'post_id' => $post->ID, - 'number' => 1, 'count' => true, + 'orderby' => 'none', ) ); $wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table' ); diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index ce0ebe769d..de23a72130 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -396,6 +396,7 @@ function get_comment_count( $post_id = 0 ) { $args = array( 'count' => true, 'update_comment_meta_cache' => false, + 'orderby' => 'none', ); if ( $post_id > 0 ) { $args['post_id'] = $post_id; @@ -1114,6 +1115,7 @@ function get_page_of_comment( $comment_id, $args = array() ) { 'fields' => 'ids', 'count' => true, 'status' => 'approve', + 'orderby' => 'none', 'parent' => 0, 'date_query' => array( array( diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 16018bb58a..1169b6b130 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -295,8 +295,9 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { // Out-of-bounds, run the query again without LIMIT for total count. unset( $prepared_args['number'], $prepared_args['offset'] ); - $query = new WP_Comment_Query(); - $prepared_args['count'] = true; + $query = new WP_Comment_Query(); + $prepared_args['count'] = true; + $prepared_args['orderby'] = 'none'; $total_comments = $query->query( $prepared_args ); $max_pages = ceil( $total_comments / $request['per_page'] ); @@ -1188,8 +1189,8 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { // Only grab one comment to verify the comment has children. $comment_children = $comment->get_children( array( - 'number' => 1, - 'count' => true, + 'count' => true, + 'orderby' => 'none', ) ); diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 50cd910796..6a367e43b4 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -3091,7 +3091,8 @@ class Tests_Comment_Query extends WP_UnitTestCase { $q = new WP_Comment_Query(); $found = $q->query( array( - 'count' => true, + 'count' => true, + 'orderby' => 'none', ) ); @@ -3129,6 +3130,7 @@ class Tests_Comment_Query extends WP_UnitTestCase { $found = $q->query( array( 'count' => true, + 'orderby' => 'none', 'meta_query' => array( array( 'key' => 'foo', @@ -5001,20 +5003,20 @@ class Tests_Comment_Query extends WP_UnitTestCase { $query_1 = $q->query( array( - 'fields' => 'ids', - 'number' => 3, - 'order' => 'ASC', - 'count' => true, + 'fields' => 'ids', + 'number' => 3, + 'orderby' => 'none', + 'count' => true, ) ); $number_of_queries = get_num_queries(); $query_2 = $q->query( array( - 'fields' => 'ids', - 'number' => 3, - 'order' => 'ASC', - 'count' => true, + 'fields' => 'ids', + 'number' => 3, + 'orderby' => 'none', + 'count' => true, ) ); $this->assertSame( $number_of_queries, get_num_queries() );