Opened 11 years ago
Closed 10 years ago
#5303 closed defect (bug) (fixed)
DISTINCT causing poor performance on user queries
Reported by: | johnjamesjacoby | Owned by: | johnjamesjacoby |
---|---|---|---|
Milestone: | 2.1 | Priority: | normal |
Severity: | normal | Version: | 1.2 |
Component: | Core | Keywords: | |
Cc: |
Description
On large installations with thousands/millions of users, a few of our user queries are looking for DISTINCT results on columns that already contain unique data; namely, the id
column.
See: BP_User_Query::prepare_user_ids_query()
Related, Andy had noticed this years ago in r3023 and removed one, but it eventually crept back in, either in a later merge, or copy/pasted from a different query.
Change History (18)
#7
in reply to:
↑ 2
@
11 years ago
Replying to johnjamesjacoby:
In 7698:
This change caused profile fields to repeat when viewing a member profile
#10
@
11 years ago
Hi.
I think also in /bp-activity/bp-activity-classes.php at line 295
the
// Select conditions $select_sql = "SELECT DISTINCT a.id";
should change to
// Select conditions $select_sql = "SELECT a.id";
#12
@
11 years ago
Sorry, about the false-positive.
Is it the same case at /bp-groups/bp-groups-classes.php on line 2033 ?
$group_sql = $wpdb->prepare( "SELECT DISTINCT group_id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_confirmed = 1 AND is_banned = 0{$pag_sql}", $user_id );
and line 2034
$total_groups = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT group_id) FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_confirmed = 1 AND is_banned = 0", $user_id ) );
#13
@
11 years ago
Yes, we can probably drop DISTINCT in these cases. There should be blocks elsewhere in BP to ensure that each group-user pair can only receive one membership. Though, fwiw, the impact of this particular DISTINCT is probably very, very small, because we're only selecting the heavily indexed group_id
column.
#17
@
10 years ago
- Keywords early removed
Is this done now? Are the remaining DISTINCTs required? The issue behind the commit that Boone mentions above indicates we should fix up that SQL query, and then remove the DISTINCT, but I'd prefer that on a separate ticket as this ticket is fairly general.
#18
@
10 years ago
- Resolution set to fixed
- Status changed from new to closed
Let's close this ticket, as (a) I think we've gotten the worse offenders, and (b) it's unlikely that anyone is going to do the kind of systematic review of all instances of DISTINCT that would be required to do more work toward the ticket as described.
Please open separate tickets for individual instances of DISTINCT that are causing problems.
I propose we remove DISTINCT's where they are not necessary. Patch incoming.