Commit 6af35b45 authored by Krasimir Angelov's avatar Krasimir Angelov

Improve performance of pages_virtual_domain

When exposing Namespace#pages_virtual_domain it eventually delegates
`private_pages?` to `project_feature`, and needs `route` for
`full_path`. Preloading these two associations avoids N+1 queries
problem.

Related to https://gitlab.com/gitlab-org/gitlab/issues/207096.
parent fcbaaaff
......@@ -326,7 +326,10 @@ class Namespace < ApplicationRecord
end
def pages_virtual_domain
Pages::VirtualDomain.new(all_projects_with_pages, trim_prefix: full_path)
Pages::VirtualDomain.new(
all_projects_with_pages.includes(:route, :project_feature),
trim_prefix: full_path
)
end
def closest_setting(name)
......
......@@ -983,6 +983,24 @@ describe Namespace do
expect(virtual_domain.lookup_paths).not_to be_empty
end
end
it 'preloads project_feature and route' do
project2 = create(:project, namespace: namespace)
project3 = create(:project, namespace: namespace)
project.mark_pages_as_deployed
project2.mark_pages_as_deployed
project3.mark_pages_as_deployed
virtual_domain = namespace.pages_virtual_domain
queries = ActiveRecord::QueryRecorder.new { virtual_domain.lookup_paths }
# 1 to load projects
# 1 to preload project features
# 1 to load routes
expect(queries.count).to eq(3)
end
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