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

tell mysql to ignore the sort index for search queries #29300

Closed
wants to merge 2 commits into from
Closed
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
17 changes: 17 additions & 0 deletions lib/private/DB/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1353,4 +1353,21 @@ public function quoteAlias($alias) {

return $this->helper->quoteColumnName($alias);
}

/**
* Either appends to or replaces a single, generic query part.
*
* The available parts are: 'select', 'from', 'set', 'where',
* 'groupBy', 'having' and 'orderBy'.
*
* @param string $sqlPartName
* @param mixed $sqlPart
* @param bool $append
*
* @return $this This QueryBuilder instance.
*/
public function add(string $sqlPartName, $sqlPart, bool $append = false) {
$this->queryBuilder->add($sqlPartName, $sqlPart, $append);
return $this;
}
}
19 changes: 16 additions & 3 deletions lib/private/Files/Cache/CacheQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
namespace OC\Files\Cache;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use OC\DB\QueryBuilder\QueryBuilder;
use OC\SystemConfig;
use OCP\DB\QueryBuilder\IQueryBuilder;
Expand All @@ -41,12 +42,24 @@ public function __construct(IDBConnection $connection, SystemConfig $systemConfi
parent::__construct($connection, $systemConfig, $logger);
}

public function selectFileCache(string $alias = null) {
public function selectFileCache(string $alias = null, string $mysqlIndexHint = '') {
$name = $alias ? $alias : 'filecache';
$this->select("$name.fileid", 'storage', 'path', 'path_hash', "$name.parent", 'name', 'mimetype', 'mimepart', 'size', 'mtime',
'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum', 'metadata_etag', 'creation_time', 'upload_time')
->from('filecache', $name)
->leftJoin($name, 'filecache_extended', 'fe', $this->expr()->eq("$name.fileid", 'fe.fileid'));
->from('filecache', $name);
if ($mysqlIndexHint !== '' && $this->getConnection()->getDatabasePlatform() instanceof MySQLPlatform) {
$this->add('join', [
$this->quoteAlias($name) => [
// horrible query builder crimes to sneak in raw sql after the "FROM oc_filecache $name"
'joinType' => $mysqlIndexHint . ' left',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would be a clean way ? is this something that could be added upstream ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would have to be added upstream (the design of the query builder doesn't really make it possible to overwrite internal parts of it) but upstream is (understandably) hesitant about adding features that only apply on one specific db backend.

While this is certainly not a nice way to do this, it is the best option I'm aware of and doesn't require waiting for upstream releases.

'joinTable' => $this->getTableName('filecache_extended'),
'joinAlias' => $this->quoteAlias('fe'),
'joinCondition' => $this->expr()->eq("$name.fileid", 'fe.fileid'),
],
], true);
} else {
$this->leftJoin($name, 'filecache_extended', 'fe', $this->expr()->eq("$name.fileid", 'fe.fileid'));
}

$this->alias = $name;

Expand Down
8 changes: 7 additions & 1 deletion lib/private/Files/Cache/QuerySearchHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ public function searchInCaches(ISearchQuery $searchQuery, array $caches): array

$builder = $this->getQueryBuilder();

$query = $builder->selectFileCache('file');
// 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.
//
// To bypass this, we tell mysql explicitly not to use the mtime (the default order field) index,
// so it will instead pick an index that is actually useful.
$query = $builder->selectFileCache('file', 'ignore index for order by (fs_mtime)');

if ($this->searchBuilder->shouldJoinTags($searchQuery->getSearchOperation())) {
$user = $searchQuery->getUser();
Expand Down