Resolve "MembersFinder contains slow database query with OR conditions"
This commit is contained in:
parent
18e3dcc65e
commit
66d48385ec
3 changed files with 13 additions and 6 deletions
|
@ -1,6 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class GroupMembersFinder
|
||||
class GroupMembersFinder < UnionFinder
|
||||
def initialize(group)
|
||||
@group = group
|
||||
end
|
||||
|
@ -8,18 +8,18 @@ class GroupMembersFinder
|
|||
# rubocop: disable CodeReuse/ActiveRecord
|
||||
def execute(include_descendants: false)
|
||||
group_members = @group.members
|
||||
wheres = []
|
||||
relations = []
|
||||
|
||||
return group_members unless @group.parent || include_descendants
|
||||
|
||||
wheres << "members.id IN (#{group_members.select(:id).to_sql})"
|
||||
relations << group_members
|
||||
|
||||
if @group.parent
|
||||
parents_members = GroupMember.non_request
|
||||
.where(source_id: @group.ancestors.select(:id))
|
||||
.where.not(user_id: @group.users.select(:id))
|
||||
|
||||
wheres << "members.id IN (#{parents_members.select(:id).to_sql})"
|
||||
relations << parents_members
|
||||
end
|
||||
|
||||
if include_descendants
|
||||
|
@ -27,10 +27,10 @@ class GroupMembersFinder
|
|||
.where(source_id: @group.descendants.select(:id))
|
||||
.where.not(user_id: @group.users.select(:id))
|
||||
|
||||
wheres << "members.id IN (#{descendant_members.select(:id).to_sql})"
|
||||
relations << descendant_members
|
||||
end
|
||||
|
||||
GroupMember.where(wheres.join(' OR '))
|
||||
find_union(relations, GroupMember)
|
||||
end
|
||||
# rubocop: enable CodeReuse/ActiveRecord
|
||||
end
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class GroupMember < Member
|
||||
include FromUnion
|
||||
|
||||
SOURCE_TYPE = 'Namespace'.freeze
|
||||
|
||||
belongs_to :group, foreign_key: 'source_id'
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
title: Improve MembersFinder query performance using UNION
|
||||
merge_request: 30451
|
||||
author: Jacopo Beschi @jacopo-beschi
|
||||
type: performance
|
Loading…
Reference in a new issue