Skip to content

Commit

Permalink
cast as unsigned converts to a bigint
Browse files Browse the repository at this point in the history
https://www.w3schools.com/sql/func_mysql_cast.asp

The old query took more than 15 minutes on a database with 100.000 files. The new takes 0.137 sec
  • Loading branch information
ata-no-one committed Nov 12, 2024
1 parent 68f0de8 commit 035944e
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Db/DbFileMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ 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('o.objectid', $qb->createFunction(sprintf('CAST(f.fileid AS %s)', $this->stringType))))
->leftJoin('f', 'systemtag_object_mapping', 'o', $qb->expr()->eq($qb->createFunction('CAST(o.objectid as unsigned)') , 'f.fileid'))
->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'))
Expand Down Expand Up @@ -68,7 +68,7 @@ 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('o.objectid', $qb->createFunction(sprintf('CAST(f.fileid AS %s)', $this->stringType))))
->leftJoin('f', 'systemtag_object_mapping', 'o', $qb->expr()->eq($qb->createFunction('CAST(o.objectid as unsigned)') , 'f.fileid'))
->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%')))
Expand Down

0 comments on commit 035944e

Please sign in to comment.