From 2d507bb65ae2118c41fc23f370fe8bc43fff0213 Mon Sep 17 00:00:00 2001
From: Janne Koschinski <janne@kuschku.de>
Date: Tue, 4 Feb 2020 12:01:05 +0100
Subject: [PATCH] Minor change to hopefully improve the performance

---
 database/backends/PostgresSmartBackend.php | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/database/backends/PostgresSmartBackend.php b/database/backends/PostgresSmartBackend.php
index 9d14e74..0fb7db4 100644
--- a/database/backends/PostgresSmartBackend.php
+++ b/database/backends/PostgresSmartBackend.php
@@ -64,6 +64,9 @@ class PostgresSmartBackend implements Backend
         $tsQueryFunction = $this->tsQueryFunction();
         $rankingFunction = $this->rankingFunction();
         return $this->db->prepare("
+            WITH matching_backlog AS (
+              SELECT * FROM backlog WHERE tsv @@ $tsQueryFunction
+            )
             SELECT
               ranked_messages.bufferid,
               ranked_messages.buffername,
@@ -101,7 +104,7 @@ class PostgresSmartBackend implements Backend
                     query,
                     $rankingFunction AS rank_value
                   FROM
-                    backlog
+                    matching_backlog AS backlog
                     JOIN buffer ON backlog.bufferid = buffer.bufferid
                     , $tsQueryFunction query
                   WHERE buffer.userid = :userid
@@ -150,6 +153,9 @@ class PostgresSmartBackend implements Backend
         $tsQueryFunction = $this->tsQueryFunction();
         $rankingFunction = $this->rankingFunction();
         return $this->db->prepare("
+            WITH matching_backlog AS (
+              SELECT * FROM backlog WHERE tsv @@ $tsQueryFunction
+            )
             SELECT
               matching_messages.messageid,
               matching_messages.time,
@@ -168,7 +174,7 @@ class PostgresSmartBackend implements Backend
                  query,
                  $rankingFunction AS rank_value
                FROM
-                 backlog
+                 matching_backlog AS backlog
                  JOIN buffer ON backlog.bufferid = buffer.bufferid
                  , $tsQueryFunction query
                WHERE buffer.userid = :userid
-- 
GitLab