Skip to content

Commit

Permalink
Optimisations des requêtes SQL (1/4) (#845)
Browse files Browse the repository at this point in the history
* MembershipShiftExemption: always prefetch ShiftExemption

* Membership autocomplete: prefetch beneficiaries

* Event: always prefetch EventKind

* Shift: always prefetch Job

* ShiftFreeLog: always prefetch Shift

* Event: fix queries

* Position: prefetch Job

* User search: prefetch Formation, Commission, MembershipShiftExemptions

* Registration: prefetch HelloassoPayments

* Extra optimizations in ambassador search

* AnonymousBeneficiary: prefetch registrar
  • Loading branch information
raphodn authored May 13, 2023
1 parent 935efb8 commit 58634a3
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/AppBundle/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function usersAction(Request $request, SearchUserFormHelper $formHelper)

$action = $form->get('action')->getData();

$qb = $formHelper->initSearchQuery($this->getDoctrine()->getManager());
$qb = $formHelper->initSearchQuery($this->getDoctrine()->getManager(), 'search');

if ($form->isSubmitted() && $form->isValid()) {
$formHelper->processSearchFormData($form, $qb);
Expand Down
4 changes: 3 additions & 1 deletion src/AppBundle/Controller/AmbassadorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ public function memberShiftTimeLogAction(Request $request, SearchUserFormHelper
$qb = $formHelper->initSearchQuery($this->getDoctrine()->getManager());
$qb = $qb->leftJoin("o.registrations", "lr", Join::WITH,'lr.date > r.date')->addSelect("lr")
->where('lr.id IS NULL') // registration is the last one registered
->addSelect("(SELECT SUM(ti.time) FROM AppBundle\Entity\TimeLog ti WHERE ti.membership = o.id) AS HIDDEN time");
->addSelect("(SELECT SUM(ti.time) FROM AppBundle\Entity\TimeLog ti WHERE ti.membership = o.id) AS HIDDEN time")
->leftJoin("o.timeLogs", "tl")->addSelect("tl")
->leftJoin("o.notes", "n")->addSelect("n");

if ($form->isSubmitted() && $form->isValid()) {
$qb = $formHelper->processSearchFormAmbassadorData($form, $qb);
Expand Down
1 change: 0 additions & 1 deletion src/AppBundle/Controller/ShiftFreeLogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public function indexAction(Request $request)
$order = 'DESC';

$qb = $em->getRepository('AppBundle:ShiftFreeLog')->createQueryBuilder('sfl')
->leftJoin("sfl.shift", "s")
->orderBy('sfl.' . $sort, $order);

if ($filter["created_at"]) {
Expand Down
4 changes: 2 additions & 2 deletions src/AppBundle/Entity/AnonymousBeneficiary.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AnonymousBeneficiary
private $email;

/**
* @ORM\OneToOne(targetEntity="Beneficiary")
* @ORM\OneToOne(targetEntity="Beneficiary", fetch="EAGER")
* @ORM\JoinColumn(name="join_to", referencedColumnName="id", onDelete="SET NULL")
* @AppAssert\BeneficiaryCanHost
*/
Expand Down Expand Up @@ -67,7 +67,7 @@ class AnonymousBeneficiary
private $mode;

/**
* @ORM\ManyToOne(targetEntity="User")
* @ORM\ManyToOne(targetEntity="User", fetch="EAGER")
* @ORM\JoinColumn(name="registrar_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $registrar;
Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Entity/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Event
private $end;

/**
* @ORM\ManyToOne(targetEntity="EventKind", inversedBy="events")
* @ORM\ManyToOne(targetEntity="EventKind", inversedBy="events", fetch="EAGER")
* @ORM\JoinColumn(name="event_kind_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $kind;
Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Entity/HelloassoPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class HelloassoPayment
private $status;

/**
* @ORM\OneToOne(targetEntity="Registration", inversedBy="helloassoPayment")
* @ORM\OneToOne(targetEntity="Registration", inversedBy="helloassoPayment", fetch="EAGER")
* @ORM\JoinColumn(name="registration_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $registration;
Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Entity/MembershipShiftExemption.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MembershipShiftExemption
private $createdBy;

/**
* @ORM\ManyToOne(targetEntity="ShiftExemption", inversedBy="membershipShiftExemptions")
* @ORM\ManyToOne(targetEntity="ShiftExemption", inversedBy="membershipShiftExemptions", fetch="EAGER")
* @ORM\JoinColumn(name="shift_exemption_id", referencedColumnName="id")
*/
private $shiftExemption;
Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Entity/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Period

/**
* One Period has One Job.
* @ORM\ManyToOne(targetEntity="Job", inversedBy="periods")
* @ORM\ManyToOne(targetEntity="Job", inversedBy="periods", fetch="EAGER")
* @ORM\JoinColumn(name="job_id", referencedColumnName="id", nullable=false)
*/
private $job;
Expand Down
4 changes: 2 additions & 2 deletions src/AppBundle/Entity/PeriodPosition.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class PeriodPosition

/**
* One Period has One Formation.
* @ORM\ManyToOne(targetEntity="Formation")
* @ORM\ManyToOne(targetEntity="Formation", fetch="EAGER")
* @ORM\JoinColumn(name="formation_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $formation;

/**
* @ORM\ManyToOne(targetEntity="Period", inversedBy="positions")
* @ORM\ManyToOne(targetEntity="Period", inversedBy="positions", fetch="EAGER")
* @ORM\JoinColumn(name="period_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $period;
Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Entity/Registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Registration
private $registrar;

/**
* @ORM\OneToOne(targetEntity="HelloassoPayment", mappedBy="registration", cascade={"persist"})
* @ORM\OneToOne(targetEntity="HelloassoPayment", mappedBy="registration", cascade={"persist"}, fetch="EAGER")
*/
private $helloassoPayment;

Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Entity/Shift.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Shift

/**
* One Shift has One Job.
* @ORM\ManyToOne(targetEntity="Job", inversedBy="shifts")
* @ORM\ManyToOne(targetEntity="Job", inversedBy="shifts", fetch="EAGER")
* @ORM\JoinColumn(name="job_id", referencedColumnName="id", nullable=false)
*/
private $job;
Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Entity/ShiftFreeLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ShiftFreeLog
private $createdBy;

/**
* @ORM\ManyToOne(targetEntity="Shift", cascade={"remove"})
* @ORM\ManyToOne(targetEntity="Shift", inversedBy="shiftFreeLogs", cascade={"remove"}, fetch="EAGER")
* @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
*/
private $shift;
Expand Down
14 changes: 6 additions & 8 deletions src/AppBundle/Repository/BeneficiaryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,15 @@ public function findFromAutoComplete($beneficiaries)
/**
* findAllActive
*
* return all the active beneficiaries meaning with
* an active membership
* return all the active beneficiaries with an active membership
*/
public function findAllActive()
{

$qb = $this->createQueryBuilder('beneficiary')
->select('beneficiary, membership')
->join('beneficiary.user', 'user')
->join('beneficiary.membership', 'membership')
->where('membership.withdrawn = 0');
$qb = $this->createQueryBuilder('b')
->select('b, m')
->join('b.user', 'user')
->join('b.membership', 'm')
->where('m.withdrawn = 0');

return $qb
->getQuery()
Expand Down
12 changes: 10 additions & 2 deletions src/AppBundle/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function findAll()
public function findFutures(\DateTime $max = null, EventKind $eventKind = null)
{
$qb = $this->createQueryBuilder('e')
->select('e, ek')
->leftJoin('e.kind', 'ek')
->addSelect('ek')
->where('e.date > :now')
->setParameter('now', new \Datetime('now'));

Expand All @@ -44,16 +44,24 @@ public function findFutures(\DateTime $max = null, EventKind $eventKind = null)
->getResult();
}

public function findPast(int $limit = null)
public function findPast(int $limit = null, EventKind $eventKind = null)
{
$qb = $this->createQueryBuilder('e')
->leftJoin('e.kind', 'ek')
->addSelect('ek')
->where('e.date < :now')
->setParameter('now', new \Datetime('now'));

if ($limit) {
$qb->setMaxResults($limit);
}

if ($eventKind) {
$qb
->andwhere('e.kind = :kind')
->setParameter('kind', $eventKind);
}

$qb->orderBy('e.date', 'DESC');

return $qb
Expand Down
12 changes: 10 additions & 2 deletions src/AppBundle/Repository/MembershipRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function findOneFromAutoComplete($membership)
preg_match('/#(\d+)\s/s', $membership, $matches);
$membershipMemberNumber = $matches[1];


$qb = $this->createQueryBuilder('m')
->where('m.member_number = :memberNumber')
->setParameter('memberNumber', $membershipMemberNumber);
Expand All @@ -35,11 +34,20 @@ public function findOneFromAutoComplete($membership)
->getOneOrNullResult();
}

public function findAllActive()
/**
* return all the active memberships
*/
public function findAllActive($prefetchBeneficiaries = true)
{
$qb = $this->createQueryBuilder('m')
->where('m.withdrawn = 0');

if ($prefetchBeneficiaries) {
$qb
->leftJoin('m.beneficiaries', 'b')
->addSelect('b');
}

return $qb
->getQuery()
->getResult();
Expand Down
42 changes: 21 additions & 21 deletions src/AppBundle/Service/SearchUserFormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,17 @@ public function createMemberShiftTimeLogFilterForm($formBuilder, $defaults = [],
* @param EntityManager $doctrineManager
* @return QueryBuilder
*/
public function initSearchQuery($doctrineManager) {
public function initSearchQuery($doctrineManager, $type = null) {
$qb = $doctrineManager->getRepository("AppBundle:Membership")->createQueryBuilder('o');
$qb = $qb->leftJoin("o.beneficiaries", "b")
->leftJoin("b.user", "u")
$qb = $qb->leftJoin("o.beneficiaries", "b")->addSelect("b")
->leftJoin("b.user", "u")->addSelect("u")
->leftJoin("o.registrations", "r")->addSelect("r")
->leftJoin("o.membershipShiftExemptions", "e");
->leftJoin("r.helloassoPayment", "rhp")->addSelect("rhp")
->leftJoin("o.membershipShiftExemptions", "mse")->addSelect("mse");
if ($type == 'search') {
$qb->leftJoin("b.commissions", "c")->addSelect("c");
$qb->leftJoin("b.formations", "f")->addSelect("f");
}
// do not include admin user
$qb = $qb->andWhere('o.member_number > 0');
return $qb;
Expand Down Expand Up @@ -426,10 +431,10 @@ public function processSearchFormData($form,&$qb) {
}
if ($form->get('exempted')->getData() > 0) {
if ($form->get('exempted')->getData() == 2) {
$qb = $qb->andWhere('e.start <= :date AND e.end >= :date')
$qb = $qb->andWhere('mse.start <= :date AND mse.end >= :date')
->setParameter('date', $now);
} else if ($form->get('exempted')->getData() == 1) {
$qb = $qb->andWhere('e.start IS NULL OR e.start > :date OR e.end < :date')
$qb = $qb->andWhere('mse.start IS NULL OR mse.start > :date OR mse.end < :date')
->setParameter('date', $now);
}
}
Expand Down Expand Up @@ -559,9 +564,8 @@ public function processSearchFormData($form,&$qb) {
$ids_groups = array();
foreach ($formations as $formation) {
$tmp_qb = clone $qb;
$tmp_qb = $tmp_qb->leftjoin("b.formations", "ro")
->andWhere('ro.id IN (:rid)')
->setParameter('rid', $formation );
$tmp_qb = $tmp_qb->andWhere('f.id IN (:fid)')
->setParameter('fid', $formation);
$ids_groups[] = $tmp_qb->select('DISTINCT o.id')->getQuery()->getArrayResult();
}
$ids = $ids_groups[0];
Expand All @@ -576,26 +580,23 @@ public function processSearchFormData($form,&$qb) {
$qb = $qb->andWhere('o.id IN (:all_formations)')
->setParameter('all_formations', $ids);
} else {
$qb = $qb->leftjoin("b.formations", "ro")
->andWhere('ro.id IN (:rids)')
->setParameter('rids', $form->get('formations')->getData());
$qb = $qb->andWhere('f.id IN (:fids)')
->setParameter('fids', $form->get('formations')->getData());
$join_formations = true;
}
}
$join_commissions = false;
if ($form->get('commissions')->getData() && count($form->get('commissions')->getData())) {
$qb = $qb->leftjoin("b.commissions", "c")
->andWhere('c.id IN (:cids)')
->setParameter('cids', $form->get('commissions')->getData() );
$qb->andWhere('c.id IN (:cids)')
->setParameter('cids', $form->get('commissions')->getData());
$join_commissions = true;
}
if ($form->get('not_formations')->getData() && count($form->get('not_formations')->getData())) {
$nrqb = clone $qb;
if (!$join_formations) {
$nrqb = $nrqb->leftjoin("b.formations", "ro")
->andWhere('ro.id IN (:rids)');
$nrqb = $nrqb->andWhere('f.id IN (:fids)');
}
$nrqb->setParameter('rids', $form->get('not_formations')->getData() );
$nrqb->setParameter('fids', $form->get('not_formations')->getData() );
$subQuery = $nrqb->select('DISTINCT o.id')->getQuery()->getArrayResult();

if (count($subQuery)) {
Expand All @@ -607,10 +608,9 @@ public function processSearchFormData($form,&$qb) {
if ($form->get('not_commissions')->getData() && count($form->get('not_commissions')->getData())) {
$ncqb = clone $qb;
if (!$join_commissions) {
$ncqb = $ncqb->leftjoin("b.commissions", "c")
->andWhere('c.id IN (:cids)');
$ncqb->andWhere('c.id IN (:cids)');
}
$ncqb->setParameter('cids', $form->get('not_commissions')->getData() );
$ncqb->setParameter('cids', $form->get('not_commissions')->getData());
$subQuery = $ncqb->select('DISTINCT o.id')->getQuery()->getArrayResult();

if (count($subQuery)) {
Expand Down

0 comments on commit 58634a3

Please sign in to comment.