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 排序
`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 排序
相关文章推荐
- 关于MySQL的order by与group by的组合使用
- hive的strict模式;where,group by,having,order by同时使用的执行顺序
- mysql :group by 与order by 共同使用时排序问题,小心utf8的排序
- 在mysql中使用group by和order by取每个分组中日期最大一行数据
- MySQL中使用group by进行分组时,子查询中order by失效的问题
- hive的strict模式;where,group by,having,order by同时使用的执行顺序
- mysql中count(), group by, order by使用详解
- MySql(28)------使用explain分析低效sql的执行情况
- 如何在使用了BOOST_REGEX_MATCH_EXTRA情况下提高boost::regex_search的执行速度
- mysql group by与order by复合使用多注意!
- [mysql] select的子句 where,group by, having, order by, limit的使用顺序及实例
- MySQL使用order by然后再group by的写法实现分类最新时间内容的替代写法
- 在mysql中使用group by和order by取每个分组中日期最大一行数据,亲测有效
- SQL-GROUP BY语句在MySQL中的一个错误使用被兼容的情况
- mysql的group by ,order by语句的使用
- SQL-GROUP BY语句在MySQL中的一个错误使用被兼容的情况
- MySQL中无GROUP BY情况下直接使用HAVING语句的问题探究
- MySql 数据库group by 的用法,order by 嵌套使用。优化问题
- mysql 中 where 、 group by 、having、order by 的执行顺序
- MySql中Group By和Order By使用的注意事项!