简体   繁体   中英

Symfony with queryBuiler and understanding performance

I have a query that returns a single product with its relationships. I want this page to load fast, so query performance is very important for me. However, I am having big performance issues and I don`t really understand how this could be fixed in Symfony.

This is my query:

public function findProduct(string $id, string $locale)
    {
        $dateNow = date('Y-m-d') . ' 23:59:59';
        $qb = $this->createQueryBuilder('p');
        $qb->innerJoin('p.productRegionData', 'prd');
//        $qb->addSelect('prd');
        $qb->innerJoin('prd.region', 'r', 'WITH', "r.iso = '" . $locale . "'");
        $qb->leftJoin('p.productCategories', 'pc');
//        $qb->addSelect('pc');
        $qb->innerJoin('p.productSeoUrls', 'psu');
//        $qb->addSelect('psu');
        $qb->leftJoin('p.productDiscounts', 'pd', 'WITH', "pd.active = 1 AND pd.discountFrom <= '" . $dateNow . "' AND pd.discountTo >= '" . $dateNow . "'");
//        $qb->addSelect('pd');
        $qb->leftJoin('p.productWallThicknesses', 'pwt');
//        $qb->addSelect('pwt');
        $qb->innerJoin('p.productImages', 'pi');
//        $qb->addSelect('pi');
        $qb->leftJoin('p.productFloorDimensions', 'pfd');
//        $qb->addSelect('pfd');
        $qb->leftJoin('p.productWindows', 'pw');
//        $qb->addSelect('pw');
        $qb->leftJoin('p.productDoors', 'pdo');
//        $qb->addSelect('pdo');
        $qb->leftJoin('p.productAttributes', 'pa');
//        $qb->addSelect('pa');
        $qb->leftJoin('pa.attribute', 'at');
//        $qb->addSelect('at');
        $qb->andWhere('p.id = :id');
        $qb->setParameter('id', $id);
        $qb->groupBy('p');
        return $qb->getQuery()->getOneOrNullResult();
    }

As you can see this query returns a product with multiple relationships. Now there are 3 scenarios that I have tried and each of them solves one problem, but another problem occurs.

SCENARIO 1: Using groupBy without addSelect() .

This is the fastest in terms of performance, however each time I access a relationship, a new query is excecuted(if I understand that is because I am not using addSelect() and thats how Doctrine works). This is pretty quick, however I am concerned with the large amount of queries being executed for a single product:

在此处输入图像描述

在此处输入图像描述

SCENARIO 2: Not using groupBy and using addSelect()

Performance wise this scenario is the slowest because I am running out of php memory(its 128MB but it still shouldn t take that much?). However the number of queries is reduced drastically since I am using t take that much?). However the number of queries is reduced drastically since I am using addSelect()`

SCENARIO 3: Using groupBy and using addSelect()

The fastest solution in terms of performance, however, using this will return a single entity from a relationship. For example if I have 5 images, it will return only 1 image, which is of course not what I want.

So now I am really struggling to understand how this should be achieved in the best way? Which scenario is the best, maybe there is a better one? Maybe I should just use raw sql? Any help is appreciated.

You don't have a relation between all these tables?

Your query is restarting in a loop.

Are you just selecting from the same table? I don't understand your inner join syntax.

<?php
$queryBuilder
    ->select('u.id', 'u.name', 'p.number')
    ->from('users', 'u')
    ->innerJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM