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

Improve performance of user directory search #15729

Merged
merged 2 commits into from
Jun 6, 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/15729.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve performance of user directory search.
12 changes: 8 additions & 4 deletions synapse/storage/databases/main/user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,12 +1061,15 @@ async def search_user_dir(
# The array of numbers are the weights for the various part of the
# search: (domain, _, display name, localpart)
sql = """
WITH matching_users AS (
SELECT user_id, vector FROM user_directory_search WHERE vector @@ to_tsquery('simple', ?)
LIMIT 10000
)
SELECT d.user_id AS user_id, display_name, avatar_url
FROM user_directory_search as t
FROM matching_users as t
INNER JOIN user_directory AS d USING (user_id)
WHERE
%(where_clause)s
AND vector @@ to_tsquery('simple', ?)
ORDER BY
(CASE WHEN d.user_id IS NOT NULL THEN 4.0 ELSE 1.0 END)
* (CASE WHEN display_name IS NOT NULL THEN 1.2 ELSE 1.0 END)
Expand Down Expand Up @@ -1095,8 +1098,9 @@ async def search_user_dir(
"order_case_statements": " ".join(additional_ordering_statements),
}
args = (
join_args
+ (full_query, exact_query, prefix_query)
(full_query,)
+ join_args
+ (exact_query, prefix_query)
+ ordering_arguments
+ (limit + 1,)
)
Expand Down