Improve tags stats performance by using the new short_url_visits_counts table

This commit is contained in:
Alejandro Celaya 2024-03-28 19:17:37 +01:00
parent 6ce1550457
commit 071cb9af2b
2 changed files with 7 additions and 7 deletions

View File

@ -85,7 +85,7 @@ class ShortUrlListRepository extends EntitySpecificationRepository implements Sh
{ {
$qb = $this->getEntityManager()->createQueryBuilder(); $qb = $this->getEntityManager()->createQueryBuilder();
$qb->from(ShortUrl::class, 's') $qb->from(ShortUrl::class, 's')
->where('1=1'); ->where('1=1');
$dateRange = $filtering->dateRange; $dateRange = $filtering->dateRange;
if ($dateRange?->startDate !== null) { if ($dateRange?->startDate !== null) {

View File

@ -76,19 +76,19 @@ class TagRepository extends EntitySpecificationRepository implements TagReposito
$buildVisitsSubQb = static function (bool $excludeBots, string $aggregateAlias) use ($conn) { $buildVisitsSubQb = static function (bool $excludeBots, string $aggregateAlias) use ($conn) {
$visitsSubQb = $conn->createQueryBuilder(); $visitsSubQb = $conn->createQueryBuilder();
$commonJoinCondition = $visitsSubQb->expr()->eq('v.short_url_id', 's.id'); $commonJoinCondition = $visitsSubQb->expr()->eq('sc.short_url_id', 'st.short_url_id');
$visitsJoin = ! $excludeBots $visitsJoin = ! $excludeBots
? $commonJoinCondition ? $commonJoinCondition
: $visitsSubQb->expr()->and( : $visitsSubQb->expr()->and(
$commonJoinCondition, $commonJoinCondition,
$visitsSubQb->expr()->eq('v.potential_bot', $conn->quote('0')), $visitsSubQb->expr()->eq('sc.potential_bot', $conn->quote('0')),
)->__toString(); )->__toString();
return $visitsSubQb return $visitsSubQb
->select('st.tag_id AS tag_id', 'COUNT(DISTINCT v.id) AS ' . $aggregateAlias) ->select('st.tag_id AS tag_id', 'SUM(sc.count) AS ' . $aggregateAlias)
->from('visits', 'v') ->from('short_url_visits_counts', 'sc')
->join('v', 'short_urls', 's', $visitsJoin) ->join('sc', 'short_urls_in_tags', 'st', $visitsJoin)
->join('s', 'short_urls_in_tags', 'st', $visitsSubQb->expr()->eq('st.short_url_id', 's.id')) ->join('sc', 'short_urls', 's', $visitsSubQb->expr()->eq('sc.short_url_id', 's.id'))
->groupBy('st.tag_id'); ->groupBy('st.tag_id');
}; };
$allVisitsSubQb = $buildVisitsSubQb(false, 'visits'); $allVisitsSubQb = $buildVisitsSubQb(false, 'visits');