From 4ea546067dcdd5a3e67adc56a02645a2724636c8 Mon Sep 17 00:00:00 2001
From: Erik Johnston <erik@matrix.org>
Date: Mon, 16 May 2022 16:30:35 +0100
Subject: [PATCH] Fix query performance for /sync (#12745)

---
 changelog.d/12745.bugfix                 | 1 +
 synapse/storage/databases/main/stream.py | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
 create mode 100644 changelog.d/12745.bugfix

diff --git a/changelog.d/12745.bugfix b/changelog.d/12745.bugfix
new file mode 100644
index 0000000000..d1dc8d03aa
--- /dev/null
+++ b/changelog.d/12745.bugfix
@@ -0,0 +1 @@
+Fix DB performance regression introduced in v1.59.0rc2.
diff --git a/synapse/storage/databases/main/stream.py b/synapse/storage/databases/main/stream.py
index 3c3137fe64..0373af86c8 100644
--- a/synapse/storage/databases/main/stream.py
+++ b/synapse/storage/databases/main/stream.py
@@ -750,7 +750,7 @@ class StreamWorkerStore(EventsWorkerStore, SQLBaseStore):
                 WHERE room_id = ?
                     AND stream_ordering <= ?
                     AND NOT outlier
-                    AND rejections.reason IS NULL
+                    AND rejections.event_id IS NULL
                 ORDER BY stream_ordering DESC
                 LIMIT 1
             """
-- 
GitLab