From 527faf27a88f78cbfec71852183f95229a0172d8 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 7 Jun 2020 20:39:51 +0200 Subject: [PATCH] Changed how visits for a tag are fetched, avoiding thousands of values to be loaded in memory --- .../Core/src/Repository/VisitRepository.php | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/module/Core/src/Repository/VisitRepository.php b/module/Core/src/Repository/VisitRepository.php index b3761c9f..5d242241 100644 --- a/module/Core/src/Repository/VisitRepository.php +++ b/module/Core/src/Repository/VisitRepository.php @@ -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