Group by优化

group by 优化 加order by nullmysql


mysql> explain select uid,sum(times) from tbl_name group by uid\G;sql

*************************** 1. row ***************************ide

          id: 1优化

 select_type: SIMPLEui

       table: tbl_name排序

        type: ALLit

possible_keys: NULLtable

         key: NULLclass

     key_len: NULLfile

         ref: NULL

        rows: 10000

       Extra: Using temporary; Using filesort

1 row in set (0.00 sec)


mysql> explain select uid,sum(times) from tbl_name group by uid order by null\G;

*************************** 1. row ***************************

          id: 1

 select_type: SIMPLE

       table: tbl_name

        type: ALL

possible_keys: NULL

         key: NULL

     key_len: NULL

         ref: NULL

        rows: 10000

       Extra: Using temporary

1 row in set (0.00 sec)

默认状况下,Group by col会对col字段进行排序,这就是为何第一语句里面有Using filesort的缘由,若是你不须要对col字段进行排序,加上order by null吧,

要快不少,由于filesort很慢的。

相关文章
相关标签/搜索