Changed how visits for a tag are fetched, avoiding thousands of values to be loaded in memory

This commit is contained in:
Alejandro Celaya 2020-06-07 20:39:51 +02:00
parent 9c339b9c4f
commit 527faf27a8

View file

@ -142,26 +142,18 @@ class VisitRepository extends EntityRepository implements VisitRepositoryInterfa
private function createVisitsByTagQueryBuilder(string $tag, ?DateRange $dateRange = null): QueryBuilder
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('s.id')
->from(ShortUrl::class, 's')
->join('s.tags', 't')
->where($qb->expr()->eq('t.name', ':tag'))
->setParameter('tag', $tag);
$shortUrlIds = array_column($qb->getQuery()->getArrayResult(), 'id');
$shortUrlIds[] = '-1'; // Add an invalid ID, in case the list is empty
// Parameters in this query need to be part of the query itself, as we need to use it a sub-query later
// Since they are not strictly provided by the caller, it's reasonably safe
$qb2 = $this->getEntityManager()->createQueryBuilder();
$qb2->from(Visit::class, 'v')
->where($qb2->expr()->in('v.shortUrl', $shortUrlIds));
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->from(Visit::class, 'v')
->join('v.shortUrl', 's')
->join('s.tags', 't')
->where($qb->expr()->eq('t.name', '\'' . $tag . '\''));
// Apply date range filtering
$this->applyDatesInline($qb2, $dateRange);
$this->applyDatesInline($qb, $dateRange);
return $qb2;
return $qb;
}
private function applyDatesInline(QueryBuilder $qb, ?DateRange $dateRange): void