Improve performance of fetching event reports when there are many of them

rei/fetch_evt_report_slight_query_optimi
Olivier Wilkinson (reivilibre) 2023-11-10 14:01:08 +00:00
parent 2c6a7dfcbf
commit 1d1e8a5634
1 changed files with 29 additions and 17 deletions

View File

@ -1600,6 +1600,7 @@ class RoomWorkerStore(CacheInvalidationWorkerStore):
count = cast(Tuple[int], txn.fetchone())[0] count = cast(Tuple[int], txn.fetchone())[0]
sql = """ sql = """
WITH considered_event_reports AS (
SELECT SELECT
er.id, er.id,
er.received_ts, er.received_ts,
@ -1607,18 +1608,29 @@ class RoomWorkerStore(CacheInvalidationWorkerStore):
er.event_id, er.event_id,
er.user_id, er.user_id,
er.content, er.content,
events.sender,
room_stats_state.canonical_alias, room_stats_state.canonical_alias,
room_stats_state.name room_stats_state.name
FROM event_reports AS er FROM event_reports AS er
LEFT JOIN events
ON events.event_id = er.event_id
JOIN room_stats_state JOIN room_stats_state
ON room_stats_state.room_id = er.room_id ON room_stats_state.room_id = er.room_id
{where_clause} {where_clause}
ORDER BY er.received_ts {order} ORDER BY er.received_ts {order}
LIMIT ? LIMIT ?
OFFSET ? OFFSET ?
)
-- only join on `events` after the LIMIT/OFFSET has been applied
SELECT
cer.id,
cer.received_ts,
cer.room_id,
cer.event_id,
cer.user_id,
cer.content,
events.sender,
cer.canonical_alias,
cer.name
FROM considered_event_reports AS cer
LEFT JOIN events ON events.event_id = cer.event_id
""".format( """.format(
where_clause=where_clause, where_clause=where_clause,
order=order, order=order,