Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

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. #16620

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/16620.misc
Original file line number Diff line number Diff line change
@@ -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.
46 changes: 29 additions & 17 deletions synapse/storage/databases/main/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -1600,25 +1600,37 @@ def _get_event_reports_paginate_txn(
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,
Expand Down
Loading