-
If you use
GROUP BY
, output rows are sorted according to theGROUP BY
columns as if you had anORDER BY
for the same columns. To avoid the overhead of sorting thatGROUP BY
produces, addORDER BY NULL
:SELECT a, COUNT(b) FROM test_table GROUP BY a ORDER BY NULL;
-- 载自 MySQL 手册.
上面说的是, group by 默认会排序, 如果不想这么做, 必须显式声明.