您的位置:首页 > 数据库 > MySQL

mysql 使用 group by和 order by 时extra 的执行情况

2017-02-22 10:39 405 查看
CREATE TABLE `stu` (

  `name` varchar(111) DEFAULT 'bb',

  `score` int(111) DEFAULT NULL,

  `class` varchar(11) DEFAULT NULL,

  KEY `class` (`class`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

explain select class from stu where class >5 group by class;

explain select score from stu where score >5 order by score;

explain select score from stu where score >5 group by score;

result1:  Using where; Using index for group-by

result2:  Using where; Using filesort

result3:  Using where; Using temporary; Using filesort

结论 

若group by 字段有索引   则为result1    使用where 使用index

若order by 字段没有索引 则为result2    多使用一次文件排序

若group by 字段没有索引 则为result3    多使用一次文件排序 且需要使用到临时表

说明 group by 在分组时 使用了临时表  且group by 自带一次排序 可理解为 group by 时 自定为 group by 的字段 asc 排序 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: