Skip to:
Content

BuddyPress.org

Changeset 11886


Ignore:
Timestamp:
03/05/2018 08:44:16 AM (9 years ago)
Author:
djpaul
Message:

Members, search: split user matching into seperate query.

Removing the subquery decreases the amount of rows MySQL needs to look at in the bp_activity table.
In the future, if we or WordPress add a user cache incrementor, we could cache this query.

Fixes #7442

Props brandonliles, r-a-y, boonebgorges

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-user-query.php

    r11705 r11886  
    414414                        }
    415415
    416                         $sql['where']['search'] = $wpdb->prepare(
    417                                 "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE %s OR user_login LIKE %s OR user_nicename LIKE %s OR user_nicename LIKE %s ) )",
     416                        $matched_user_ids = $wpdb->get_col( $wpdb->prepare(
     417                                "SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE %s OR user_login LIKE %s OR user_nicename LIKE %s OR user_nicename LIKE %s )",
    418418                                $search_terms_nospace,
    419419                                $search_terms_space,
    420420                                $search_terms_nospace,
    421421                                $search_terms_space
    422                         );
     422                        ) );
     423
     424                        $match_in_clause = empty( $matched_user_ids) ? 'NULL' : implode( ',', $matched_user_ids );
     425                        $sql['where']['search'] = "u.{$this->uid_name} IN ({$match_in_clause})";
    423426                }
    424427
  • trunk/src/bp-xprofile/bp-xprofile-functions.php

    r11849 r11886  
    785785        // Combine the core search (against wp_users) into a single OR clause
    786786        // with the xprofile_data search.
    787         $search_xprofile = $wpdb->prepare(
    788                 "u.{$query->uid_name} IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE %s OR value LIKE %s )",
     787        $matched_user_ids = $wpdb->get_col( $wpdb->prepare(
     788                "SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE %s OR value LIKE %s",
    789789                $search_terms_nospace,
    790790                $search_terms_space
    791         );
    792 
    793         $search_core     = $sql['where']['search'];
    794         $search_combined = "( {$search_xprofile} OR {$search_core} )";
    795         $sql['where']['search'] = $search_combined;
     791        ) );
     792
     793        if ( ! empty( $matched_user_ids ) ) {
     794                $search_core     = $sql['where']['search'];
     795                $search_combined = " ( u.{$query->uid_name} IN (" . implode(',', $matched_user_ids) . ") OR {$search_core} )";
     796                $sql['where']['search'] = $search_combined;
     797        }
    796798
    797799        return $sql;
Note: See TracChangeset for help on using the changeset viewer.