mysql优化-group by
一、group by 执行过程
select a,count(*) as cnt from t group by a 假设a上面没有索引
执行过程:
- 创建临时表,有两个字段a,cnt 且a为主键
- 遍历表t,假设记录为R,查看临时表中,是否有a=R.a的记录,没有就新增一条,有就cnt+1
- 临时表按cnt排序
- 返回临时表
当临时表数据大小 select a,count(*) as cnt from t group by a 假设a上面有索引 执行过程: 二、优化
一、group by 执行过程
select a,count(*) as cnt from t group by a 假设a上面没有索引
执行过程:
当临时表数据大小 select a,count(*) as cnt from t group by a 假设a上面有索引 执行过程: 二、优化