Skip to content

Commit

Permalink
Merge pull request #32125 from nextcloud/backport/32074/stable22
Browse files Browse the repository at this point in the history
[stable22] tell mysql to ignore the sort index for search queries
  • Loading branch information
blizzz authored May 16, 2022
2 parents 8c61f19 + 0edce61 commit 758e375
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/private/DB/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ public function orderBy($sort, $order = null) {
/**
* 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.
Expand Down
11 changes: 11 additions & 0 deletions lib/private/Files/Cache/SearchBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ public function addSearchOrdersToQuery(IQueryBuilder $query, array $orders) {
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());
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/public/DB/QueryBuilder/IQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ public function orderBy($sort, $order = null);
/**
* 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.
Expand Down

0 comments on commit 758e375

Please sign in to comment.