summary refs log tree commit diff
diff options
context:
space:
mode:
authorMark Haines <mark.haines@matrix.org>2016-07-04 15:48:25 +0100
committerMark Haines <mark.haines@matrix.org>2016-07-04 15:48:25 +0100
commitf18d7546c63ae30c4058d1ec6ab2d5c3b001d257 (patch)
tree5524cf3fe41019c171e12f94d23a8385b1946a03
parentcode_style.rst: add link to sphinx examples (diff)
downloadsynapse-f18d7546c63ae30c4058d1ec6ab2d5c3b001d257.tar.xz
Use a query that postgresql optimises better for get_events_around
-rw-r--r--synapse/storage/stream.py28
1 files changed, 16 insertions, 12 deletions
diff --git a/synapse/storage/stream.py b/synapse/storage/stream.py
index b9ad965fd6..4dd11284e5 100644
--- a/synapse/storage/stream.py
+++ b/synapse/storage/stream.py
@@ -591,25 +591,28 @@ class StreamStore(SQLBaseStore):
 
         query_before = (
             "SELECT topological_ordering, stream_ordering, event_id FROM events"
-            " WHERE room_id = ? AND (topological_ordering < ?"
-            " OR (topological_ordering = ? AND stream_ordering < ?))"
-            " ORDER BY topological_ordering DESC, stream_ordering DESC"
-            " LIMIT ?"
+            " WHERE room_id = ? AND topological_ordering < ?"
+            " UNION ALL "
+            " SELECT topological_ordering, stream_ordering, event_id FROM events"
+            " WHERE room_id = ? AND topological_ordering = ? AND stream_ordering < ?"
+            " ORDER BY topological_ordering DESC, stream_ordering DESC LIMIT ?"
         )
 
         query_after = (
             "SELECT topological_ordering, stream_ordering, event_id FROM events"
-            " WHERE room_id = ? AND (topological_ordering > ?"
-            " OR (topological_ordering = ? AND stream_ordering > ?))"
-            " ORDER BY topological_ordering ASC, stream_ordering ASC"
-            " LIMIT ?"
+            " WHERE room_id = ? AND topological_ordering > ?"
+            " UNION ALL"
+            " SELECT topological_ordering, stream_ordering, event_id FROM events"
+            " WHERE room_id = ? AND topological_ordering = ? AND stream_ordering > ?"
+            " ORDER BY topological_ordering ASC, stream_ordering ASC LIMIT ?"
         )
 
         txn.execute(
             query_before,
             (
-                room_id, topological_ordering, topological_ordering,
-                stream_ordering, before_limit,
+                room_id, topological_ordering,
+                room_id, topological_ordering, stream_ordering,
+                before_limit,
             )
         )
 
@@ -630,8 +633,9 @@ class StreamStore(SQLBaseStore):
         txn.execute(
             query_after,
             (
-                room_id, topological_ordering, topological_ordering,
-                stream_ordering, after_limit,
+                room_id, topological_ordering,
+                room_id, topological_ordering, stream_ordering,
+                after_limit,
             )
         )