diff options
author | Robin Appelman <robin@icewind.nl> | 2022-04-25 11:23:51 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-25 11:23:51 +0000 |
commit | f7413b9afc2a29bef64f2cc207b318308cb6ebef (patch) | |
tree | c900a611c7abfc49225bd84190af812c0adef8d5 /lib | |
parent | ff385dc679b7c0819e9903c5488e9a154ef7dec4 (diff) | |
parent | a4e120c2034bc1739d7428245660670b09aa7a75 (diff) | |
download | nextcloud-server-f7413b9afc2a29bef64f2cc207b318308cb6ebef.tar.gz nextcloud-server-f7413b9afc2a29bef64f2cc207b318308cb6ebef.zip |
Merge pull request #32074 from nextcloud/mysql-search-ignore-index-2
tell mysql to ignore the sort index for search queries
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/DB/QueryBuilder/QueryBuilder.php | 2 | ||||
-rw-r--r-- | lib/private/Files/Cache/SearchBuilder.php | 11 | ||||
-rw-r--r-- | lib/public/DB/QueryBuilder/IQueryBuilder.php | 2 |
3 files changed, 13 insertions, 2 deletions
diff --git a/lib/private/DB/QueryBuilder/QueryBuilder.php b/lib/private/DB/QueryBuilder/QueryBuilder.php index 4ecfd773492..fc436383b04 100644 --- a/lib/private/DB/QueryBuilder/QueryBuilder.php +++ b/lib/private/DB/QueryBuilder/QueryBuilder.php @@ -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. diff --git a/lib/private/Files/Cache/SearchBuilder.php b/lib/private/Files/Cache/SearchBuilder.php index 003d3ac15e7..c8c442bcb8c 100644 --- a/lib/private/Files/Cache/SearchBuilder.php +++ b/lib/private/Files/Cache/SearchBuilder.php @@ -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()); } } diff --git a/lib/public/DB/QueryBuilder/IQueryBuilder.php b/lib/public/DB/QueryBuilder/IQueryBuilder.php index afca9e372ee..e3257e82bca 100644 --- a/lib/public/DB/QueryBuilder/IQueryBuilder.php +++ b/lib/public/DB/QueryBuilder/IQueryBuilder.php @@ -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. |