Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/12/2014 09:27:20 PM (11 years ago)
Author:
boonebgorges
Message:

Migrate user 'last_activity' data from usermeta to the activity table

Storing last_activity in usermeta caused severe bottlenecks on sites with
large user bases. The usermeta table has a tendency to get bloated. Its
option_value column is not indexed, and even if it were, it would not be
indexed properly for the kind of chronological sorting that BuddyPress was
using it for.

This changeset refactors all core last_activity user functionality, so that
the data is stored in the wp_bp_activity table (even when the activity
component is disabled).

For backward compatibility with plugins that reference last_activity metadata
entries, all last_activity data is retained in wp_usermeta, and new data will
be mirrored there until further notice.

See #5128

File:
1 edited

Legend:

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

    r7556 r7860  
    365365        global $wpdb;
    366366
    367         $user_ids = implode( ',', wp_parse_id_list( $user_ids ) );
    368 
    369         return $wpdb->get_results( $wpdb->prepare( "SELECT meta_value as last_activity, user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} ) ORDER BY meta_value DESC", bp_get_user_meta_key( 'last_activity' ) ) );
     367        $last_activities = BP_Core_User::get_last_activity( $user_ids );
     368
     369        // Sort and structure as expected in legacy function
     370        usort( $last_activities, create_function( '$a, $b', '
     371            if ( $a["date_recorded"] == $b["date_recorded"] ) {
     372                return 0;
     373            }
     374
     375            return ( strtotime( $a["date_recorded"] ) < strtotime( $b["date_recorded"] ) ) ? 1 : -1;
     376        ' ) );
     377
     378        $retval = array();
     379        foreach ( $last_activities as $last_activity ) {
     380            $u = new stdClass;
     381            $u->last_activity = $last_activity['date_recorded'];
     382            $u->user_id       = $last_activity['user_id'];
     383
     384            $retval[] = $u;
     385        }
     386
     387        return $retval;
    370388    }
    371389
Note: See TracChangeset for help on using the changeset viewer.