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
This commit is contained in:
spacedmonkey 2023-09-29 17:13:24 +00:00
parent 3a3f9b80b7
commit 8aca3fdc68
5 changed files with 17 additions and 11 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

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