Commit 69e31003 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'dm-discussions-n-plus-1' into 'master'

Resolve N+1 query issue with discussions

Closes #33013

See merge request !11775
parents dd0f8b8c 78207b95
...@@ -43,7 +43,12 @@ module Noteable ...@@ -43,7 +43,12 @@ module Noteable
end end
def resolvable_discussions def resolvable_discussions
@resolvable_discussions ||= discussion_notes.resolvable.discussions(self) @resolvable_discussions ||=
if defined?(@discussions)
@discussions.select(&:resolvable?)
else
discussion_notes.resolvable.discussions(self)
end
end end
def discussions_resolvable? def discussions_resolvable?
......
...@@ -21,7 +21,8 @@ class Discussion ...@@ -21,7 +21,8 @@ class Discussion
end end
def self.build_collection(notes, context_noteable = nil) def self.build_collection(notes, context_noteable = nil)
notes.group_by { |n| n.discussion_id(context_noteable) }.values.map { |notes| build(notes, context_noteable) } grouped_notes = notes.group_by { |n| n.discussion_id(context_noteable) }
grouped_notes.values.map { |notes| build(notes, context_noteable) }
end end
# Returns an alphanumeric discussion ID based on `build_discussion_id` # Returns an alphanumeric discussion ID based on `build_discussion_id`
......
...@@ -110,7 +110,7 @@ class Note < ActiveRecord::Base ...@@ -110,7 +110,7 @@ class Note < ActiveRecord::Base
end end
def discussions(context_noteable = nil) def discussions(context_noteable = nil)
Discussion.build_collection(fresh, context_noteable) Discussion.build_collection(all.includes(:noteable).fresh, context_noteable)
end end
def find_discussion(discussion_id) def find_discussion(discussion_id)
......
---
title: Resolve N+1 query issue with discussions
merge_request:
author:
module ActiveRecord
module Associations
class Preloader
module NoCommitPreloader
def preloader_for(reflection, owners, rhs_klass)
return NullPreloader if rhs_klass == ::Commit
super
end
end
prepend NoCommitPreloader
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