From 0494c3836237298e7507bfae8adb643679fc1ccb Mon Sep 17 00:00:00 2001 From: Johannes Haass Date: Tue, 5 Dec 2023 15:46:50 +0100 Subject: [PATCH] Use sub query for user visibility filter In case there are many service instances related db queries might become very complex: ``` SELECT * FROM \"services\" WHERE ((\"services\".\"id\" IN (7)) AND (\"id\" IN ())) ``` This can lead to high memory consumption on the api VMs and can also cause memory bloats as everything is loaded into memory. With this change we change the query to use a sub query instead: ``` SELECT * FROM \"services\" WHERE ((\"services\".\"id\" IN (7)) AND (\"id\" IN (SELECT DISTINCT \"service_id\" FROM \"service_plans\"))) ``` Co-authored-by: Philipp Thun --- app/models/services/service.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/models/services/service.rb b/app/models/services/service.rb index 80c1153207b..811eab15f47 100644 --- a/app/models/services/service.rb +++ b/app/models/services/service.rb @@ -36,9 +36,8 @@ def public_visible def user_visibility_filter(current_user, operation=nil) visible_plans = ServicePlan.user_visible(current_user, operation) - ids_from_plans = visible_plans.map(&:service_id).uniq - { id: ids_from_plans } + { id: visible_plans.select(:service_id).distinct } end def user_visibility_for_read(current_user, _admin_override)