Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/18/2022 05:32:26 AM (3 years ago)
Author:
imath
Message:

Improve group members count query performance

[13103] introduced a change in 10.0.0 that can be very time consuming when a group has a lot of members.

To keep the main improvements of the referenced commit (only refreshing group members count when a user joins or leaves a group) but optimize queries performance, we are introducing a new way to count group members in the BP_Group_Member_Query.

We are also introducing a way to defer group members count when adding a batch of members to a group. Using bp_groups_defer_group_members_count() avoids to refresh the count each time a member of this batch is added. For more information about how to use this function, you can have a look at how BuddyPress is using it into src/bp-groups/bp-groups-admin.php.

Props dd32, espellcaste

See #8688 (trunk)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/classes/class-bp-groups-group.php

    r13185 r13280  
    17901790     */
    17911791    public static function get_total_member_count( $group_id, $skip_cache = false ) {
    1792         $cache_key = 'total_member_count';
    1793         $count     = groups_get_groupmeta( $group_id, $cache_key );
     1792        $meta_key = 'total_member_count';
     1793        $count    = groups_get_groupmeta( $group_id, $meta_key );
    17941794
    17951795        if ( false === $count || true === $skip_cache ) {
    1796             $members = groups_get_group_members(
     1796            $group_members = new BP_Group_Member_Query(
    17971797                array(
    17981798                    'group_id'   => $group_id,
    17991799                    'group_role' => array( 'member', 'admin', 'mod' ),
    1800                     'type'       => 'active',
     1800                    'count'      => true,
    18011801                )
    18021802            );
    18031803
    1804             $count = $members['count'] ? $members['count'] : 0;
    1805 
    1806             groups_update_groupmeta( $group_id, $cache_key, (int) $count );
     1804            $count = $group_members->total_users;
     1805            groups_update_groupmeta( $group_id, $meta_key, $count );
    18071806        }
    18081807
     
    18151814         * @param int $group_id The ID of the group.
    18161815         */
    1817         return (int) apply_filters( 'bp_groups_total_member_count', (int) $count, (int) $group_id );
     1816        return (int) apply_filters( 'bp_groups_total_member_count', $count, (int) $group_id );
    18181817    }
    18191818
Note: See TracChangeset for help on using the changeset viewer.