MySql:如何减少这个Query的执行时间?

时间:2016-04-18 16:06:13

标签: mysql sql

如何自定义此查询,大约需要30秒才能取出结果,并在'视频'该表大约有50万,会员和会员中有300万会员。表,是否有任何备用查询或我应该在2个选择查询中中断此查询?

user_id已编入索引 vid_id已编入索引

select a.ref_url , a.source , a.video_name , a.viewers , b.username ,           
c.points from 
members_videos a 
inner join Members b on a.user_id = b.user_id
inner join rankings      c on c.user_id = b.user_id

where a.cat_ids in (123,234,52,234,423,122) not in (110,99)
order by a.vid_id Desc limit 10 

1 个答案:

答案 0 :(得分:0)

有多种因素:

  1. 确保members_videos,成员和排名在user_id列上有索引。
  2. 打破查询;消除Order By子句。获取临时表中的数据。
  3. 从临时表中选择数据并将Order By Clause放在那里。
  4. 替代,尝试子查询。

    SELECT *
    FROM
    (
    select a.ref_url , a.source , a.video_name , a.viewers , b.username ,           
    c.points from 
    members_videos a 
    inner join Members b on a.user_id = b.user_id
    inner join rankings      c on c.user_id = b.user_id
    where a.cat_ids in (123,234,52,234,423,122) not in (110,99)
    ) as T
    order by T.vid_id Desc limit 10