Performance comparison of distinct and group by in MySQL

1 Let me talk about the general conclusion first (the complete conclusion is at the end of the article)

  • In the case of the same semantics and an index: both group by and distinct can use the index, and the efficiency is the same.

  • In the case of the same semantics and no index: distinct is more efficient than group by. The reason is that both distinct and group by perform grouping operations, but group by may perform sorting, which triggers filesort, resulting in low SQL execution efficiency.

Based on this conclusion, you might ask:

  • Why do group by and distinct have the same efficiency when they have the same semantics and indexes?

  • Under what circumstances will group by perform a sorting operation?

Find answers to these two questions. Next, let's take a look at the basic usage of distinct and group by.

2 use of distinct

2.1 distinct usage

SELECT DISTINCT columns FROM table_name WHERE where_conditions;

For example:

select DISTINCT age from employee;

output:

The DISTINCT keyword is used to return uniquely distinct values. It is used before the first field in the query statement and acts on all columns of the main clause.

If a column has NULL values ​​and you use the DISTINCT clause on that column, MySQL will keep one NULL value and remove the other NULL values, because the DISTINCT clause treats all NULL values ​​as the same value.

2.2 distinct multi-column deduplication

The deduplication of distinct multiple columns is performed according to the specified deduplication column information, that is, only if all the specified column information is the same, it will be considered as duplicate information.

SELECT DISTINCT columns FROM table_name WHERE where_conditions;

For example:

select DISTINCT age, manager from employee;

output:

3 use of group by

For basic deduplication, the use of group by is similar to distinct.

3.1 Single column deduplication

grammar:

SELECT columns FROM table_name WHERE where_conditions GROUP BY columns;

For example:

SELECT age FROM employee GROUP BY age;

output:

3.2 Multi-column deduplication

grammar:

SELECT columns FROM table_name WHERE where_conditions GROUP BY columns;

example:

SELECT age, manager FROM employee GROUP BY age, manager;

output:

3.3 Difference example

两者的语法区别在于,group by可以进行单列去重,group by的原理是先对结果进行分组排序,然后返回每组中的第一条数据。且是根据group by的后接字段进行去重的。

例如:

SELECT age, manager FROM employee GROUP BY age;

输出:

4 distinct 和 group by 原理

在大多数例子中,DISTINCT可以被看作是特殊的GROUP BY,它们的实现都基于分组操作,且都可以通过松散索引扫描、紧凑索引扫描(关于索引扫描的内容会在其他文章中详细介绍,就不在此细致介绍了)来实现。

DISTINCT和GROUP BY都是可以使用索引进行扫描搜索的。例如以下两条 sql(只单单看表格最后 extra 的内容),我们对这两条 sql 进行分析,可以看到,在 extra 中,这两条 sql 都使用了紧凑索引扫描Using index for group-by。

所以,在一般情况下,对于相同语义的DISTINCT和GROUP BY语句,我们可以对其使用相同的索引优化手段来进行优化。

但对于GROUP BY来说,在 MYSQL8.0 之前,GROUP Y默认会依据字段进行隐式排序。

可以看到,下面这条 sql 语句在使用了临时表的同时,还进行了 filesort。

5 隐式排序

5.1 对于隐式排序,我们可以参考 MySQL 官方的解释:

https://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html

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 进行显式或隐式排序已经过时(deprecated)了,要生成给定的排序顺序,请提供 ORDER BY 子句。

所以,在 MySQL8.0 之前,GROUP BY会默认根据作用字段(GROUP BY的后接字段)对结果进行排序。在能利用索引的情况下,GROUP BY不需要额外进行排序操作;但当无法利用索引排序时,MySQL 优化器就不得不选择通过使用临时表然后再排序的方式来实现GROUP BY了。

且当结果集的大小超出系统设置临时表大小时,MySQL 会将临时表数据 copy 到磁盘上面再进行操作,语句的执行效率会变得极低。这也是 MySQL 选择将此操作(隐式排序)弃用的原因。

5.2 基于上述原因,Mysql 在 8.0 时,对此进行了优化更新:

https://dev.mysql.com/doc/refman/8.0/en/order-by-optimization.html

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.

大致解释一下:

从前(MySQL5.7 版本之前),Group by 会根据确定的条件进行隐式排序。在 MySQL 8.0 中,已经移除了这个功能,所以不再需要通过添加 order by null 来禁止隐式排序了,但是,查询结果可能与以前的 MySQL 版本不同。要生成给定顺序的结果,请按通过 ORDER BY指定需要进行排序的字段。

6 结论

根据上述描述,我们的结论也就出来了

  • 在语义相同,有索引的情况下: group by和 distinct 都能使用索引,效率相同。因为group by和distinct近乎等价,distinct 可以被看做是特殊的group by。

  • 在语义相同,无索引的情况下: distinct效率高于group by。原因是distinct 和 group by都会进行分组操作,但group by在 MySQL8.0 之前会进行隐式排序,导致触发 filesort,sql 执行效率低下。但从 MySQL8.0 开始,MySQL 就删除了隐式排序,所以,此时在语义相同,无索引的情况下,group by和distinct的执行效率也是近乎等价的。

相比于distinct来说,group by的语义明确。且由于 distinct 关键字会对所有字段生效,在进行复合业务处理时,group by的使用灵活性更高,group by能根据分组情况,对数据进行更为复杂的处理,例如通过having对数据进行过滤,或通过聚合函数对数据进行运算。

Guess you like

Origin blog.csdn.net/qq_37284798/article/details/129378330