Commit 1f55e4a3 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'fj-227311-optimize-snippets-finder-query' into 'master'

Improve snippet finders queries

See merge request gitlab-org/gitlab!36292
parents 3c3635fc 47c1a175
......@@ -117,7 +117,7 @@ class SnippetsFinder < UnionFinder
queries << snippets_of_authorized_projects if current_user
end
find_union(queries, Snippet)
prepared_union(queries)
end
def snippets_for_a_single_project
......@@ -208,6 +208,17 @@ class SnippetsFinder < UnionFinder
def sort_param
sort.presence || 'id_desc'
end
def prepared_union(queries)
return Snippet.none if queries.empty?
return queries.first if queries.length == 1
# The queries are going to be part of a global `where`
# therefore we only need to retrieve the `id` column
# which will speed the query
queries.map! { |rel| rel.select(:id) }
Snippet.id_in(find_union(queries, Snippet))
end
end
SnippetsFinder.prepend_if_ee('EE::SnippetsFinder')
---
title: Improve snippet finders queries
merge_request: 36292
author:
type: performance
......@@ -34,7 +34,7 @@ module EE
queries << snippets_of_authorized_projects unless only_personal
end
find_union(queries, ::Snippet)
prepared_union(queries)
end
def restricted_personal_snippets
......
......@@ -181,6 +181,14 @@ RSpec.describe SnippetsFinder do
private_project_snippet
)
end
context 'when no personal snippets are visible' do
let(:user) { nil }
it 'does not return any record' do
expect(subject).to be_empty
end
end
end
end
end
......@@ -283,6 +283,12 @@ RSpec.describe SnippetsFinder do
it 'returns only personal snippets when the user cannot read cross project' do
expect(described_class.new(user).execute).to contain_exactly(private_personal_snippet, internal_personal_snippet, public_personal_snippet)
end
context 'when only project snippets are required' do
it 'returns no records' do
expect(described_class.new(user, only_project: true).execute).to be_empty
end
end
end
context 'when project snippets are disabled' do
......
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