浏览代码

Add a little more optimization

Consider the topic author and the author of the last post in the topic so as not to send a database query.
Visman 2 年之前
父节点
当前提交
8df01cbac7
共有 1 个文件被更改,包括 33 次插入13 次删除
  1. 33 13
      app/Models/Topic/View.php

+ 33 - 13
app/Models/Topic/View.php

@@ -50,26 +50,46 @@ class View extends Action
             return $result;
         }
 
+        $uid = $this->c->user->id;
+
         if ('topics_with_your_posts' === $arg->currentAction) {
             $dots = $arg->idsList;
         } else {
-            $vars = [
-                ':uid' => $this->c->user->id,
-                ':ids' => $arg->idsList,
-            ];
-            $query = 'SELECT p.topic_id
-                FROM ::posts AS p
-                WHERE p.poster_id=?i:uid AND p.topic_id IN (?ai:ids)
-                GROUP BY p.topic_id';
+            $ids = [];
+
+            foreach ($arg->idsList as $id) {
+                if (! $result[$id] instanceof Topic) {
+                    continue;
+                }
+
+                if (
+                    $uid === $result[$id]->poster_id
+                    || $uid === $result[$id]->last_poster_id
+                ) {
+                    $result[$id]->__dot = true;
+                } else {
+                    $ids[] = $id;
+                }
+            }
+
+            if ($ids) {
+                $vars = [
+                    ':uid' => $uid,
+                    ':ids' => $ids,
+                ];
+                $query = 'SELECT p.topic_id
+                    FROM ::posts AS p
+                    WHERE p.poster_id=?i:uid AND p.topic_id IN (?ai:ids)
+                    GROUP BY p.topic_id';
 
-            $dots = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
+                $dots = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
+            } else {
+                $dots = [];
+            }
         }
 
         foreach ($dots as $id) {
-            if (
-                isset($result[$id])
-                && $result[$id] instanceof Topic
-            ) {
+            if ($result[$id] instanceof Topic) {
                 $result[$id]->__dot = true;
             }
         }