]> source.dussan.org Git - nextcloud-server.git/commitdiff
tell mysql to ignore the sort index for search queries 32074/head
authorRobin Appelman <robin@icewind.nl>
Fri, 22 Apr 2022 11:31:34 +0000 (13:31 +0200)
committerRobin Appelman <robin@icewind.nl>
Fri, 22 Apr 2022 11:42:33 +0000 (13:42 +0200)
Signed-off-by: Robin Appelman <robin@icewind.nl>
lib/private/DB/QueryBuilder/QueryBuilder.php
lib/private/Files/Cache/SearchBuilder.php
lib/public/DB/QueryBuilder/IQueryBuilder.php

index 4ecfd77349299ad0be75c3eaedb414d619ed05a3..fc436383b04f80bc32ae3f6ad58ca67348a90094 100644 (file)
@@ -1113,7 +1113,7 @@ class QueryBuilder implements IQueryBuilder {
        /**
         * Adds an ordering to the query results.
         *
-        * @param string $sort The ordering expression.
+        * @param string|ILiteral|IParameter|IQueryFunction $sort The ordering expression.
         * @param string $order The ordering direction.
         *
         * @return $this This QueryBuilder instance.
index 003d3ac15e7762c41daceb847f0cefe1559f1fd3..c8c442bcb8cd8c312dee953e8da137a61b127a1b 100644 (file)
@@ -232,6 +232,17 @@ class SearchBuilder {
                        if ($field === 'fileid') {
                                $field = 'file.fileid';
                        }
+
+                       // Mysql really likes to pick an index for sorting if it can't fully satisfy the where
+                       // filter with an index, since search queries pretty much never are fully filtered by index
+                       // mysql often picks an index for sorting instead of the much more useful index for filtering.
+                       //
+                       // By changing the order by to an expression, mysql isn't smart enough to see that it could still
+                       // use the index, so it instead picks an index for the filtering
+                       if ($field === 'mtime') {
+                               $field = $query->func()->add($field, $query->createNamedParameter(0));
+                       }
+
                        $query->addOrderBy($field, $order->getDirection());
                }
        }
index afca9e372ee51203e13ea54a9d552c0a15364f09..e3257e82bca9e80ff0d01d8bbe5160cde639ef61 100644 (file)
@@ -834,7 +834,7 @@ interface IQueryBuilder {
        /**
         * Adds an ordering to the query results.
         *
-        * @param string $sort The ordering expression.
+        * @param string|ILiteral|IParameter|IQueryFunction $sort The ordering expression.
         * @param string $order The ordering direction.
         *
         * @return $this This QueryBuilder instance.