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

As an optimisation, use TRUNCATE on Postgres when clearing the user directory tables. #15316

Merged
merged 2 commits into from
Mar 24, 2023
Merged
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/15316.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
As an optimisation, use `TRUNCATE` on Postgres when clearing the user directory tables.
15 changes: 11 additions & 4 deletions synapse/storage/databases/main/user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,17 @@ async def delete_all_from_user_dir(self) -> None:
"""Delete the entire user directory"""

def _delete_all_from_user_dir_txn(txn: LoggingTransaction) -> None:
txn.execute("DELETE FROM user_directory")
txn.execute("DELETE FROM user_directory_search")
txn.execute("DELETE FROM users_in_public_rooms")
txn.execute("DELETE FROM users_who_share_private_rooms")
# SQLite doesn't support TRUNCATE.
# On Postgres, DELETE FROM does a table scan but TRUNCATE is more efficient.
truncate = (
"DELETE FROM"
if isinstance(self.database_engine, Sqlite3Engine)
else "TRUNCATE"
)
txn.execute(f"{truncate} user_directory")
txn.execute(f"{truncate} user_directory_search")
txn.execute(f"{truncate} users_in_public_rooms")
txn.execute(f"{truncate} users_who_share_private_rooms")
txn.call_after(self.get_user_in_directory.invalidate_all)

await self.db_pool.runInteraction(
Expand Down