Compare commits

...

2 Commits

Author SHA1 Message Date
Olivier Wilkinson (reivilibre)
660807bb6e Newsfile
Signed-off-by: Olivier Wilkinson (reivilibre) <oliverw@matrix.org>
2023-11-10 14:04:31 +00:00
Olivier Wilkinson (reivilibre)
1d1e8a5634 Improve performance of fetching event reports when there are many of them 2023-11-10 14:01:08 +00:00
2 changed files with 30 additions and 17 deletions

1
changelog.d/16620.misc Normal file
View File

@@ -0,0 +1 @@
Improve the performance of fetching event reports over the admin API when there are many of them and the requester has paginated far down the list.

View File

@@ -1600,25 +1600,37 @@ class RoomWorkerStore(CacheInvalidationWorkerStore):
count = cast(Tuple[int], txn.fetchone())[0]
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
er.id,
er.received_ts,
er.room_id,
er.event_id,
er.user_id,
er.content,
cer.id,
cer.received_ts,
cer.room_id,
cer.event_id,
cer.user_id,
cer.content,
events.sender,
room_stats_state.canonical_alias,
room_stats_state.name
FROM event_reports AS er
LEFT JOIN events
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 ?
cer.canonical_alias,
cer.name
FROM considered_event_reports AS cer
LEFT JOIN events ON events.event_id = cer.event_id
""".format(
where_clause=where_clause,
order=order,