Skip to:
Content

bbPress.org

Changeset 6446


Ignore:
Timestamp:
06/01/2017 02:53:56 PM (7 years ago)
Author:
johnjamesjacoby
Message:

Engagements: Performance optimizations to bbp_update_topic_voice_count().

  • Use a UNION query strategy (only on the wp_posts table)
  • Compare old count to new count before deleting & updating engagements

Fixes #3083. Props januzi_pl.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/topics/functions.php

    r6438 r6446  
    29792979    }
    29802980
    2981     // Delete all engagements
    2982     delete_post_meta( $topic_id, '_bbp_engagement' );
    2983 
    29842981    // Query the DB to get voices in this topic
     2982    // See: https://bbpress.trac.wordpress.org/ticket/3083
    29852983    $bbp_db  = bbp_db();
    2986     $sql     = "SELECT DISTINCT post_author FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = %s AND post_type = %s ) OR ( ID = %d AND post_type = %s )";
    2987     $query   = $bbp_db->prepare( $sql, $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() );
     2984    $sql     = "SELECT DISTINCT( post_author ) FROM (
     2985                    SELECT post_author FROM {$bbp_db->posts}
     2986                        WHERE ( ID = %d AND post_type = %s )
     2987                    UNION
     2988                    SELECT post_author FROM {$bbp_db->posts}
     2989                        WHERE ( post_parent = %d AND post_status = %s AND post_type = %s )
     2990                ) as u1";
     2991    $query   = $bbp_db->prepare( $sql, $topic_id, bbp_get_topic_post_type(), $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type() );
    29882992    $results = $bbp_db->get_col( $query );
    29892993
    29902994    // Parse results into voices
    2991     $voices  = ! is_wp_error( $results )
     2995    $new_voices  = ! is_wp_error( $results )
    29922996        ? wp_parse_id_list( array_filter( $results ) )
    29932997        : array();
    29942998
     2999    // Get the old voices
     3000    $old_voices = bbp_get_topic_engagements( $topic_id );
     3001
     3002    // Get the count
     3003    $new_count = count( $new_voices );
     3004    $old_count = count( $old_voices );
     3005
     3006    // Only recalculate if count is different
     3007    if ( $new_count !== $old_count ) {
     3008
     3009        // Delete all engagements
     3010        delete_post_meta( $topic_id, '_bbp_engagement' );
     3011
     3012        // Update the voice count for this topic id
     3013        foreach ( $new_voices as $user_id ) {
     3014            bbp_add_user_engagement( $user_id, $topic_id );
     3015        }
     3016    }
     3017
    29953018    // Update the voice count for this topic id
    2996     foreach ( $voices as $user_id ) {
    2997         bbp_add_user_engagement( $user_id, $topic_id );
    2998     }
    2999 
    3000     // Get the count
    3001     $count = count( $voices );
    3002 
    3003     // Update the voice count for this topic id
    3004     update_post_meta( $topic_id, '_bbp_voice_count', $count );
     3019    update_post_meta( $topic_id, '_bbp_voice_count', $new_count );
    30053020
    30063021    // Filter & return
    3007     return (int) apply_filters( 'bbp_update_topic_voice_count', $count, $topic_id );
     3022    return (int) apply_filters( 'bbp_update_topic_voice_count', $new_count, $topic_id );
    30083023}
    30093024
Note: See TracChangeset for help on using the changeset viewer.