From 1d1e8a5634281c4bc931527575ebf0679d6df880 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Fri, 10 Nov 2023 14:01:08 +0000 Subject: [PATCH 1/2] Improve performance of fetching event reports when there are many of them --- synapse/storage/databases/main/room.py | 46 ++++++++++++++++---------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/synapse/storage/databases/main/room.py b/synapse/storage/databases/main/room.py index ef26d5d9d37e..851ed83caacf 100644 --- a/synapse/storage/databases/main/room.py +++ b/synapse/storage/databases/main/room.py @@ -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, From 660807bb6ef5800f9668f7092276733ddf5249af Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Fri, 10 Nov 2023 14:04:31 +0000 Subject: [PATCH 2/2] Newsfile Signed-off-by: Olivier Wilkinson (reivilibre) --- changelog.d/16620.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/16620.misc diff --git a/changelog.d/16620.misc b/changelog.d/16620.misc new file mode 100644 index 000000000000..e6388f3a1848 --- /dev/null +++ b/changelog.d/16620.misc @@ -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. \ No newline at end of file