Skip to:
Content

BuddyPress.org

Ticket #7442: bp-search-optimize.patch

File bp-search-optimize.patch, 2.3 KB (added by brandonliles, 7 years ago)

Subquery Materialization Approach

  • src/bp-core/classes/class-bp-user-query.php

     
    412412                                $search_terms_nospace = '%' . $search_terms . '%';
    413413                                $search_terms_space   = '%' . $search_terms . '%';
    414414                        }
    415 
    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 ) )",
     415                       
     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
     426                        $sql['where']['search'] = "u.{$this->uid_name} IN ({$match_in_clause})";
    423427                }
    424428
    425429                // Only use $member_type__in if $member_type is not set.
  • src/bp-xprofile/bp-xprofile-functions.php

     
    781781
    782782        // Combine the core search (against wp_users) into a single OR clause
    783783        // with the xprofile_data search.
    784         $search_xprofile = $wpdb->prepare(
    785                 "u.{$query->uid_name} IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE %s OR value LIKE %s )",
     784        $matched_user_ids = $wpdb->get_col($wpdb->prepare(
     785                "SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE %s OR value LIKE %s",
    786786                $search_terms_nospace,
    787787                $search_terms_space
    788         );
     788        ));
    789789
    790         $search_core     = $sql['where']['search'];
    791         $search_combined = "( {$search_xprofile} OR {$search_core} )";
    792         $sql['where']['search'] = $search_combined;
     790        if (!empty($matched_user_ids)) {
     791                $search_core     = $sql['where']['search'];
     792                $search_combined = " ( u.{$query->uid_name} IN (" . implode(',', $matched_user_ids) . ") OR {$search_core} )";
     793                $sql['where']['search'] = $search_combined;
     794        }
    793795
    794796        return $sql;
    795797}