Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable22] tell mysql to ignore the sort index for search queries #32125

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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