summary refs log tree commit diff
path: root/synapse/storage/search.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2015-11-13 11:40:17 +0000
committerErik Johnston <erik@matrix.org>2015-11-13 11:40:17 +0000
commitda3dd4867dfb6414a4402089cbe3ef70fb605d23 (patch)
treea80ccd094d9dacafdbe02dc4a6705b0f3d217c2b /synapse/storage/search.py
parentUpdate date (diff)
parentImplementation of state rollback in /sync (diff)
downloadsynapse-da3dd4867dfb6414a4402089cbe3ef70fb605d23.tar.xz
Merge branch 'develop' into release-v0.11.0
Diffstat (limited to 'synapse/storage/search.py')
-rw-r--r--synapse/storage/search.py17
1 files changed, 14 insertions, 3 deletions
diff --git a/synapse/storage/search.py b/synapse/storage/search.py
index 2e88c51ad0..380270b009 100644
--- a/synapse/storage/search.py
+++ b/synapse/storage/search.py
@@ -252,12 +252,23 @@ class SearchStore(BackgroundUpdateStore):
                 " WHERE vector @@ query AND room_id = ?"
             )
         elif isinstance(self.database_engine, Sqlite3Engine):
+            # We use CROSS JOIN here to ensure we use the right indexes.
+            # https://sqlite.org/optoverview.html#crossjoin
+            #
+            # We want to use the full text search index on event_search to
+            # extract all possible matches first, then lookup those matches
+            # in the events table to get the topological ordering. We need
+            # to use the indexes in this order because sqlite refuses to
+            # MATCH unless it uses the full text search index
             sql = (
-                "SELECT rank(matchinfo(event_search)) as rank, room_id, event_id,"
+                "SELECT rank(matchinfo) as rank, room_id, event_id,"
                 " topological_ordering, stream_ordering"
+                " FROM (SELECT key, event_id, matchinfo(event_search) as matchinfo"
                 " FROM event_search"
-                " NATURAL JOIN events"
-                " WHERE value MATCH ? AND room_id = ?"
+                " WHERE value MATCH ?"
+                " )"
+                " CROSS JOIN events USING (event_id)"
+                " WHERE room_id = ?"
             )
         else:
             # This should be unreachable.