Skip to content

Commit b6ad124

Browse files
committed
tell mysql to ignore the sort index for search queries
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 9a76f06 commit b6ad124

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

lib/private/DB/QueryBuilder/QueryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ public function orderBy($sort, $order = null) {
11131113
/**
11141114
* Adds an ordering to the query results.
11151115
*
1116-
* @param string $sort The ordering expression.
1116+
* @param string|ILiteral|IParameter|IQueryFunction $sort The ordering expression.
11171117
* @param string $order The ordering direction.
11181118
*
11191119
* @return $this This QueryBuilder instance.

lib/private/Files/Cache/SearchBuilder.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,17 @@ public function addSearchOrdersToQuery(IQueryBuilder $query, array $orders) {
232232
if ($field === 'fileid') {
233233
$field = 'file.fileid';
234234
}
235+
236+
// Mysql really likes to pick an index for sorting if it can't fully satisfy the where
237+
// filter with an index, since search queries pretty much never are fully filtered by index
238+
// mysql often picks an index for sorting instead of the much more useful index for filtering.
239+
//
240+
// By changing the order by to an expression, mysql isn't smart enough to see that it could still
241+
// use the index, so it instead picks an index for the filtering
242+
if ($field === 'mtime') {
243+
$field = $query->func()->add($field, $query->createNamedParameter(0));
244+
}
245+
235246
$query->addOrderBy($field, $order->getDirection());
236247
}
237248
}

0 commit comments

Comments
 (0)