SQL and performance improvements (#2017)

This commit is contained in:
Kevin Papst
2020-10-23 00:31:53 +02:00
committed by GitHub
parent 6ed7ee76e7
commit 4533c994a3
14 changed files with 216 additions and 206 deletions

View File

@@ -11,6 +11,7 @@ namespace App\Repository;
use App\Entity\Customer;
use App\Entity\Invoice;
use App\Entity\Team;
use App\Entity\User;
use App\Repository\Loader\InvoiceLoader;
use App\Repository\Paginator\LoaderPaginator;
@@ -115,23 +116,25 @@ class InvoiceRepository extends EntityRepository
$teams = array_merge($teams, $user->getTeams()->toArray());
}
$qb
->leftJoin('i.customer', 'c')
->leftJoin('c.teams', 'c_teams');
$qb->leftJoin('i.customer', 'c');
if (empty($teams)) {
$qb->andWhere($qb->expr()->isNull('c_teams'));
$qb->andWhere('SIZE(c.teams) = 0');
return;
}
$orCustomer = $qb->expr()->orX(
$qb->expr()->isNull('c_teams'),
'SIZE(c.teams) = 0',
$qb->expr()->isMemberOf(':teams', 'c.teams')
);
$qb->andWhere($orCustomer);
$qb->setParameter('teams', $teams);
$ids = array_values(array_unique(array_map(function (Team $team) {
return $team->getId();
}, $teams)));
$qb->setParameter('teams', $ids);
}
private function getQueryBuilderForQuery(InvoiceQuery $query): QueryBuilder
@@ -154,13 +157,6 @@ class InvoiceRepository extends EntityRepository
$this->addPermissionCriteria($qb, $query->getCurrentUser());
// this will make sure, that we do not accidentally create results with multiple rows,
// which would result in a wrong LIMIT with paginated results
$qb->addGroupBy('i');
// the second group by is needed to satisfy SQL standard (ONLY_FULL_GROUP_BY)
$qb->addGroupBy($orderBy);
return $qb;
}