Skip to content

Commit

Permalink
Add database indices for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-meyer committed Feb 1, 2024
1 parent 16dbc1c commit 17bfc16
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
34 changes: 14 additions & 20 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public function getAllSets(): Result
->from(Set::class, 'sets', 'sets.spec');
$query = $dql->getQuery();
$query->enableResultCache();
/** @var Sets */
/** @var Sets $resultQuery */
$resultQuery = $query->getResult();
return new Result($resultQuery);
}
Expand All @@ -236,18 +236,13 @@ public function getEarliestDatestamp(): string
{
$timestamp = '0000-00-00T00:00:00Z';
$dql = $this->entityManager->createQueryBuilder();
$dql->select('record')
->from(Record::class, 'record')
->orderBy('record.lastChanged', 'ASC')
->setMaxResults(1);
$dql->select($dql->expr()->min('record.lastChanged'))
->from(Record::class, 'record');
$query = $dql->getQuery();
$query->enableResultCache();
/** @var ?array<string, \DateTime> */
$result = $query->getOneOrNullResult(AbstractQuery::HYDRATE_ARRAY);
if (isset($result)) {
$timestamp = $result['lastChanged']->format('Y-m-d\TH:i:s\Z');
}
return $timestamp;
/** @var ?string $result */
$result = $query->getOneOrNullResult(AbstractQuery::HYDRATE_SCALAR_COLUMN);
return $result ?? $timestamp;
}

/**
Expand Down Expand Up @@ -284,7 +279,7 @@ public function getMetadataFormats(?string $identifier = null): Result
}
$query = $dql->getQuery();
$query->enableResultCache();
/** @var Formats */
/** @var Formats $queryResult */
$queryResult = $query->getResult();
return new Result($queryResult);
}
Expand Down Expand Up @@ -364,7 +359,7 @@ public function getRecords(
$set = $set->getSpec();
}
$query = $dql->getQuery();
/** @var Records */
/** @var Records $queryResult */
$queryResult = $query->getResult();
$result = new Result($queryResult);
$paginator = new Paginator($query, true);
Expand Down Expand Up @@ -429,9 +424,9 @@ public function getSets(int $counter = 0): Result
->setMaxResults($maxRecords);
$query = $dql->getQuery();
$query->enableResultCache();
/** @var Sets */
$resultQuery = $query->getResult();
$result = new Result($resultQuery);
/** @var Sets $queryResult */
$queryResult = $query->getResult();
$result = new Result($queryResult);
$paginator = new Paginator($query, false);
if (count($paginator) > ($cursor + count($result))) {
$token = new Token('ListSets', [
Expand All @@ -455,13 +450,12 @@ public function getSets(int $counter = 0): Result
public function idDoesExist(string $identifier): bool
{
$dql = $this->entityManager->createQueryBuilder();
$dql->select('COUNT(record.identifier)')
$dql->select($dql->expr()->count('record.identifier'))
->from(Record::class, 'record')
->where($dql->expr()->eq('record.identifier', ':identifier'))
->setParameter('identifier', $identifier)
->setMaxResults(1);
->setParameter('identifier', $identifier);
$query = $dql->getQuery();
return (bool) $query->getOneOrNullResult(AbstractQuery::HYDRATE_SINGLE_SCALAR);
return (bool) $query->getOneOrNullResult(AbstractQuery::HYDRATE_SCALAR_COLUMN);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Entity/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
*/
#[ORM\Entity]
#[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
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/Entity/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*/
#[ORM\Entity]
#[ORM\Table(name: 'tokens')]
#[ORM\Index(name: 'valid_until_idx', columns: ['valid_until'])]
class Token extends Entity
{
/**
Expand Down

0 comments on commit 17bfc16

Please sign in to comment.