Skip to content

Commit

Permalink
Possibly expensive SQL Query on determining files to process #144 (#150)
Browse files Browse the repository at this point in the history
Possibly expensive SQL Query on determining files to process #144

Optimize getFileIdsWithoutTags, getFileIdsWithTags
---------

Co-authored-by: Philip Stadermann <[email protected]>
Co-authored-by: Lennart Dohmann <[email protected]>
(cherry picked from commit e266783)
  • Loading branch information
pstadermann authored and lennartdohmann committed Oct 22, 2024
1 parent 87cdbfc commit 4e8b672
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/Db/DbFileMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
use OCP\IDBConnection;

class DbFileMapper extends QBMapper {
private string $stringType;

public function __construct(IDBConnection $db) {
parent::__construct($db, 'filecache');
$this->stringType = $this->getStringTypeDeclarationSQL();
}

/**
Expand All @@ -26,11 +29,15 @@ public function getFileIdsWithoutTags(array $excludedTagIds, int $limit, int $of

$qb->select('f.fileid')
->from($this->getTableName(), 'f')
->leftJoin('f', 'systemtag_object_mapping', 'o', $qb->expr()->eq('f.fileid', $qb->createFunction($this->getPlatformSpecificCast())))
->leftJoin('f', 'systemtag_object_mapping', 'o', $qb->expr()->eq('o.objectid', $qb->createFunction(sprintf('CAST(f.fileid AS %s)', $this->stringType))))
->leftJoin('f', 'mimetypes', 'm', $qb->expr()->eq('f.mimetype', 'm.id'))
->where($qb->expr()->notIn('o.systemtagid', $qb->createNamedParameter($excludedTagIds, IQueryBuilder::PARAM_INT_ARRAY)))
->orWhere($qb->expr()->isNull('o.systemtagid'))
->andWhere($qb->expr()->notLike('m.mimetype', $qb->createNamedParameter('%unix-directory%')))
->andWhere($qb->expr()->orX(
$qb->expr()->eq('o.objecttype', $qb->createNamedParameter('files')),
$qb->expr()->isNull('o.objecttype')
))
->andWhere($qb->expr()->orX(
$qb->expr()->like('f.path', $qb->createNamedParameter('files/%')),
$qb->expr()->like('f.path', $qb->createNamedParameter('__groupfolders/%'))
Expand Down Expand Up @@ -61,10 +68,11 @@ public function getFileIdsWithTags(array $includedTagIds, int $limit, int $offse

$qb->select('f.fileid')
->from($this->getTableName(), 'f')
->leftJoin('f', 'systemtag_object_mapping', 'o', $qb->expr()->eq('f.fileid', $qb->createFunction($this->getPlatformSpecificCast())))
->leftJoin('f', 'systemtag_object_mapping', 'o', $qb->expr()->eq('o.objectid', $qb->createFunction(sprintf('CAST(f.fileid AS %s)', $this->stringType))))
->leftJoin('f', 'mimetypes', 'm', $qb->expr()->eq('f.mimetype', 'm.id'))
->where($qb->expr()->in('o.systemtagid', $qb->createNamedParameter($includedTagIds, IQueryBuilder::PARAM_INT_ARRAY)))
->andWhere($qb->expr()->notLike('m.mimetype', $qb->createNamedParameter('%unix-directory%')))
->andWhere($qb->expr()->eq('o.objecttype', $qb->createNamedParameter('files')))
->andWhere($qb->expr()->orX(
$qb->expr()->like('f.path', $qb->createNamedParameter('files/%')),
$qb->expr()->like('f.path', $qb->createNamedParameter('__groupfolders/%'))
Expand All @@ -82,22 +90,18 @@ public function getFileIdsWithTags(array $includedTagIds, int $limit, int $offse
}

/**
* Create a platform-specific cast function
* @return string the database platform-specific cast function
* Get the DB type for a string
* @return string the database string type
* @throws Exception if the database platform is not supported
*/
private function getPlatformSpecificCast(): string {
private function getStringTypeDeclarationSQL(): string {
$platform = $this->db->getDatabasePlatform()->getName();
if ($platform === 'mysql') {
$cast = 'CAST(' . 'o.objectid' . ' AS UNSIGNED)';
} elseif ($platform === 'sqlite') {
$cast = 'CAST(' . 'o.objectid' . ' AS INTEGER)';
} elseif ($platform === 'postgresql') {
$cast = 'CAST(' . 'o.objectid' . ' AS BIGINT)';
if ($platform === 'mysql' || $platform === 'sqlite' || $platform === 'postgres') {
$stringType = 'VARCHAR(64)';
} else {
throw new Exception('Unsupported database platform: ' . $platform);
}
return $cast;
return $stringType;
}

/**
Expand Down

0 comments on commit 4e8b672

Please sign in to comment.