Skip to content

Commit 0494c38

Browse files
johhaphilippthun
andcommitted
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 (<super long list of service ids>))) ``` 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 <[email protected]>
1 parent d502820 commit 0494c38

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

app/models/services/service.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ def public_visible
3636

3737
def user_visibility_filter(current_user, operation=nil)
3838
visible_plans = ServicePlan.user_visible(current_user, operation)
39-
ids_from_plans = visible_plans.map(&:service_id).uniq
4039

41-
{ id: ids_from_plans }
40+
{ id: visible_plans.select(:service_id).distinct }
4241
end
4342

4443
def user_visibility_for_read(current_user, _admin_override)

0 commit comments

Comments
 (0)