我有sql查询
SELECT
bd2.*,
bdmax.last_video,
bdmax.number_videos
FROM
video bd2
JOIN
( SELECT
bd.video_cat_id,
MAX(bd.last_poster_time) AS last_video,
COUNT(bd.video_id) as number_videos
FROM
video bd
GROUP BY
bd.video_cat_id
) AS bdmax
ON bd2.video_cat_id = bdmax.video_cat_id
AND bd2.last_poster_time = bdmax.last_video;
表键是
PRIMARY KEY (`video_id`),
KEY `video_cat_id` (`video_cat_id`),
KEY `video_user_id` (`video_user_id`),
KEY `video_time` (`video_time`),
KEY `video_urn` (`video_urn`),
KEY `last_poster_time` (`last_poster_time`),
KEY `video_username` (`video_username`),
KEY `video_approval` (`video_approval`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=45619 ;
如何优化此查询以使用索引?
答案 0 :(得分:0)
通过将表名放在列名的开头,不要混乱。那么这个'复合'指数
INDEX(cat_id, last_poster_time, id)
将帮助子查询(通过“覆盖”)和JOIN
。
此查询的一部分是“groupwise max”。但是COUNT
意味着对max的优化没有用。