Commit be33d356 authored by Adam Hegyi's avatar Adam Hegyi

Fix maven package query performance

This change fixes the maven package finder query performance by using a
CTE fence.
parent 4ee871cb
...@@ -33,7 +33,7 @@ module Packages ...@@ -33,7 +33,7 @@ module Packages
end end
def packages_with_path def packages_with_path
matching_packages = base.only_maven_packages_with_path(path) matching_packages = base.only_maven_packages_with_path(path, use_cte: group.present?)
matching_packages = matching_packages.order_by_package_file if versionless_package?(matching_packages) matching_packages = matching_packages.order_by_package_file if versionless_package?(matching_packages)
matching_packages matching_packages
......
...@@ -141,9 +141,20 @@ class Packages::Package < ApplicationRecord ...@@ -141,9 +141,20 @@ class Packages::Package < ApplicationRecord
where(project_id: projects) where(project_id: projects)
end end
def self.only_maven_packages_with_path(path) def self.only_maven_packages_with_path(path, use_cte: false)
if use_cte && Feature.enabled?(:maven_metadata_by_path_with_optimization_fence)
# This is an optimization fence which assumes that looking up the Metadatum record by path (globally)
# and then filter down the packages (by project or by group and subgroups) will be cheaper than
# looking up all packages within a project or group and filter them by path.
inner_query = Packages::Maven::Metadatum.where(path: path).select(:id, :package_id)
cte = Gitlab::SQL::CTE.new(:maven_metadata_by_path, inner_query)
with(cte.to_arel)
.joins('INNER JOIN maven_metadata_by_path ON maven_metadata_by_path.package_id=packages_packages.id')
else
joins(:maven_metadatum).where(packages_maven_metadata: { path: path }) joins(:maven_metadatum).where(packages_maven_metadata: { path: path })
end end
end
def self.by_name_and_file_name(name, file_name) def self.by_name_and_file_name(name, file_name)
with_name(name) with_name(name)
......
---
name: maven_metadata_by_path_with_optimization_fence
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/57041
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/325460
milestone: '13.11'
type: development
group: group::optimize
default_enabled: false
...@@ -17,6 +17,7 @@ RSpec.describe ::Packages::Maven::PackageFinder do ...@@ -17,6 +17,7 @@ RSpec.describe ::Packages::Maven::PackageFinder do
group.add_developer(user) group.add_developer(user)
end end
shared_examples 'Packages::Maven::PackageFinder examples' do
describe '#execute!' do describe '#execute!' do
subject { finder.execute! } subject { finder.execute! }
...@@ -78,4 +79,27 @@ RSpec.describe ::Packages::Maven::PackageFinder do ...@@ -78,4 +79,27 @@ RSpec.describe ::Packages::Maven::PackageFinder do
it { is_expected.to eq(package2) } it { is_expected.to eq(package2) }
end end
end end
end
context 'when the maven_metadata_by_path_with_optimization_fence feature flag is off' do
before do
stub_feature_flags(maven_metadata_by_path_with_optimization_fence: false)
end
it_behaves_like 'Packages::Maven::PackageFinder examples'
end
context 'when the maven_metadata_by_path_with_optimization_fence feature flag is on' do
before do
stub_feature_flags(maven_metadata_by_path_with_optimization_fence: true)
end
it_behaves_like 'Packages::Maven::PackageFinder examples'
it 'uses CTE in the query' do
sql = described_class.new('some_path', user, group: group).send(:packages_with_path).to_sql
expect(sql).to include('WITH "maven_metadata_by_path" AS')
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