Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5f7c908

Browse files
authoredMar 24, 2023
As an optimisation, use TRUNCATE on Postgres when clearing the user directory tables. (#15316)
1 parent 5b70f24 commit 5f7c908

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed
 

‎changelog.d/15316.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
As an optimisation, use `TRUNCATE` on Postgres when clearing the user directory tables.

‎synapse/storage/databases/main/user_directory.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,17 @@ async def delete_all_from_user_dir(self) -> None:
698698
"""Delete the entire user directory"""
699699

700700
def _delete_all_from_user_dir_txn(txn: LoggingTransaction) -> None:
701-
txn.execute("DELETE FROM user_directory")
702-
txn.execute("DELETE FROM user_directory_search")
703-
txn.execute("DELETE FROM users_in_public_rooms")
704-
txn.execute("DELETE FROM users_who_share_private_rooms")
701+
# SQLite doesn't support TRUNCATE.
702+
# On Postgres, DELETE FROM does a table scan but TRUNCATE is more efficient.
703+
truncate = (
704+
"DELETE FROM"
705+
if isinstance(self.database_engine, Sqlite3Engine)
706+
else "TRUNCATE"
707+
)
708+
txn.execute(f"{truncate} user_directory")
709+
txn.execute(f"{truncate} user_directory_search")
710+
txn.execute(f"{truncate} users_in_public_rooms")
711+
txn.execute(f"{truncate} users_who_share_private_rooms")
705712
txn.call_after(self.get_user_in_directory.invalidate_all)
706713

707714
await self.db_pool.runInteraction(

0 commit comments

Comments
 (0)
This repository has been archived.