Skip to content

Commit c9fb4ed

Browse files
committed
refactor(be): remove coalesce when ordering events list
A coalesce when ordering the list of events was causing MySQL to ignore the timestamp index, resulting in a slower query execution time. Removed the coalesce in favor of jordering by just the timestamp field
1 parent deaf5ec commit c9fb4ed

2 files changed

Lines changed: 3 additions & 7 deletions

File tree

common/entities/event.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ class EventEntity(BaseEntity):
4444
instance_set = ForeignKeyField(InstanceSet, null=True, backref="events", on_delete="SET NULL")
4545
v2_payload = JSONField(null=False)
4646

47-
@classmethod
48-
def timestamp_coalesce(cls) -> Node:
49-
return fn.COALESCE(cls.timestamp, cls.created_timestamp)
50-
5147
@property
5248
def components(self) -> list[Component]:
5349
return [self.component] if self.component else []

common/entity_services/event_service.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ def get_events_with_rules(*, rules: ListRules, filters: ProjectEventFilters) ->
5454
filter_list.append(EventEntity.task << filters.task_ids)
5555
if filters.date_range_start:
5656
filter_list.append(
57-
EventEntity.timestamp_coalesce() >= EventEntity.timestamp.db_value(filters.date_range_start)
57+
EventEntity.timestamp >= EventEntity.timestamp.db_value(filters.date_range_start)
5858
)
5959
if filters.date_range_end:
6060
filter_list.append(
61-
EventEntity.timestamp_coalesce() < EventEntity.timestamp.db_value(filters.date_range_end)
61+
EventEntity.timestamp < EventEntity.timestamp.db_value(filters.date_range_end)
6262
)
6363

6464
query = query.where(*filter_list)
65-
page = Page[EventEntity].get_paginated_results(query, EventEntity.timestamp_coalesce(), rules)
65+
page = Page[EventEntity].get_paginated_results(query, EventEntity.timestamp, rules)
6666

6767
# Using a single query to fetch the Instance and Journey data
6868
instance_set_ids = {e.instance_set_id for e in page.results}

0 commit comments

Comments
 (0)