有没有更快的方法来进行组sql查询?

时间:2012-12-13 15:45:49

标签: sql group-by

此查询来自两个单独的表。 video_thumb_chosen有一个缩略图,需要选择模式值。

$query = "SELECT DISTINCT videos.VIDEOID, 
                          videos.title,
                          (
                           select video_thumb_chosen.thumb
                           from video_thumb_chosen
                           where video_thumb_chosen.videoid = videos.VIDEOID 
                           group by video_thumb_chosen.thumb
                           order by count(video_thumb_chosen.thumb) desc limit 1 
                          ) as thumb, 
                          videos.rating, 
                          videos.runtime, 
                          videos.viewcount, 
                          videos.public, 
                          videos.time_added, 
                          videos.HD 
          FROM videos,video_thumb_chosen 
          WHERE videos.active='1' 
             && videos.tube=0  
             && videos.categories <> 7 
          ORDER BY videos.last_viewed desc limit $config[max_viewing_now]"; 

1 个答案:

答案 0 :(得分:1)

删除from子句中的交叉联接会有所帮助:

FROM videos

您没有在外部查询中使用video_thumb_chosen,因此没有理由包含它。

也可能有其他优化。

鉴于你在MySQL中做了什么,这可能会解决问题。这是查询:

SELECT videos.VIDEOID, videos.title,
      (select video_thumb_chosen.thumb
       from video_thumb_chosen
       where video_thumb_chosen.videoid = videos.VIDEOID
       group by video_thumb_chosen.thumb
       order by count(video_thumb_chosen.thumb) desc limit 1
      ) as thumb,
      videos.rating, videos.runtime, videos.viewcount, videos.public, videos.time_added, videos.HD
FROM videos
where  videos.active='1' && videos.tube=0  && videos.categories <> 7
ORDER BY videos.last_viewed
desc limit $config[max_viewing_now]";