欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL解惑——GROUP BY隐式排序

程序员文章站 2023-02-06 20:09:35
MySQL中GROUP BY隐式排序是什么概念呢? 主要是其它RDBMS没有这样的概念,如果没有认真了解过概念,对这个概念会感觉有点困惑,我们先来看看官方文档的介绍: 官方文档MySQL 5.7 Reference Manual中的“.2.1.14 ORDER BY Optimization”章节有... ......

mysql中group by隐式排序是什么概念呢? 主要是其它rdbms没有这样的概念,如果没有认真了解过概念,对这个概念会感觉有点困惑,我们先来看看官方文档的介绍:

 

官方文档mysql 5.7 reference manual中的.2.1.14 order by optimization章节有如下介绍:

 

group by implicitly sorts by default (that is, in the absence of asc or desc designators for group by columns). however, relying on implicit group by sorting (that is, sorting in the absence of asc or desc designators) or explicit sorting for group by (that is, by using explicit asc or desc designators for group by columns) is deprecated. to produce a given sort order, provide an order by clause.

 

默认情况下group by隐式排序(即,缺少group by列的asc或desc指示符)。但是,不推荐依赖于隐式group by排序(即,在没有asc或desc指示符的情况下排序)或group by的显式排序(即,通过对group by列使用显式asc或desc指示符)。要生成给定的排序 order,请提供order by子句。

 

从mysql 8.0开始,group by字段不再支持隐式排序. 官方文档mysql 8.0 reference manual中8.2.1.16 order by optimization章节有如下介绍:

 

previously (mysql 5.7 and lower), group by sorted implicitly under certain conditions. in mysql 8.0, that no longer occurs, so specifying order by null at the end to suppress implicit sorting (as was done previously) is no longer necessary. however, query results may differ from previous mysql versions. to produce a given sort order, provide an order by clause.

 

那么来看看mysql的group by隐式排序(group by sorted implicitly)吧。我们用removal of implicit and explicit sorting for group by这篇博客中的例子。

 

下面实验环境为mysql 5.6.41()

 

mysql> select version() from dual;
+------------+
| version()  |
+------------+
| 5.6.41-log |
+------------+
1 row in set (0.00 sec)
 
mysql> create table t (id integer,  cnt integer);
query ok, 0 rows affected (0.04 sec)
 
mysql> insert into t values (4,1),(3,2),(1,4),(2,2),(1,1),(1,5),(2,6),(2,1),(1,3),(3,4),(4,5),(3,6);
query ok, 12 rows affected (0.00 sec)
records: 12  duplicates: 0  warnings: 0

 

#mysql在这里隐式地对group by的结果进行排序(即在缺少group by列的asc或desc指示符的情况下)。

mysql> select id, sum(cnt) from t group by id; --group by隐式排序
+------+----------+
| id   | sum(cnt) |
+------+----------+
|    1 |       13 |
|    2 |        9 |
|    3 |       12 |
|    4 |        6 |
+------+----------+
4 rows in set (0.00 sec)

 

 

mysql还支持使用group by进行显式排序(即通过对group by列使用显式asc或desc指示符)

 

mysql> select id, sum(cnt) from t group by id desc;  --group by显式排序
+------+----------+
| id   | sum(cnt) |
+------+----------+
|    4 |        6 |
|    3 |       12 |
|    2 |        9 |
|    1 |       13 |
+------+----------+
4 rows in set (0.00 sec)

 

从mysql8.0开始,mysql不再支持group by的隐式或显示排序,如下所示:

 

 

#下面实验环境为mysql 8.0.18

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.18    |
+-----------+
1 row in set (0.00 sec)
 
mysql> create table t (id integer,  cnt integer);
query ok, 0 rows affected (0.39 sec)
 
mysql> insert into t values (4,1),(3,2),(1,4),(2,2),(1,1),(1,5),(2,6),(2,1),(1,3),(3,4),(4,5),(3,6);
query ok, 12 rows affected (0.10 sec)
records: 12  duplicates: 0  warnings: 0
 
mysql> select id, sum(cnt) from t group by id;
+------+----------+
| id   | sum(cnt) |
+------+----------+
|    4 |        6 |
|    3 |       12 |
|    1 |       13 |
|    2 |        9 |
+------+----------+
4 rows in set (0.00 sec)
 
mysql> select id, sum(cnt) from t group by id desc;
error 1064 (42000): you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'desc' at line 1

 

 

如上所示,group by隐式排序不支持了,在mysql 8.0中,上面测试例子是无序的。group by显示排序则直接报错。所以如果有数据库从mysql 5.7或之前的版本,迁移升级到mysql 8的话,就需要特别留意这个问题了。正确的做法应该是group by .. order by 这种操作。如下所示:

 

mysql>  select id, sum(cnt) from t group by id
    ->  order by id;
+------+----------+
| id   | sum(cnt) |
+------+----------+
|    1 |       13 |
|    2 |        9 |
|    3 |       12 |
|    4 |        6 |
+------+----------+
4 rows in set (0.00 sec)
 
mysql> 

 

 

mysql 8.0.13中删除了group by的显式排序。至于为什么mysql 8.0不再支持group by的隐式排序和显示排序,这篇文章removal of implicit and explicit sorting for group by已经有详细的介绍,这里不画蛇添足了。

 

 

 

参考资料: