Skip to content
代码片段 群组 项目
提交 fad40e75 编辑于 作者: Vasilii Iakliushin's avatar Vasilii Iakliushin
浏览文件

Fix N+1 problem for notes association

Follow-up for https://gitlab.com/gitlab-org/gitlab/-/issues/347407

In previous MR I added a permission verification when user requests
the list of participants. But it increased the number of db queries.

I found that notes association does not preload project object and
that generates additional queries.

Changelog: fixed
上级 783ce2d8
No related branches found
No related tags found
无相关合并请求
......@@ -61,6 +61,11 @@ def award_emojis_loaded?
# We check first if we're loaded to not load unnecessarily.
loaded? && to_a.all? { |note| note.association(:award_emoji).loaded? }
end
def projects_loaded?
# We check first if we're loaded to not load unnecessarily.
loaded? && to_a.all? { |note| note.association(:project).loaded? }
end
end
has_many :note_authors, -> { distinct }, through: :notes, source: :author
......@@ -524,6 +529,7 @@ def notes_with_associations
includes = []
includes << :author unless notes.authors_loaded?
includes << :award_emoji unless notes.award_emojis_loaded?
includes << :project unless notes.projects_loaded?
if includes.any?
notes.includes(includes)
......
......@@ -49,6 +49,19 @@
it 'returns all participants for this user' do
is_expected.to match_array([issue.author, note.author])
end
it 'does not execute N+1 for project relation' do
query = -> { resolve(described_class, args: {}, ctx: { current_user: current_user }, obj: issue)&.items }
# warm-up
query.call
control_count = ActiveRecord::QueryRecorder.new { query.call }
create(:note, :confidential, project: project, noteable: issue, author: create(:user))
expect { query.call }.not_to exceed_query_limit(control_count)
end
end
end
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册