Commit 9127ae5c authored by Zeger-Jan van de Weg's avatar Zeger-Jan van de Weg

Improve performance of queries

Credits to Douwe Maan
parent ac44e384
...@@ -63,6 +63,7 @@ v 8.1.0 (unreleased) ...@@ -63,6 +63,7 @@ v 8.1.0 (unreleased)
- Only render 404 page from /public - Only render 404 page from /public
- Hide passwords from services API (Alex Lossent) - Hide passwords from services API (Alex Lossent)
- Fix: Images cannot show when projects' path was changed - Fix: Images cannot show when projects' path was changed
- Optimize query when filtering on issuables (Zeger-Jan van de Weg)
v 8.0.4 v 8.0.4
- Fix Message-ID header to be RFC 2111-compliant to prevent e-mails being dropped (Stan Hu) - Fix Message-ID header to be RFC 2111-compliant to prevent e-mails being dropped (Stan Hu)
......
...@@ -53,15 +53,36 @@ class IssuableFinder ...@@ -53,15 +53,36 @@ class IssuableFinder
end end
end end
def project?
params[:project_id].present?
end
def project def project
return @project if defined?(@project) return @project if defined?(@project)
@project = if project?
if params[:project_id].present? @project = Project.find(params[:project_id])
Project.find(params[:project_id])
else unless Ability.abilities.allowed?(current_user, :read_project, @project)
nil @project = nil
end end
else
@project = nil
end
@project
end
def projects
return if project?
return @projects if defined?(@projects)
if current_user && params[:authorized_only].presence && !current_user_related?
current_user.authorized_projects
else
ProjectsFinder.new.execute(current_user)
end
end end
def search def search
...@@ -84,8 +105,10 @@ class IssuableFinder ...@@ -84,8 +105,10 @@ class IssuableFinder
scope = scope =
if project if project
project.milestones project.milestones
elsif projects
Milestone.where(project_id: projects)
else else
Milestone.all Milestone.none
end end
scope.where(title: params[:milestone_title]) scope.where(title: params[:milestone_title])
...@@ -127,19 +150,7 @@ class IssuableFinder ...@@ -127,19 +150,7 @@ class IssuableFinder
private private
def init_collection def init_collection
table_name = klass.table_name klass.all
if project
if Ability.abilities.allowed?(current_user, :read_project, project)
project.send(table_name)
else
[]
end
elsif current_user && params[:authorized_only].presence && !current_user_related?
klass.of_projects(current_user.authorized_projects).references(:project)
else
klass.of_projects(ProjectsFinder.new.execute(current_user)).references(:project)
end
end end
def by_scope(items) def by_scope(items)
...@@ -177,7 +188,14 @@ class IssuableFinder ...@@ -177,7 +188,14 @@ class IssuableFinder
end end
def by_project(items) def by_project(items)
items = items.of_projects(project.id) if project items =
if project
items.of_projects(project)
elsif projects
items.of_projects(projects).references(:project)
else
items.none
end
items items
end end
...@@ -223,17 +241,17 @@ class IssuableFinder ...@@ -223,17 +241,17 @@ class IssuableFinder
def by_label(items) def by_label(items)
if params[:label_name].present? if params[:label_name].present?
if params[:label_name] == Label::None.title if params[:label_name] == Label::None.title
item_ids = LabelLink.where(target_type: klass.name).pluck(:target_id) items = items.
joins("LEFT OUTER JOIN label_links ON label_links.target_type = '#{klass.name}' AND label_links.target_id = #{klass.table_name}.id").
items = items.where('id NOT IN (?)', item_ids) where(label_links: { id: nil })
else else
label_names = params[:label_name].split(",") label_names = params[:label_name].split(",")
item_ids = LabelLink.joins(:label). items = items.joins(:labels).where(labels: { title: label_names })
where('labels.title in (?)', label_names).
where(target_type: klass.name).pluck(:target_id)
items = items.where(id: item_ids) if project
items = items.where('labels.project_id = :id', id: project.id)
end
end end
end end
......
class AddIndexToMilestones < ActiveRecord::Migration
def change
add_index :milestones, :title
add_index :labels, :title
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