我在我的 symfony2 项目中使用了学说。
我有一个桌子事件和一张桌子照片。一个事件可以有一张或多张照片,一张照片与一个事件相关。
这是我的 dql 查询之一:
$dql = "
SELECT e, (e.views * 0.1) + (e.likes * 0.9) as ratingEvent
FROM WevseenMainBundle:Event e
INNER JOIN e.photos p
INNER JOIN e.firstPhoto fp
WHERE fp.date BETWEEN :dateA AND :dateB
AND p.lat BETWEEN :latA AND :latB
AND ( p.lng > :lngA AND p.lng < :lngB )
AND e.status = 'open'
GROUP BY e
HAVING COUNT(p.id) >= :minCountPhotos
ORDER BY ratingEvent DESC
";
$query = $em->createQuery($dql)
->setParameters($parameters)
->setFirstResult($firstEntry) // 0
->setMaxResults($numberOf); // 10
$paginatorEvents = new Paginator($query, true);
对于 11500 个事件和 160 000 张照片,查询非常慢(超过 10 秒),它来自
GROUP BY e
HAVING COUNT(p.id) >= :minCountPhotos
没有这个,它很快。
我检查了 sf2 分析器,它说:
SELECT count(DISTINCT e0_.id) AS sclr0 FROM Event e0_ INNER JOIN Photo p1_ ON e0_.id = p1_.event_id INNER JOIN Photo p2_ ON e0_.firstPhoto_id = p2_.id WHERE p2_.date BETWEEN ? AND ? AND p1_.lat BETWEEN ? AND ? AND (p1_.lng > ? AND p1_.lng < ?) AND e0_.status = 'open' GROUP BY e0_.id, e0_.name, e0_.description, e0_.nb_photos, e0_.views, e0_.viewsEventPhotos, e0_.votes, e0_.rating, e0_.likes, e0_.up, e0_.down, e0_.status, e0_.end, e0_.time, e0_.averageTimeEvent, e0_.averageTimePhotos, e0_.averageTimeEventAndPhotos, e0_.needInstagramUpdate, e0_.origin, e0_.featured, e0_.firstPhoto_id HAVING COUNT(p1_.id) >= ?
Parameters: [Object(DateTime), Object(DateTime), '-42.93442389074508', '73.48078267112892', '-180', '180', '2']
Time: 2029.33 ms
SELECT DISTINCT e0_.id AS id0, e0_.views * 1 + e0_.likes * 0 AS sclr1 FROM Event e0_
INNER JOIN Photo p1_ ON e0_.id = p1_.event_id INNER JOIN Photo p2_ ON e0_.firstPhoto_id = p2_.id WHERE p2_.date BETWEEN ? AND ? AND p1_.lat BETWEEN ? AND ? AND (p1_.lng > ? AND p1_.lng < ?) AND e0_.status = 'open' GROUP BY e0_.id, e0_.name, e0_.description, e0_.nb_photos, e0_.views, e0_.viewsEventPhotos, e0_.votes, e0_.rating, e0_.likes, e0_.up, e0_.down, e0_.status, e0_.end, e0_.time, e0_.averageTimeEvent, e0_.averageTimePhotos, e0_.averageTimeEventAndPhotos, e0_.needInstagramUpdate, e0_.origin, e0_.featured, e0_.firstPhoto_id HAVING COUNT(p1_.id) >= ? ORDER BY sclr1 DESC LIMIT 10 OFFSET 0
Time: 6179.01 ms
这是两个需要时间的查询。我该如何改进呢?
更新,解决方案:
改变了
GROUP BY e
和
GROUP BY e.id