Skip to content

Commit

Permalink
perf(files): faster query to fetch incomplete directories
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
  • Loading branch information
Altahrim committed Feb 12, 2025
1 parent f2b25a4 commit 465a656
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
19 changes: 5 additions & 14 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ public function getIncompleteChildrenCount($fileId) {
->from('filecache')
->whereParent($fileId)
->whereStorageId($this->getNumericStorageId())
->andWhere($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
->andWhere($query->expr()->eq('size', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT)));

$result = $query->executeQuery();
$size = (int)$result->fetchOne();
Expand Down Expand Up @@ -1067,28 +1067,19 @@ public function getAll() {
* @return string|false the path of the folder or false when no folder matched
*/
public function getIncomplete() {
// we select the fileid here first instead of directly selecting the path since this helps mariadb/mysql
// to use the correct index.
// The overhead of this should be minimal since the cost of selecting the path by id should be much lower
// than the cost of finding an item with size < 0
$query = $this->getQueryBuilder();
$query->select('fileid')
$query->select('path')
->from('filecache')
->whereStorageId($this->getNumericStorageId())
->andWhere($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('size', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT)))
->orderBy('fileid', 'DESC')
->setMaxResults(1);

$result = $query->executeQuery();
$id = $result->fetchOne();
$path = $result->fetchOne();
$result->closeCursor();

if ($id === false) {
return false;
}

$path = $this->getPathById($id);
return $path ?? false;
return $path ? (string)$path : false;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/ObjectStore/ObjectStoreScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function getIncomplete() {
$query->select('path')
->from('filecache')
->where($query->expr()->eq('storage', $query->createNamedParameter($this->cache->getNumericStorageId(), IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('size', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT)))
->orderBy('path', 'DESC')
->setMaxResults(1);

Expand Down

0 comments on commit 465a656

Please sign in to comment.