From d13c36f72de6b1c56e2063dafddd14ecbb430b9a Mon Sep 17 00:00:00 2001 From: Ahmad Sherif Date: Fri, 12 Aug 2016 14:29:59 +0200 Subject: Speed up todos queries by limiting the projects set we join with Closes #20828 --- app/finders/todos_finder.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'app/finders/todos_finder.rb') diff --git a/app/finders/todos_finder.rb b/app/finders/todos_finder.rb index ff866c2faa5..9b24a86e1c1 100644 --- a/app/finders/todos_finder.rb +++ b/app/finders/todos_finder.rb @@ -27,9 +27,11 @@ class TodosFinder items = by_action_id(items) items = by_action(items) items = by_author(items) - items = by_project(items) items = by_state(items) items = by_type(items) + # Filtering by project HAS TO be the last because we use + # the project IDs yielded by the todos query thus far + items = by_project(items) items.reorder(id: :desc) end @@ -91,13 +93,10 @@ class TodosFinder @project end - def projects - return @projects if defined?(@projects) - - if project? - @projects = project - else - @projects = ProjectsFinder.new.execute(current_user) + def projects(items) + item_project_ids = items.reorder(nil).select(:project_id) + ProjectsFinder.new.execute(current_user) do |relation| + relation.where(id: item_project_ids) end end @@ -136,8 +135,9 @@ class TodosFinder def by_project(items) if project? items = items.where(project: project) - elsif projects - items = items.merge(projects).joins(:project) + else + item_projects = projects(items) + items = items.merge(item_projects).joins(:project) end items -- cgit v1.2.3