命令使得mysql变慢

时间:2016-10-06 09:17:25

标签: mysql sql

我们花了5秒加载但当我删除ORDER BY时,我们只花了0.4秒来改进此查询的任何想法?

以下查询还有更多左连接,但不要认为它不是问题

SELECT b.id, 
  b.name, 
  b.start_date, 
  b.start_time, 
  at.filename, 
  at.width, 
  at.height, 
  at.id   AS attachment_id, 
  ve.name AS venue_name, 
  ve.slug AS v_slug, 
  ve.address, 
  ci.name AS city_name, 
  st.code AS state_code 
FROM   events b 
LEFT JOIN attachments at 
    ON at.foreign_id = b.id 
LEFT JOIN venues ve 
    ON b.venue_id = ve.id 
LEFT JOIN cities ci 
    ON ve.city_id = ci.id 
LEFT JOIN states st 
    ON ci.state_id = st.id 
WHERE  1 = 1 
  AND b.start_date >= '2016-10-06' 
  AND b.is_active = 1 
  AND b.virtual_delete = 0 
  AND ve.is_approved = 1 
GROUP  BY b.id 
ORDER  BY b.event_view_count DESC 
LIMIT  30

1 个答案:

答案 0 :(得分:1)

请参阅this article

  

要提高ORDER BY速度,请检查是否可以让MySQL使用索引而不是额外的排序阶段。

首先检查event_view_count列是否已编入索引。