From 15a11ad4f80a01f8e1893e94e81dc2f12494612c Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 25 Aug 2022 15:41:26 -0400 Subject: [PATCH 1/3] Use execute_values instead of execute_batch when inserting into the event_push_actions_staging table. --- synapse/storage/databases/main/event_push_actions.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/synapse/storage/databases/main/event_push_actions.py b/synapse/storage/databases/main/event_push_actions.py index 8dfa545c2771..6e3619450966 100644 --- a/synapse/storage/databases/main/event_push_actions.py +++ b/synapse/storage/databases/main/event_push_actions.py @@ -707,15 +707,16 @@ def _add_push_actions_to_staging_txn(txn: LoggingTransaction) -> None: sql = """ INSERT INTO event_push_actions_staging (event_id, user_id, actions, notif, highlight, unread) - VALUES (?, ?, ?, ?, ?, ?) + VALUES ? """ - txn.execute_batch( + txn.execute_values( sql, ( _gen_entry(user_id, actions) for user_id, actions in user_id_actions.items() ), + fetch=False, ) return await self.db_pool.runInteraction( From ac69e5c3a65b4c9d54d916e29c70414d10536918 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 25 Aug 2022 15:46:22 -0400 Subject: [PATCH 2/3] Use the simpler_insert_many helper. --- .../databases/main/event_push_actions.py | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/synapse/storage/databases/main/event_push_actions.py b/synapse/storage/databases/main/event_push_actions.py index 6e3619450966..9f410d69def5 100644 --- a/synapse/storage/databases/main/event_push_actions.py +++ b/synapse/storage/databases/main/event_push_actions.py @@ -700,27 +700,14 @@ def _gen_entry( int(count_as_unread), # unread column ) - def _add_push_actions_to_staging_txn(txn: LoggingTransaction) -> None: - # We don't use simple_insert_many here to avoid the overhead - # of generating lists of dicts. - - sql = """ - INSERT INTO event_push_actions_staging - (event_id, user_id, actions, notif, highlight, unread) - VALUES ? - """ - - txn.execute_values( - sql, - ( - _gen_entry(user_id, actions) - for user_id, actions in user_id_actions.items() - ), - fetch=False, - ) - - return await self.db_pool.runInteraction( - "add_push_actions_to_staging", _add_push_actions_to_staging_txn + await self.db_pool.simple_insert_many( + "event_push_actions_staging", + keys=("event_id", "user_id", "actions", "notif", "highlight", "unread"), + values=[ + _gen_entry(user_id, actions) + for user_id, actions in user_id_actions.items() + ], + desc="add_push_actions_to_staging", ) async def remove_push_actions_from_staging(self, event_id: str) -> None: From 691bfa30d16211a2ddb3260378c2aff142777421 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 25 Aug 2022 15:47:29 -0400 Subject: [PATCH 3/3] Newsfragment --- changelog.d/13634.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/13634.feature diff --git a/changelog.d/13634.feature b/changelog.d/13634.feature new file mode 100644 index 000000000000..0a8827205d7b --- /dev/null +++ b/changelog.d/13634.feature @@ -0,0 +1 @@ +Improve performance of sending messages in rooms with thousands of local users.