summary refs log tree commit diff
path: root/synapse/storage
diff options
context:
space:
mode:
authorShay <hillerys@element.io>2023-06-06 13:16:03 -0700
committerGitHub <noreply@github.com>2023-06-06 21:16:03 +0100
commit6ee96e936646d6ccc55dc076f62f8cf518c90d1e (patch)
tree6d62ec31cc199742ba4ebad5ac99eea5a030386d /synapse/storage
parentPrevent "twisted trunk" and "latest deps" workflows from running on forks (#1... (diff)
downloadsynapse-6ee96e936646d6ccc55dc076f62f8cf518c90d1e.tar.xz
Improve performance of user directory search (#15729)
Diffstat (limited to 'synapse/storage')
-rw-r--r--synapse/storage/databases/main/user_directory.py12
1 files changed, 8 insertions, 4 deletions
diff --git a/synapse/storage/databases/main/user_directory.py b/synapse/storage/databases/main/user_directory.py
index a0319575f0..b0a06baf4f 100644
--- a/synapse/storage/databases/main/user_directory.py
+++ b/synapse/storage/databases/main/user_directory.py
@@ -1061,12 +1061,15 @@ class UserDirectoryStore(UserDirectoryBackgroundUpdateStore):
             # 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)
@@ -1095,8 +1098,9 @@ class UserDirectoryStore(UserDirectoryBackgroundUpdateStore):
                 "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,)
             )