Make WordPress Core

Changeset 56747

Timestamp:
09/29/2023 05:11:21 PM (10 months ago)
Author:
spacedmonkey
Message:

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

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-comments-list-table.php

    r56665 r56747  
    178178                $args,
    179179                array(
    180                     'count'  => true,
    181                     'offset' => 0,
    182                     'number' => 0,
     180                    'count'   => true,
     181                    'offset'  => 0,
     182                    'number'  => 0,
     183                    'orderby' => 'none',
    183184                )
    184185            )
     
    299300                        'user_id' => $current_user_id,
    300301                        'count'   => true,
     302
    301303                    )
    302304                );
     
    519521                if ( get_comments(
    520522                    array(
    521                         'number' => 1,
    522                         'type'   => $type,
     523                        'count'   => true,
     524                        'orderby' => 'none',
     525                        'type'    => $type,
    523526                    )
    524527                ) ) {
  • trunk/src/wp-admin/includes/meta-boxes.php

    r56571 r56747  
    902902        array(
    903903            'post_id' => $post->ID,
    904             'number'  => 1,
    905904            'count'   => true,
     905
    906906        )
    907907    );
  • trunk/src/wp-includes/comment.php

    r56548 r56747  
    397397        'count'                     => true,
    398398        'update_comment_meta_cache' => false,
     399
    399400    );
    400401    if ( $post_id > 0 ) {
     
    11151116            'count'      => true,
    11161117            'status'     => 'approve',
     1118
    11171119            'parent'     => 0,
    11181120            'date_query' => array(
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r56586 r56747  
    296296            unset( $prepared_args['number'], $prepared_args['offset'] );
    297297
    298             $query                  = new WP_Comment_Query();
    299             $prepared_args['count'] = true;
     298            $query                    = new WP_Comment_Query();
     299            $prepared_args['count']   = true;
     300            $prepared_args['orderby'] = 'none';
    300301
    301302            $total_comments = $query->query( $prepared_args );
     
    11891190        $comment_children = $comment->get_children(
    11901191            array(
    1191                 'number' => 1,
    1192                 'count'  => true,
     1192                ',
     1193                ',
    11931194            )
    11941195        );
  • trunk/tests/phpunit/tests/comment/query.php

    r56549 r56747  
    30923092        $found = $q->query(
    30933093            array(
    3094                 'count' => true,
     3094                'count'   => true,
     3095                'orderby' => 'none',
    30953096            )
    30963097        );
     
    31303131            array(
    31313132                'count'      => true,
     3133
    31323134                'meta_query' => array(
    31333135                    array(
     
    50025004        $query_1           = $q->query(
    50035005            array(
    5004                 'fields' => 'ids',
    5005                 'number' => 3,
    5006                 'order'  => 'ASC',
    5007                 'count'  => true,
     5006                'fields' => 'ids',
     5007                'number' => 3,
     5008                'order',
     5009                'count'  => true,
    50085010            )
    50095011        );
     
    50125014        $query_2 = $q->query(
    50135015            array(
    5014                 'fields' => 'ids',
    5015                 'number' => 3,
    5016                 'order'  => 'ASC',
    5017                 'count'  => true,
     5016                'fields' => 'ids',
     5017                'number' => 3,
     5018                'order',
     5019                'count'  => true,
    50185020            )
    50195021        );
Note: See TracChangeset for help on using the changeset viewer.