Skip to content

Commit b8559a5

Browse files
authored
Improve permissions endpoint query performance (baserow#5464)
1 parent 7872562 commit b8559a5

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

backend/src/baserow/core/registries.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,11 +1024,29 @@ def get_objects_in_scopes(self, scopes: List[Any]) -> Dict[Any, Any]:
10241024
parent_scope_types.add(object_scope_type)
10251025

10261026
if parent_scopes:
1027-
query_result = list(
1028-
self.get_enhanced_queryset().filter(
1029-
self.get_filter_for_scopes(parent_scopes)
1027+
scopes_by_type = defaultdict(list)
1028+
for scope in parent_scopes:
1029+
scopes_by_type[object_scope_type_registry.get_by_model(scope)].append(
1030+
scope
10301031
)
1032+
1033+
enhanced_queryset = self.get_enhanced_queryset()
1034+
ordering = (
1035+
enhanced_queryset.query.order_by
1036+
or enhanced_queryset.model._meta.ordering
1037+
)
1038+
1039+
branches = [
1040+
enhanced_queryset.filter(
1041+
self.get_filter_for_scope_type(scope_type, typed_scopes)
1042+
).order_by()
1043+
for scope_type, typed_scopes in scopes_by_type.items()
1044+
]
1045+
combined = (
1046+
branches[0] if len(branches) == 1 else branches[0].union(*branches[1:])
10311047
)
1048+
combined = combined.order_by(*ordering)
1049+
query_result = list(combined)
10321050

10331051
# We have all the objects in the queryset, but now we want to sort them
10341052
# into buckets per original scope they are a child of.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "bug",
3+
"message": "Improve permissions endpoint query performance by using union instead of OR.",
4+
"issue_origin": "github",
5+
"issue_number": null,
6+
"domain": "database",
7+
"bullet_points": [],
8+
"created_at": "2026-06-03"
9+
}

0 commit comments

Comments
 (0)