Commit 707148eb authored by Robert Speicher's avatar Robert Speicher

Merge branch '47496-more-n-1s-in-calculating-notification-recipients' into 'master'

Resolve "More N+1s in calculating notification recipients"

Closes #47496

See merge request gitlab-org/gitlab-ce!22050
parents fc411207 0199c92c
...@@ -31,9 +31,11 @@ module Subscribable ...@@ -31,9 +31,11 @@ module Subscribable
end end
def subscribers(project) def subscribers(project)
subscriptions_available(project) relation = subscriptions_available(project)
.where(subscribed: true) .where(subscribed: true)
.map(&:user) .select(:user_id)
User.where(id: relation)
end end
def toggle_subscription(user, project = nil) def toggle_subscription(user, project = nil)
......
...@@ -390,7 +390,11 @@ class ProjectPolicy < BasePolicy ...@@ -390,7 +390,11 @@ class ProjectPolicy < BasePolicy
greedy_load_subject ||= !@user.persisted? greedy_load_subject ||= !@user.persisted?
if greedy_load_subject if greedy_load_subject
project.team.members.include?(user) # We want to load all the members with one query. Calling #include? on
# project.team.members will perform a separate query for each user, unless
# project.team.members was loaded before somewhere else. Calling #to_a
# ensures it's always loaded before checking for membership.
project.team.members.to_a.include?(user)
else else
# otherwise we just make a specific query for # otherwise we just make a specific query for
# this particular user. # this particular user.
......
---
title: Reduce queries needed to compute notification recipients
merge_request: 22050
author:
type: performance
...@@ -10,27 +10,50 @@ describe NotificationRecipientService do ...@@ -10,27 +10,50 @@ describe NotificationRecipientService do
let(:issue) { create(:issue, project: project, assignees: [assignee]) } let(:issue) { create(:issue, project: project, assignees: [assignee]) }
let(:note) { create(:note_on_issue, noteable: issue, project_id: issue.project_id) } let(:note) { create(:note_on_issue, noteable: issue, project_id: issue.project_id) }
def create_watcher shared_examples 'no N+1 queries' do
watcher = create(:user) it 'avoids N+1 queries', :request_store do
create(:notification_setting, source: project, user: watcher, level: :watch) create_user
other_projects.each do |other_project| service.build_new_note_recipients(note)
create(:notification_setting, source: other_project, user: watcher, level: :watch)
control_count = ActiveRecord::QueryRecorder.new do
service.build_new_note_recipients(note)
end
create_user
expect { service.build_new_note_recipients(note) }.not_to exceed_query_limit(control_count)
end end
end end
it 'avoids N+1 queries', :request_store do context 'when there are multiple watchers' do
create_watcher def create_user
watcher = create(:user)
create(:notification_setting, source: project, user: watcher, level: :watch)
other_projects.each do |other_project|
create(:notification_setting, source: other_project, user: watcher, level: :watch)
end
end
service.build_new_note_recipients(note) include_examples 'no N+1 queries'
end
control_count = ActiveRecord::QueryRecorder.new do context 'when there are multiple subscribers' do
service.build_new_note_recipients(note) def create_user
subscriber = create(:user)
issue.subscriptions.create(user: subscriber, project: project, subscribed: true)
end end
create_watcher include_examples 'no N+1 queries'
expect { service.build_new_note_recipients(note) }.not_to exceed_query_limit(control_count) context 'when the project is private' do
before do
project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
end
include_examples 'no N+1 queries'
end
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment