Skip to:
Content

BuddyPress.org

Changeset 5940


Ignore:
Timestamp:
03/20/2012 09:16:58 PM (13 years ago)
Author:
boonebgorges
Message:

Provide index hints for queries in BP_Activity_Activity::get(). See #4045.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-classes.php

    r5926 r5940  
    107107        $select_sql = "SELECT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name";
    108108
    109         $from_sql = " FROM {$bp->activity->table_name} a LEFT JOIN {$wpdb->users} u ON a.user_id = u.ID";
     109        $from_sql = " FROM {$bp->activity->table_name} a USE INDEX (date_recorded) LEFT JOIN {$wpdb->users} u ON a.user_id = u.ID";
    110110
    111111        // Where conditions
     
    156156        $where_sql = 'WHERE ' . join( ' AND ', $where_conditions );
    157157
     158        // Define the preferred order for indexes
     159        $indexes = apply_filters( 'bp_activity_preferred_index_order', array( 'user_id', 'item_id', 'secondary_item_id', 'date_recorded', 'component', 'type', 'hide_sitewide', 'is_spam' ) );
     160       
     161        foreach( $indexes as $key => $index ) {
     162            if ( false !== strpos( $where_sql, $index ) ) {
     163                $the_index = $index;
     164                break; // Take the first one we find
     165            }
     166        }
     167
     168        if ( !empty( $the_index ) ) {
     169            $index_hint_sql = $wpdb->prepare( "USE INDEX ({$the_index})" );
     170        } else {
     171            $index_hint_sql = '';
     172        }
     173
    158174        if ( !empty( $per_page ) && !empty( $page ) ) {
    159175
     
    168184        }
    169185
    170         $total_activities_sql = apply_filters( 'bp_activity_total_activities_sql', $wpdb->prepare( "SELECT count(a.id) FROM {$bp->activity->table_name} a {$where_sql} ORDER BY a.date_recorded {$sort}" ), $where_sql, $sort );
     186        $total_activities_sql = apply_filters( 'bp_activity_total_activities_sql', $wpdb->prepare( "SELECT count(a.id) FROM {$bp->activity->table_name} a {$index_hint_sql} {$where_sql} ORDER BY a.date_recorded {$sort}" ), $where_sql, $sort );
    171187
    172188        $total_activities = $wpdb->get_var( $total_activities_sql );
Note: See TracChangeset for help on using the changeset viewer.