MYSQL优化之GROUP BY
2017-10-15 11:56
295 查看
MYSQL优化之GROUP BY
使用group by进行分组的时候,会自动对分组的字段进行排序mysql> explain select id from ds_goods group by type_id\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: ds_goods type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 3011379 Extra: Using temporary; Using filesort 1 row in set (0.00 sec)
如果不需要这个排序可以使用
order by null取消它.
mysql> explain select id from ds_goods group by type_id order by null\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: ds_goods type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 3011379 Extra: Using temporary 1 row in set (0.00 sec)
可以看到在Extra的部分,使用了
order by null就不会提示
Using filesort了.
Using temporary表明的是用到了临时表.
我们可以通过对这个字段建立索引的方式解决这个问题.
mysql> alter table ds_goods add key type (type_id); Query OK, 0 rows affected (10.05 sec) Records: 0 Duplicates: 0 Warnings: 0
需要注意的是当表很大的时候,建立索引是需要时间的.
再次查询.
mysql> explain select id from ds_goods group by type_id order by null\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: ds_goods type: range possible_keys: type key: type key_len: 4 ref: NULL rows: 11 Extra: Using index for group-by 1 row in set (0.00 sec)
相关文章推荐
- MySql 的group by having 优化案例
- mysql group by 优化
- MYSQL优化-之GROUP BY
- MySQL Group By的优化
- MySQL Group By的优化
- mysql "group by"与"order by"的研究--分类中最新的内容
- MySQL order by的一个优化思路
- MySQL: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'sss
- mysql group by
- mysql select * .... group by ..... 语句使用
- mysql命令gruop by报错this is incompatible with sql_mode=only_full_group_by
- MySQL Order By实现原理分析和Filesort优化
- -gMIS 吉密斯优化更新+分组项区段AddGroupBySeg/+复制AddByCopy等
- MySQL GROUP BY 语句使用总结
- mysql查询中group by语句中出现sql_mode=only_full_group_by
- mysql group by中没有的字段 select中可以有
- Linq Mysql GroupBy语句的问题处理
- MySQL: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column '
- MySQL order by 分页查询优化
- mysql group by怎么用