简体   繁体   中英

How to optimize performance of count query in symfony doctrine (mysql)?

I used to below query to count rows (~ 1M record and left join many table):

SELECT COUNT(DISTINCT u0_.id) AS sclr_0 
FROM user u0_ 
LEFT JOIN user_detail u1_ ON u0_.id = u1_.user_id 
LEFT JOIN recruitment_info r2_ ON u0_.id = r2_.user_id 
LEFT JOIN user u3_ ON u0_.master_account_id = u3_.id 
LEFT JOIN applicants_partners a4_ ON u0_.id = a4_.applicant_id 
LEFT JOIN partner p5_ ON a4_.partner_id = p5_.id 
WHERE u0_.type <> 'PARTNER' 
  AND u0_.is_delete = 0 
ORDER BY u0_.id DESC;

In my symfony, i got total: $total = $queryBuilder->getQuery()->getSingleScalarResult(); It worked well but it took ~ 2.5s.

So, I would like to improve performance of it. I changed it into:

SELECT COUNT(u0_.id) AS sclr_0 
FROM user u0_ 
LEFT JOIN user_detail u1_ ON u0_.id = u1_.user_id 
LEFT JOIN recruitment_info r2_ ON u0_.id = r2_.user_id 
LEFT JOIN user u3_ ON u0_.master_account_id = u3_.id 
LEFT JOIN applicants_partners a4_ ON u0_.id = a4_.applicant_id 
LEFT JOIN partner p5_ ON a4_.partner_id = p5_.id 
WHERE u0_.type <> 'PARTNER' 
  AND u0_.is_delete = 0 
GROUP BY u0_.id 
ORDER BY u0_.id DESC;

The change here is remove DISTINCT and add GROUP BY . Then I apply in symfony by count array result:

$result = $queryBuilder->getQuery()->getArrayResult();
$total = count($result);

So the total is correct but this time, it took ~ 20s, OMG. When I tried run only raw query in Sequel Pro tool, it only took ~ 40ms. Maybe there is a problem in getArrayResult() ? . Please help me, thank you.

As Akina mentions it in his comment, you can simplify this query by removing all the left join, the order by and even the distinct.

Your query will become something like this:

SELECT COUNT(u0_.id) AS sclr_0 
FROM user u0_ 
WHERE u0_.type <> 'PARTNER'
AND u0_.is_delete = 0;

And yes, you have to use getSingleScalarResult() on your Doctrine Query instance.

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