Commit 24b945ec authored by Igor Drozdov's avatar Igor Drozdov

Merge branch 'fj-fix-n-plus-one-in-dashboard-snippets' into 'master'

Fix N+1 in Dashboard::SnippetsController#index

See merge request gitlab-org/gitlab!50569
parents 86b9d1a8 007eae72
...@@ -18,6 +18,7 @@ class Dashboard::SnippetsController < Dashboard::ApplicationController ...@@ -18,6 +18,7 @@ class Dashboard::SnippetsController < Dashboard::ApplicationController
.execute .execute
.page(params[:page]) .page(params[:page])
.inc_author .inc_author
.inc_projects_namespace_route
return if redirect_out_of_range(@snippets) return if redirect_out_of_range(@snippets)
......
...@@ -83,6 +83,7 @@ class Snippet < ApplicationRecord ...@@ -83,6 +83,7 @@ class Snippet < ApplicationRecord
scope :inc_author, -> { includes(:author) } scope :inc_author, -> { includes(:author) }
scope :inc_relations_for_view, -> { includes(author: :status) } scope :inc_relations_for_view, -> { includes(author: :status) }
scope :with_statistics, -> { joins(:statistics) } scope :with_statistics, -> { joins(:statistics) }
scope :inc_projects_namespace_route, -> { includes(project: [:route, :namespace]) }
attr_mentionable :description attr_mentionable :description
......
---
title: Fix N+1 when rendering snippets in the dashboard
merge_request: 50569
author:
type: performance
...@@ -28,5 +28,24 @@ RSpec.describe Dashboard::SnippetsController do ...@@ -28,5 +28,24 @@ RSpec.describe Dashboard::SnippetsController do
end end
it_behaves_like 'snippets sort order' it_behaves_like 'snippets sort order'
context 'when views are rendered' do
render_views
it 'avoids N+1 database queries' do
# Warming call to load everything non snippet related
get(:index)
project = create(:project, namespace: user.namespace)
create(:project_snippet, project: project, author: user)
control_count = ActiveRecord::QueryRecorder.new { get(:index) }.count
project = create(:project, namespace: user.namespace)
create(:project_snippet, project: project, author: user)
expect { get(:index) }.not_to exceed_query_limit(control_count)
end
end
end 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