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
This commit is contained in:
Jonny Harris
2023-09-29 17:11:21 +00:00
parent 702e2c76a6
commit 4baf0a1eda
5 changed files with 27 additions and 19 deletions

View File

@@ -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(

View File

@@ -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' );

View File

@@ -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(

View File

@@ -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',
)
);

View File

@@ -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() );