From e1f0a3dddf0c631d256a2ba2e880c657bf1a5078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serdar=20=C3=96zer?= Date: Mon, 11 May 2026 10:02:48 +0200 Subject: [PATCH 1/2] fixing N+1 query for service broker when it is space broker When service broker space scoped, during build_relationships inside service_broker_presenter.rb it makes select query for each of them and that makes N+1 problem. With eager loading it would be adding all the spaces as association into service broker so it won't make queries anymore will just fetch from cache. --- app/fetchers/service_broker_list_fetcher.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/fetchers/service_broker_list_fetcher.rb b/app/fetchers/service_broker_list_fetcher.rb index fc6cd6c7cde..0403f366c79 100644 --- a/app/fetchers/service_broker_list_fetcher.rb +++ b/app/fetchers/service_broker_list_fetcher.rb @@ -5,7 +5,7 @@ module VCAP::CloudController class ServiceBrokerListFetcher < BaseListFetcher class << self def fetch(message:, permitted_space_guids: nil, eager_loaded_associations: []) - dataset = ServiceBroker.dataset.eager(eager_loaded_associations) + dataset = ServiceBroker.dataset.eager(eager_loaded_associations).eager(:space) dataset = dataset.join(:spaces, id: Sequel[:service_brokers][:space_id]) if permitted_space_guids || message.requested?(:space_guids) From 49d99b874a94275b90c4449a3b8f1c3c520c7f4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serdar=20=C3=96zer?= Date: Tue, 12 May 2026 13:26:49 +0200 Subject: [PATCH 2/2] test: test added when serviece broker has space then sequel cache must have space association --- spec/unit/fetchers/service_broker_list_fetcher_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/unit/fetchers/service_broker_list_fetcher_spec.rb b/spec/unit/fetchers/service_broker_list_fetcher_spec.rb index d5a34a08e2e..b567aeb3e36 100644 --- a/spec/unit/fetchers/service_broker_list_fetcher_spec.rb +++ b/spec/unit/fetchers/service_broker_list_fetcher_spec.rb @@ -45,6 +45,13 @@ module VCAP::CloudController expect(dataset.all.first.associations.key?(:labels)).to be true expect(dataset.all.first.associations.key?(:annotations)).to be false end + + it 'sets space to nil for global brokers and to the space object for space-scoped brokers' do + brokers = fetcher.fetch(message:).all.index_by(&:name) + + expect(brokers[broker.name].associations[:space]).to be_nil + expect(brokers[space_scoped_broker_1.name].associations[:space]).to eq(space_1) + end end context 'when filtering by space GUIDs' do