0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-06-01 10:18:54 +02:00

Speed up e2e device keys queries for bot accounts (#16841)

This helps with bot accounts with lots of non-e2e devices.

The change is basically to change the order of the join for the case of
using `INNER JOIN`
This commit is contained in:
Erik Johnston 2024-01-23 11:37:16 +00:00 committed by GitHub
parent 23740eaa3d
commit c925b45567
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 11 deletions

1
changelog.d/16841.misc Normal file
View file

@ -0,0 +1 @@
Speed up e2e device keys queries for bot accounts.

View file

@ -408,17 +408,24 @@ class EndToEndKeyWorkerStore(EndToEndKeyBackgroundStore, CacheInvalidationWorker
def get_e2e_device_keys_txn( def get_e2e_device_keys_txn(
txn: LoggingTransaction, query_clause: str, query_params: list txn: LoggingTransaction, query_clause: str, query_params: list
) -> None: ) -> None:
sql = ( if include_all_devices:
"SELECT user_id, device_id, " sql = f"""
" d.display_name, " SELECT user_id, device_id, d.display_name, k.key_json
" k.key_json" FROM devices d
" FROM devices d" LEFT JOIN e2e_device_keys_json k USING (user_id, device_id)
" %s JOIN e2e_device_keys_json k USING (user_id, device_id)" WHERE {query_clause} AND NOT d.hidden
" WHERE %s AND NOT d.hidden" """
) % ( else:
"LEFT" if include_all_devices else "INNER", # We swap around `e2e_device_keys_json` and `devices`, as we
query_clause, # want Postgres to query `e2e_device_keys_json` first as it will
) # have fewer rows in it. This helps *a lot* with accounts with
# lots of non-e2e devices (such as bots).
sql = f"""
SELECT user_id, device_id, d.display_name, k.key_json
FROM e2e_device_keys_json k
INNER JOIN devices d USING (user_id, device_id)
WHERE {query_clause} AND NOT d.hidden
"""
txn.execute(sql, query_params) txn.execute(sql, query_params)