Skip to content

Commit

Permalink
Use sub query for user visibility filter
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
johha and philippthun committed Dec 5, 2023
1 parent 7eeac9a commit ccd2ffb
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions app/models/services/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ccd2ffb

Please sign in to comment.