Improve performance of fetching event reports when there are many of them
parent
2c6a7dfcbf
commit
1d1e8a5634
|
@ -1600,25 +1600,37 @@ 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
|
||||||
|
er.id,
|
||||||
|
er.received_ts,
|
||||||
|
er.room_id,
|
||||||
|
er.event_id,
|
||||||
|
er.user_id,
|
||||||
|
er.content,
|
||||||
|
room_stats_state.canonical_alias,
|
||||||
|
room_stats_state.name
|
||||||
|
FROM event_reports AS er
|
||||||
|
JOIN room_stats_state
|
||||||
|
ON room_stats_state.room_id = er.room_id
|
||||||
|
{where_clause}
|
||||||
|
ORDER BY er.received_ts {order}
|
||||||
|
LIMIT ?
|
||||||
|
OFFSET ?
|
||||||
|
)
|
||||||
|
-- only join on `events` after the LIMIT/OFFSET has been applied
|
||||||
SELECT
|
SELECT
|
||||||
er.id,
|
cer.id,
|
||||||
er.received_ts,
|
cer.received_ts,
|
||||||
er.room_id,
|
cer.room_id,
|
||||||
er.event_id,
|
cer.event_id,
|
||||||
er.user_id,
|
cer.user_id,
|
||||||
er.content,
|
cer.content,
|
||||||
events.sender,
|
events.sender,
|
||||||
room_stats_state.canonical_alias,
|
cer.canonical_alias,
|
||||||
room_stats_state.name
|
cer.name
|
||||||
FROM event_reports AS er
|
FROM considered_event_reports AS cer
|
||||||
LEFT JOIN events
|
LEFT JOIN events ON events.event_id = cer.event_id
|
||||||
ON events.event_id = er.event_id
|
|
||||||
JOIN room_stats_state
|
|
||||||
ON room_stats_state.room_id = er.room_id
|
|
||||||
{where_clause}
|
|
||||||
ORDER BY er.received_ts {order}
|
|
||||||
LIMIT ?
|
|
||||||
OFFSET ?
|
|
||||||
""".format(
|
""".format(
|
||||||
where_clause=where_clause,
|
where_clause=where_clause,
|
||||||
order=order,
|
order=order,
|
||||||
|
|
Loading…
Reference in New Issue