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

Commit 6ee96e9

Browse files
authored
Improve performance of user directory search (#15729)
1 parent d43c72a commit 6ee96e9

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

changelog.d/15729.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve performance of user directory search.

synapse/storage/databases/main/user_directory.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,12 +1061,15 @@ async def search_user_dir(
10611061
# The array of numbers are the weights for the various part of the
10621062
# search: (domain, _, display name, localpart)
10631063
sql = """
1064+
WITH matching_users AS (
1065+
SELECT user_id, vector FROM user_directory_search WHERE vector @@ to_tsquery('simple', ?)
1066+
LIMIT 10000
1067+
)
10641068
SELECT d.user_id AS user_id, display_name, avatar_url
1065-
FROM user_directory_search as t
1069+
FROM matching_users as t
10661070
INNER JOIN user_directory AS d USING (user_id)
10671071
WHERE
10681072
%(where_clause)s
1069-
AND vector @@ to_tsquery('simple', ?)
10701073
ORDER BY
10711074
(CASE WHEN d.user_id IS NOT NULL THEN 4.0 ELSE 1.0 END)
10721075
* (CASE WHEN display_name IS NOT NULL THEN 1.2 ELSE 1.0 END)
@@ -1095,8 +1098,9 @@ async def search_user_dir(
10951098
"order_case_statements": " ".join(additional_ordering_statements),
10961099
}
10971100
args = (
1098-
join_args
1099-
+ (full_query, exact_query, prefix_query)
1101+
(full_query,)
1102+
+ join_args
1103+
+ (exact_query, prefix_query)
11001104
+ ordering_arguments
11011105
+ (limit + 1,)
11021106
)

0 commit comments

Comments
 (0)