diff options
author | Stan Hu <stanhu@gmail.com> | 2019-03-02 20:31:36 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2019-03-06 18:03:46 +0300 |
commit | 062efe4f7a83fb2b6d951b314692cca9ee8731cd (patch) | |
tree | 2907359acdf497130b38c75555056226189af829 /app/models/todo.rb | |
parent | a592a78072bb44fed1a25c25f2cabdc4cf4bc0bd (diff) |
Significantly reduce N+1 queries in /api/v4/todos endpoint
By preloading associations and batching issuable metadata lookups,
we can significantly cut the number of SQL queries needed to load
the Todos API endpoint.
On GitLab.com, my own tests showed my user's SQL queries went
from 365 to under 60 SQL queries.
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/40378
Diffstat (limited to 'app/models/todo.rb')
-rw-r--r-- | app/models/todo.rb | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/app/models/todo.rb b/app/models/todo.rb index d9b86d941b6..2b0dee875a3 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -31,7 +31,13 @@ class Todo < ActiveRecord::Base belongs_to :note belongs_to :project belongs_to :group - belongs_to :target, polymorphic: true, touch: true # rubocop:disable Cop/PolymorphicAssociations + belongs_to :target, -> { + if self.klass.respond_to?(:with_api_entity_associations) + self.with_api_entity_associations + else + self + end + }, polymorphic: true, touch: true # rubocop:disable Cop/PolymorphicAssociations belongs_to :user delegate :name, :email, to: :author, prefix: true, allow_nil: true @@ -52,6 +58,7 @@ class Todo < ActiveRecord::Base scope :for_type, -> (type) { where(target_type: type) } scope :for_target, -> (id) { where(target_id: id) } scope :for_commit, -> (id) { where(commit_id: id) } + scope :with_api_entity_associations, -> { preload(:target, :author, :note, group: :route, project: [:route, { namespace: :route }]) } state_machine :state, initial: :pending do event :done do |