MySQL查询花费了太多时间

时间:2017-05-03 11:23:48

标签: mysql

我在下面查询:

SELECT t.t_id
     , t.usr_idx
     , t.t_is_for
     , t.tg_ids
     , t.created_time
     , t.allow_reply
     , u.usr_name
     , u.usr_avatar
     , u.show_profile
     , IF(u.usr_timeline != '',CONCAT('https://s3.amazonaws.com/tuurnts3thumbnail/',u.usr_timeline),'') as usr_timeline
     , u.node_userid
     , t.t_time
  FROM tuu_tuurnt t
  JOIN tuu_user u 
    ON t.usr_idx = u.usr_idx 
   AND u.usr_state = 1
  LEFT 
  JOIN tuu_post p 
    ON t.t_id = p.t_id 
   AND p.usr_idx = 44756
  LEFT 
  JOIN tuu_friend f 
    ON f.frd_my_idx = 44756
   AND f.frd_your_idx = t.usr_idx
  LEFT 
  JOIN tuu_friend fl 
    ON fl.frd_your_idx = 44756 
   AND fl.frd_my_idx = t.usr_idx
 WHERE t.status = 0
   AND NOT EXISTS ( SELECT b.tuu_b_by_usr_idx 
                      FROM tuu_blocked as b 
                     WHERE b.tuu_b_usr_idx = 44756
                       AND t.usr_idx = b.tuu_b_by_usr_idx
                  )     
 GROUP 
    BY t.t_id     
 ORDER 
    BY t.t_time DESC
     , t.t_id DESC     
 LIMIT 0,30;

结果需要大约7-8秒,但是当我通过t.t_time和t.t_id删除订单时,它会在1秒内运行。

我有什么问题吗?

1 个答案:

答案 0 :(得分:0)

如果没有索引,MySQL必须以第一行开头,然后读取整个表以查找相关行。表越大,成本越高。有关详细信息,请参阅How MySQL Uses Indexes。另请参阅this topic有关一起使用索引和别名的信息。