优化 gruop by 语句

时间:2023-03-09 00:27:44
优化 gruop by  语句

默认情况下,mysql对所有的gruop by col1,col2...的字段进行排序。如果查询包含group by但用户想要避免排序结果的消耗,则可以指定order by null禁止排序。

explain select payment_date,sum(amount) from payment group by payment_date\G;

优化 gruop by  语句

 explain select payment_date,sum(amount) from payment group by payment_date order by null\G;

优化 gruop by  语句

从上面的例子可以看出,第一个SQL的执行时间为0.05sec,  而第二个SQL的执行时间为0.00sec。