Skip to content

Commit

Permalink
Speed up scenario filtered entity subquery
Browse files Browse the repository at this point in the history
Seems better to compute and join element count earlier in the query
rather than later
  • Loading branch information
manuelma committed Feb 1, 2024
1 parent 2da3e72 commit 6cf762c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
24 changes: 18 additions & 6 deletions spinedb_api/filters/scenario_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ def _make_scenario_filtered_entity_sq(db_map, state):
Alias: a subquery for entity filtered by selected scenario
"""
ext_entity_sq = _ext_entity_sq(db_map, state)
ext_entity_element_count_sq = (
db_map.query(
db_map.entity_element_sq.c.entity_id,
func.count(db_map.entity_element_sq.c.element_id).label("element_count"),
)
.group_by(db_map.entity_element_sq.c.entity_id)
.subquery()
)
ext_entity_class_dimension_count_sq = (
db_map.query(
db_map.entity_class_dimension_sq.c.entity_class_id,
Expand All @@ -281,17 +289,21 @@ def _make_scenario_filtered_entity_sq(db_map, state):
and_(ext_entity_sq.c.active == None, ext_entity_sq.c.active_by_default == True),
),
)
.outerjoin(
ext_entity_element_count_sq,
ext_entity_element_count_sq.c.entity_id == ext_entity_sq.c.id,
)
.outerjoin(
ext_entity_class_dimension_count_sq,
ext_entity_class_dimension_count_sq.c.entity_class_id == ext_entity_sq.c.class_id,
)
.outerjoin(db_map.entity_element_sq, ext_entity_sq.c.id == db_map.entity_element_sq.c.entity_id)
.group_by(ext_entity_sq.c.id)
.having(
.filter(
or_(
ext_entity_class_dimension_count_sq.c.dimension_count == None,
ext_entity_class_dimension_count_sq.c.dimension_count
== func.count(db_map.entity_element_sq.c.element_id),
and_(
ext_entity_element_count_sq.c.element_count == None,
ext_entity_class_dimension_count_sq.c.dimension_count == None,
),
ext_entity_element_count_sq.c.element_count == ext_entity_class_dimension_count_sq.c.dimension_count,
)
)
.subquery()
Expand Down
3 changes: 3 additions & 0 deletions spinedb_api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def __init__(self, bind, *entities):
self._select = select(entities)
self._from = None

def __str__(self):
return str(self._select)

@property
def column_descriptions(self):
return [{"name": c.name} for c in self._select.columns]
Expand Down

0 comments on commit 6cf762c

Please sign in to comment.