Making WordPress.org

Changeset 12434


Ignore:
Timestamp:
02/28/2023 08:54:26 PM (17 months ago)
Author:
akirk
Message:

Polyglots Stats: Improve memory usage by optimizing SQL statements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-stats.php

    r12407 r12434  
    948948            ),
    949949        );
    950 
    951         $feedback_posts                 = new WP_Query( $feedback_posts_args );
    952         $original_strings_with_comments = number_format_i18n( $feedback_posts->post_count );
     950        $feedback_posts_count = $wpdb->get_var( $wpdb->prepare(
     951            "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status='publish' AND post_type=%s AND post_date > %s", $this::FEEDBACK_POST_TYPE, '2022-07-28'
     952        ));
     953        $original_strings_with_comments = number_format_i18n( $feedback_posts_count );
    953954
    954955        // Get the total number of comments.
     
    969970        // First, get the comments related with a translation, because we can get comments related
    970971        // only with the original.
    971         $comments = get_comments(
    972             array(
    973                 'post_type'  => $this::FEEDBACK_POST_TYPE,
    974                 'count'      => false,
    975                 'meta_key'   => 'translation_id',
    976                 'date_query' => array(
    977                     array( 'after' => '2022-07-28' ),
    978                 ),
    979             )
    980         );
    981 
    982         // Get the translation ids with a feedback comment.
    983         foreach ( $comments as $comment ) {
    984             $comment_meta_translation_ids[] = get_comment_meta( $comment->comment_post_ID, 'translation_id', true );
    985         }
    986         $comment_meta_translation_ids = array_unique( $comment_meta_translation_ids );
     972        $comment_meta_translation_ids = $wpdb->get_col( $wpdb->prepare(
     973            "SELECT cm.meta_value FROM $wpdb->commentmeta cm, $wpdb->comments c, $wpdb->posts p WHERE p.post_status='publish' AND p.post_type=%s AND p.post_date > %s AND cm.meta_key = 'translation_id' AND c.comment_post_id = p.id AND cm.comment_id = c.comment_id", $this::FEEDBACK_POST_TYPE, '2022-07-28'
     974        ));
    987975
    988976        // Check all comments with a related translation.
     
    10281016
    10291017        // Get most active commenter's.
    1030         $comments = get_comments(
    1031             array(
    1032                 'post_type'  => $this::FEEDBACK_POST_TYPE,
    1033                 'count'      => false,
    1034                 'date_query' => array(
    1035                     array( 'after' => '2022-07-28' ),
    1036                 ),
    1037             )
    1038         );
    1039         foreach ( $comments as $comment ) {
    1040             $comment_user_ids[] = $comment->user_id;
    1041         }
     1018        $comment_user_ids = $wpdb->get_col( $wpdb->prepare(
     1019            "SELECT c.user_id FROM $wpdb->comments c, $wpdb->posts p WHERE p.post_status='publish' AND p.post_type=%s AND p.post_date > %s AND c.comment_post_id = p.id", $this::FEEDBACK_POST_TYPE, '2022-07-28'
     1020        ));
    10421021
    10431022        $commenters_with_comment_count = array_count_values( $comment_user_ids );
Note: See TracChangeset for help on using the changeset viewer.