Commit 0e335a91 authored by Tiger Watson's avatar Tiger Watson

Merge branch 'rp/reduce-n+1-queries-in-namespaces-api-1' into 'master'

Include route and hosted_plan to reduce n+1 queries in namespaces list api

See merge request gitlab-org/gitlab!43846
parents 56a0e576 2cd75d1f
......@@ -83,6 +83,7 @@ class Namespace < ApplicationRecord
scope :for_user, -> { where('type IS NULL') }
scope :sort_by_type, -> { order(Gitlab::Database.nulls_first_order(:type)) }
scope :include_route, -> { includes(:route) }
scope :with_statistics, -> do
joins('LEFT JOIN project_statistics ps ON ps.namespace_id = namespaces.id')
......
......@@ -36,6 +36,7 @@ module EE
accepts_nested_attributes_for :namespace_limit
scope :include_gitlab_subscription, -> { includes(:gitlab_subscription) }
scope :include_gitlab_subscription_with_hosted_plan, -> { includes(gitlab_subscription: :hosted_plan) }
scope :join_gitlab_subscription, -> { joins("LEFT OUTER JOIN gitlab_subscriptions ON gitlab_subscriptions.namespace_id=namespaces.id") }
scope :eligible_for_trial, -> do
......
......@@ -138,7 +138,7 @@ RSpec.describe API::Namespaces do
# an association on group, not namespace).
# The route adds one for each namespace.
# And more...
expect { get api("/namespaces", user) }.not_to exceed_all_query_limit(control).with_threshold(10)
expect { get api("/namespaces", user) }.not_to exceed_all_query_limit(control).with_threshold(7)
end
it 'includes max_seats_used' do
......
......@@ -32,7 +32,9 @@ module API
get do
namespaces = current_user.admin ? Namespace.all : current_user.namespaces
namespaces = namespaces.include_gitlab_subscription if Gitlab.ee?
namespaces = namespaces.include_route
namespaces = namespaces.include_gitlab_subscription_with_hosted_plan if Gitlab.ee?
namespaces = namespaces.search(params[:search]) if params[:search].present?
......
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