Browse Source

Fix DELETE queries for MariaDB and MySQL(?)

MariaDB refuses to use index in queries like DELETE FROM WHERE IN (SELECT ...).
Visman 1 year ago
parent
commit
40f9b55dfc
2 changed files with 76 additions and 29 deletions
  1. 18 7
      app/Models/Post/Delete.php
  2. 58 22
      app/Models/Search/Delete.php

+ 18 - 7
app/Models/Post/Delete.php

@@ -161,13 +161,24 @@ class Delete extends Action
 
             $uidsUpdate = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
 
-            $query = 'DELETE
-                FROM ::posts
-                WHERE topic_id IN (
-                    SELECT id
-                    FROM ::topics
-                    WHERE forum_id IN (?ai:forums)
-                )';
+            switch ($this->c->DB->getType()) {
+                case 'mysql':
+                    $query = 'DELETE p
+                        FROM ::posts AS p, ::topics AS t
+                        WHERE t.forum_id IN (?ai:forums) AND p.topic_id=t.id';
+
+                    break;
+                default:
+                    $query = 'DELETE
+                        FROM ::posts
+                        WHERE topic_id IN (
+                            SELECT id
+                            FROM ::topics
+                            WHERE forum_id IN (?ai:forums)
+                        )';
+
+                break;
+            }
 
             $this->c->DB->exec($query, $vars);
         }

+ 58 - 22
app/Models/Search/Delete.php

@@ -87,40 +87,76 @@ class Delete extends Method
             $vars = [
                 ':users' => $uids,
             ];
-            $query = 'DELETE
-                FROM ::search_matches
-                WHERE post_id IN (
-                    SELECT p.id
-                    FROM ::posts AS p
-                    WHERE p.poster_id IN (?ai:users)
-                )';
+
+            switch ($this->c->DB->getType()) {
+                case 'mysql':
+                    $query = 'DELETE sm
+                        FROM ::search_matches AS sm, ::posts AS p
+                        WHERE p.poster_id IN (?ai:users) AND sm.post_id=p.id';
+
+                    break;
+                default:
+                    $query = 'DELETE
+                        FROM ::search_matches
+                        WHERE post_id IN (
+                            SELECT p.id
+                            FROM ::posts AS p
+                            WHERE p.poster_id IN (?ai:users)
+                        )';
+
+                    break;
+            }
         }
 
         if ($forums) {
             $vars = [
                 ':forums' => \array_keys($forums),
             ];
-            $query = 'DELETE
-                FROM ::search_matches
-                WHERE post_id IN (
-                    SELECT p.id
-                    FROM ::posts AS p
-                    INNER JOIN ::topics AS t ON t.id=p.topic_id
-                    WHERE t.forum_id IN (?ai:forums)
-                )';
+
+            switch ($this->c->DB->getType()) {
+                case 'mysql':
+                    $query = 'DELETE sm
+                        FROM ::search_matches AS sm, ::posts AS p, ::topics AS t
+                        WHERE t.forum_id IN (?ai:forums) AND p.topic_id=t.id AND sm.post_id=p.id';
+
+                    break;
+                default:
+                    $query = 'DELETE
+                        FROM ::search_matches
+                        WHERE post_id IN (
+                            SELECT p.id
+                            FROM ::posts AS p
+                            INNER JOIN ::topics AS t ON t.id=p.topic_id
+                            WHERE t.forum_id IN (?ai:forums)
+                        )';
+
+                    break;
+            }
         }
 
         if ($topics) {
             $vars = [
                 ':topics' => \array_keys($topics),
             ];
-            $query = 'DELETE
-                FROM ::search_matches
-                WHERE post_id IN (
-                    SELECT p.id
-                    FROM ::posts AS p
-                    WHERE p.topic_id IN (?ai:topics)
-                )';
+
+            switch ($this->c->DB->getType()) {
+                case 'mysql':
+                    $query = 'DELETE sm
+                        FROM ::search_matches AS sm, ::posts AS p
+                        WHERE p.topic_id IN (?ai:topics) AND sm.post_id=p.id';
+
+                    break;
+                default:
+                    $query = 'DELETE
+                        FROM ::search_matches
+                        WHERE post_id IN (
+                            SELECT p.id
+                            FROM ::posts AS p
+                            WHERE p.topic_id IN (?ai:topics)
+                        )';
+
+                    break;
+            }
         }
 
         if ($posts) {