sql的groupby和orderby疑惑数据

1.  explain select * from operate_log where operate_time<'2019-09-02 16:11:52' and operate_time>'2019-09-02 16:01:52'  
group by relate_id order by relate_id desc , operate_time desc

2.  explain select * from iwork_operate_log where operate_time<'2019-09-02 16:11:52' and operate_time>'2019-09-02 16:01:52'  and relate_id=20 group by relate_id order by relate_id desc , operate_time desc

 

3.  explain select * from iwork_operate_log where operate_time<'2019-09-02 16:11:52' and operate_time>'2019-09-02 16:01:52'  group by relate_id 

不太理解为什么没有file sort,group by不是也有默认排序吗,因为relate_id 本身是索引,本身有序

4.  explain select * from iwork_operate_log where operate_time<'2019-09-02 16:11:52' and operate_time>'2019-09-02 16:01:52'  group by operate_time 

 

5.groupby取得默认不是Id最小的那个,而是groupby的那个字段的降序的那一条,因此如果order by你groupby的那个字段,可以达到组内排序的效果,其他则不行。

 

 

你可能感兴趣的:(数据库)