From 8aca3fdc688814631af0e19b6b5e6c7c2c08e8cb Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Fri, 29 Sep 2023 17:13:24 +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 Built from https://develop.svn.wordpress.org/trunk@56747 git-svn-id: http://core.svn.wordpress.org/trunk@56259 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/class-wp-comments-list-table.php | 13 ++++++++----- wp-admin/includes/meta-boxes.php | 2 +- wp-includes/comment.php | 2 ++ .../endpoints/class-wp-rest-comments-controller.php | 9 +++++---- wp-includes/version.php | 2 +- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/wp-admin/includes/class-wp-comments-list-table.php b/wp-admin/includes/class-wp-comments-list-table.php index c4f288d8d0..b9eb0c2052 100644 --- a/wp-admin/includes/class-wp-comments-list-table.php +++ b/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/wp-admin/includes/meta-boxes.php b/wp-admin/includes/meta-boxes.php index c63474b29a..5228076079 100644 --- a/wp-admin/includes/meta-boxes.php +++ b/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/wp-includes/comment.php b/wp-includes/comment.php index ce0ebe769d..de23a72130 100644 --- a/wp-includes/comment.php +++ b/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/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 16018bb58a..1169b6b130 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/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/wp-includes/version.php b/wp-includes/version.php index d049007ddd..cc38f4f2b6 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-beta1-56746'; +$wp_version = '6.4-beta1-56747'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.