Add database indices for performance

This commit is contained in:
Sebastian Meyer 2024-02-01 12:58:27 +01:00
parent 16dbc1c39a
commit 17bfc163ed
3 changed files with 19 additions and 20 deletions

View File

@ -222,7 +222,7 @@ class Database
->from(Set::class, 'sets', 'sets.spec'); ->from(Set::class, 'sets', 'sets.spec');
$query = $dql->getQuery(); $query = $dql->getQuery();
$query->enableResultCache(); $query->enableResultCache();
/** @var Sets */ /** @var Sets $resultQuery */
$resultQuery = $query->getResult(); $resultQuery = $query->getResult();
return new Result($resultQuery); return new Result($resultQuery);
} }
@ -236,18 +236,13 @@ class Database
{ {
$timestamp = '0000-00-00T00:00:00Z'; $timestamp = '0000-00-00T00:00:00Z';
$dql = $this->entityManager->createQueryBuilder(); $dql = $this->entityManager->createQueryBuilder();
$dql->select('record') $dql->select($dql->expr()->min('record.lastChanged'))
->from(Record::class, 'record') ->from(Record::class, 'record');
->orderBy('record.lastChanged', 'ASC')
->setMaxResults(1);
$query = $dql->getQuery(); $query = $dql->getQuery();
$query->enableResultCache(); $query->enableResultCache();
/** @var ?array<string, \DateTime> */ /** @var ?string $result */
$result = $query->getOneOrNullResult(AbstractQuery::HYDRATE_ARRAY); $result = $query->getOneOrNullResult(AbstractQuery::HYDRATE_SCALAR_COLUMN);
if (isset($result)) { return $result ?? $timestamp;
$timestamp = $result['lastChanged']->format('Y-m-d\TH:i:s\Z');
}
return $timestamp;
} }
/** /**
@ -284,7 +279,7 @@ class Database
} }
$query = $dql->getQuery(); $query = $dql->getQuery();
$query->enableResultCache(); $query->enableResultCache();
/** @var Formats */ /** @var Formats $queryResult */
$queryResult = $query->getResult(); $queryResult = $query->getResult();
return new Result($queryResult); return new Result($queryResult);
} }
@ -364,7 +359,7 @@ class Database
$set = $set->getSpec(); $set = $set->getSpec();
} }
$query = $dql->getQuery(); $query = $dql->getQuery();
/** @var Records */ /** @var Records $queryResult */
$queryResult = $query->getResult(); $queryResult = $query->getResult();
$result = new Result($queryResult); $result = new Result($queryResult);
$paginator = new Paginator($query, true); $paginator = new Paginator($query, true);
@ -429,9 +424,9 @@ class Database
->setMaxResults($maxRecords); ->setMaxResults($maxRecords);
$query = $dql->getQuery(); $query = $dql->getQuery();
$query->enableResultCache(); $query->enableResultCache();
/** @var Sets */ /** @var Sets $queryResult */
$resultQuery = $query->getResult(); $queryResult = $query->getResult();
$result = new Result($resultQuery); $result = new Result($queryResult);
$paginator = new Paginator($query, false); $paginator = new Paginator($query, false);
if (count($paginator) > ($cursor + count($result))) { if (count($paginator) > ($cursor + count($result))) {
$token = new Token('ListSets', [ $token = new Token('ListSets', [
@ -455,13 +450,12 @@ class Database
public function idDoesExist(string $identifier): bool public function idDoesExist(string $identifier): bool
{ {
$dql = $this->entityManager->createQueryBuilder(); $dql = $this->entityManager->createQueryBuilder();
$dql->select('COUNT(record.identifier)') $dql->select($dql->expr()->count('record.identifier'))
->from(Record::class, 'record') ->from(Record::class, 'record')
->where($dql->expr()->eq('record.identifier', ':identifier')) ->where($dql->expr()->eq('record.identifier', ':identifier'))
->setParameter('identifier', $identifier) ->setParameter('identifier', $identifier);
->setMaxResults(1);
$query = $dql->getQuery(); $query = $dql->getQuery();
return (bool) $query->getOneOrNullResult(AbstractQuery::HYDRATE_SINGLE_SCALAR); return (bool) $query->getOneOrNullResult(AbstractQuery::HYDRATE_SCALAR_COLUMN);
} }
/** /**

View File

@ -37,6 +37,10 @@ use Symfony\Component\Validator\Exception\ValidationFailedException;
*/ */
#[ORM\Entity] #[ORM\Entity]
#[ORM\Table(name: 'records')] #[ORM\Table(name: 'records')]
#[ORM\Index(name: 'identifier_idx', columns: ['identifier'])]
#[ORM\Index(name: 'format_idx', columns: ['format'])]
#[ORM\Index(name: 'last_changed_idx', columns: ['last_changed'])]
#[ORM\Index(name: 'format_last_changed_idx', columns: ['format', 'last_changed'])]
class Record extends Entity class Record extends Entity
{ {
/** /**

View File

@ -36,6 +36,7 @@ use OCC\OaiPmh2\Entity;
*/ */
#[ORM\Entity] #[ORM\Entity]
#[ORM\Table(name: 'tokens')] #[ORM\Table(name: 'tokens')]
#[ORM\Index(name: 'valid_until_idx', columns: ['valid_until'])]
class Token extends Entity class Token extends Entity
{ {
/** /**