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

Resolve N+1 query issue with discussions

Closes #33013

See merge request !11775
This commit is contained in:
Sean McGivern 2017-06-01 09:01:08 +00:00
commit 69e31003be
6 changed files with 28 additions and 3 deletions

View file

@ -43,7 +43,12 @@ module Noteable
end
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
def discussions_resolvable?

View file

@ -21,7 +21,8 @@ class Discussion
end
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
# Returns an alphanumeric discussion ID based on `build_discussion_id`

View file

@ -110,7 +110,7 @@ class Note < ActiveRecord::Base
end
def discussions(context_noteable = nil)
Discussion.build_collection(fresh, context_noteable)
Discussion.build_collection(all.includes(:noteable).fresh, context_noteable)
end
def find_discussion(discussion_id)

View file

@ -0,0 +1,4 @@
---
title: Resolve N+1 query issue with discussions
merge_request:
author:

View file

@ -0,0 +1,15 @@
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