diff options
author | Felipe Artur <felipefac@gmail.com> | 2017-01-23 18:40:25 -0200 |
---|---|---|
committer | Felipe Artur <felipefac@gmail.com> | 2017-02-09 17:40:37 -0200 |
commit | 0b14b654b6e5d936f7241dcc0c249e0d4cc42728 (patch) | |
tree | 714fff562236a7cbc78d837cea8ff7a648923166 /app/models | |
parent | c4fd6ff407cff8f2f742997a7400ba940a1fce5f (diff) | |
download | gitlab-ce-0b14b654b6e5d936f7241dcc0c249e0d4cc42728.tar.gz |
Gather issuable metadata to avoid n+ queries on index viewissue_25900_2
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/award_emoji.rb | 8 | ||||
-rw-r--r-- | app/models/concerns/issuable.rb | 5 | ||||
-rw-r--r-- | app/models/note.rb | 6 |
3 files changed, 19 insertions, 0 deletions
diff --git a/app/models/award_emoji.rb b/app/models/award_emoji.rb index 46b17479d6d..6937ad3bdd9 100644 --- a/app/models/award_emoji.rb +++ b/app/models/award_emoji.rb @@ -16,6 +16,14 @@ class AwardEmoji < ActiveRecord::Base scope :downvotes, -> { where(name: DOWNVOTE_NAME) } scope :upvotes, -> { where(name: UPVOTE_NAME) } + class << self + def votes_for_collection(ids, type) + select('name', 'awardable_id', 'COUNT(*) as count'). + where('name IN (?) AND awardable_type = ? AND awardable_id IN (?)', [DOWNVOTE_NAME, UPVOTE_NAME], type, ids). + group('name', 'awardable_id') + end + end + def downvote? self.name == DOWNVOTE_NAME end diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb index 3517969eabc..bfb54e878fc 100644 --- a/app/models/concerns/issuable.rb +++ b/app/models/concerns/issuable.rb @@ -15,6 +15,11 @@ module Issuable include Taskable include TimeTrackable + # This object is used to gather issuable meta data for displaying + # upvotes, downvotes and notes count for issues and merge requests + # lists avoiding n+1 queries and improving performance. + IssuableMeta = Struct.new(:upvotes, :downvotes, :notes_count) + included do cache_markdown_field :title, pipeline: :single_line cache_markdown_field :description diff --git a/app/models/note.rb b/app/models/note.rb index bf090a0438c..029fe667a45 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -108,6 +108,12 @@ class Note < ActiveRecord::Base Discussion.for_diff_notes(active_notes). map { |d| [d.line_code, d] }.to_h end + + def count_for_collection(ids, type) + user.select('noteable_id', 'COUNT(*) as count'). + group(:noteable_id). + where(noteable_type: type, noteable_id: ids) + end end def cross_reference? |