服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|

服务器之家 - 数据库 - Mysql - MySQL中distinct语句的基本原理及其与group by的比较

MySQL中distinct语句的基本原理及其与group by的比较

2020-05-31 15:26海底苍鹰 Mysql

这篇文章主要介绍了MySQL中distinct语句的基本原理及其与group by的比较,一般情况下来说group by和distinct的实现原理相近且性能稍好,需要的朋友可以参考下

DISTINCT 实际上和 GROUP BY 操作的实现非常相似,只不过是在 GROUP BY 之后的每组中只取出一条记录而已。所以,DISTINCT 的实现和 GROUP BY 的实现也基本差不多,没有太大的区别。同样可以通过松散索引扫描或者是紧凑索引扫描来实现,当然,在无法仅仅使用索引即能完成 DISTINCT 的时候,MySQL 只能通过临时表来完成。但是,和 GROUP BY 有一点差别的是,DISTINCT 并不需要进行排序。也就是说,在仅仅只是 DISTINCT 操作的 Query 如果无法仅仅利用索引完成操作的时候,MySQL 会利用临时表来做一次数据的“缓存”,但是不会对临时表中的数据进行 filesort 操作。当然,如果我们在进行 DISTINCT 的时候还使用了 GROUP BY 并进行了分组,并使用了类似于 MAX 之类的聚合函数操作,就无法避免 filesort 了。

下面我们就通过几个简单的 Query 示例来展示一下 DISTINCT 的实现。

1.首先看看通过松散索引扫描完成 DISTINCT 的操作:

?
1
2
sky@localhost : example 11:03:41> EXPLAIN SELECT DISTINCT group_id
  -> FROM group_messageG
?
1
2
3
4
5
6
7
8
9
10
11
12
*************************** 1. row ***************************
      id: 1
 SELECT_type: SIMPLE
    table: group_message
     type: range
possible_keys: NULL
     key: idx_gid_uid_gc
   key_len: 4
     ref: NULL
     rows: 10
    Extra: Using index for group-by
1 row in set (0.00 sec)

我们可以很清晰的看到,执行计划中的 Extra 信息为“Using index for group-by”,这代表什么意思?为什么我没有进行 GROUP BY 操作的时候,执行计划中会告诉我这里通过索引进行了 GROUP BY 呢?其实这就是于 DISTINCT 的实现原理相关的,在实现 DISTINCT的过程中,同样也是需要分组的,然后再从每组数据中取出一条返回给客户端。而这里的 Extra 信息就告诉我们,MySQL 利用松散索引扫描就完成了整个操作。当然,如果 MySQL Query Optimizer 要是能够做的再人性化一点将这里的信息换成“Using index for distinct”那就更好更容易让人理解了,呵呵。

2.我们再来看看通过紧凑索引扫描的示例:

?
1
2
3
sky@localhost : example 11:03:53> EXPLAIN SELECT DISTINCT user_id
  -> FROM group_message
  -> WHERE group_id = 2G
?
1
2
3
4
5
6
7
8
9
10
11
12
*************************** 1. row ***************************
      id: 1
 SELECT_type: SIMPLE
    table: group_message
     type: ref
possible_keys: idx_gid_uid_gc
     key: idx_gid_uid_gc
   key_len: 4
     ref: const
     rows: 4
    Extra: Using WHERE; Using index
1 row in set (0.00 sec)

这里的显示和通过紧凑索引扫描实现 GROUP BY 也完全一样。实际上,这个 Query 的实现过程中,MySQL 会让存储引擎扫描 group_id = 2 的所有索引键,得出所有的 user_id,然后利用索引的已排序特性,每更换一个 user_id 的索引键值的时候保留一条信息,即可在扫描完所有 gruop_id = 2 的索引键的时候完成整个 DISTINCT 操作。

3.下面我们在看看无法单独使用索引即可完成 DISTINCT 的时候会是怎样:

?
1
2
3
sky@localhost : example 11:04:40> EXPLAIN SELECT DISTINCT user_id
  -> FROM group_message
  -> WHERE group_id > 1 AND group_id < 10G
?
1
2
3
4
5
6
7
8
9
10
11
12
*************************** 1. row ***************************
      id: 1
 SELECT_type: SIMPLE
    table: group_message
     type: range
possible_keys: idx_gid_uid_gc
     key: idx_gid_uid_gc
   key_len: 4
     ref: NULL
     rows: 32
    Extra: Using WHERE; Using index; Using temporary
1 row in set (0.00 sec)

当 MySQL 无法仅仅依赖索引即可完成 DISTINCT 操作的时候,就不得不使用临时表来进行相应的操作了。但是我们可以看到,在 MySQL 利用临时表来完成 DISTINCT 的时候,和处理 GROUP BY 有一点区别,就是少了 filesort。实际上,在 MySQL 的分组算法中,并不一定非要排序才能完成分组操作的,这一点在上面的 GROUP BY 优化小技巧中我已经提到过了。实际上这里 MySQL 正是在没有排序的情况下实现分组最后完成 DISTINCT 操作的,所以少了 filesort 这个排序操作。

4.最后再和 GROUP BY 结合试试看:

?
1
2
3
4
sky@localhost : example 11:05:06> EXPLAIN SELECT DISTINCT max(user_id)
  -> FROM group_message
  -> WHERE group_id > 1 AND group_id < 10
  -> GROUP BY group_idG
?
1
2
3
4
5
6
7
8
9
10
11
12
*************************** 1. row ***************************
      id: 1
 SELECT_type: SIMPLE
    table: group_message
     type: range
possible_keys: idx_gid_uid_gc
     key: idx_gid_uid_gc
   key_len: 4
     ref: NULL
     rows: 32
    Extra: Using WHERE; Using index; Using temporary; Using filesort
1 row in set (0.00 sec)

最后我们再看一下这个和 GROUP BY 一起使用带有聚合函数的示例,和上面第三个示例相比,可以看到已经多了 filesort 排序操作了,正是因为我们使用了 MAX 函数的缘故。要取得分组后的 MAX 值,又无法使用索引完成操作,只能通过排序才行了。

mysql distinct和group by谁更好
1,测试前的准备

?
1
2
3
4
5
6
//准备一张测试表
mysql> CREATE TABLE `test_test` (
 ->  `id` int(11) NOT NULL auto_increment,
 ->  `num` int(11) NOT NULL default '0',
 ->  PRIMARY KEY (`id`)
 -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

  

?
1
Query OK, 0 rows affected (0.05 sec)

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
mysql> delimiter || //改变mysql命令结束符为||
 
//建个储存过程向表中插入10W条数据
mysql> create procedure p_test(pa int(11))
 -> begin
 ->
 -> declare max_num int(11) default 100000;
 -> declare i int default 0;
 -> declare rand_num int;
 ->
 -> select count(id) into max_num from test_test;
 ->
 -> while i < pa do
 ->     if max_num < 100000 then
 ->         select cast(rand()*100 as unsigned) into rand_num;
 ->         insert into test_test(num)values(rand_num);
 ->     end if;
 ->     set i = i +1;
 -> end while;
 -> end||
?
1
Query OK, 0 rows affected (0.00 sec)

 

?
1
mysql> call p_test(100000)||
?
1
Query OK, 1 row affected (5.66 sec)

 

?
1
2
mysql> delimiter ;//改变mysql命令结束符为;
mysql> select count(id) from test_test; //数据都进去了
?
1
2
3
4
5
6
+-----------+
| count(id) |
+-----------+
|  100000 |
+-----------+
1 row in set (0.00 sec)

 

?
1
mysql> show variables like "%pro%";  //查看一下,记录执行的profiling是不是开启动了,默认是不开启的
?
1
2
3
4
5
6
7
8
9
+---------------------------+-------+
| Variable_name       | Value |
+---------------------------+-------+
| profiling         | OFF  |
| profiling_history_size  | 15  |
| protocol_version     | 10  |
| slave_compressed_protocol | OFF  |
+---------------------------+-------+
4 rows in set (0.00 sec)

 

?
1
mysql> set profiling=1;      //开启
?
1
Query OK, 0 rows affected (0.00 sec)

2,测试

?
1
2
3
4
5
//做了4组测试
mysql> select distinct(num) from test_test;
mysql> select num from test_test group by num;
 
mysql> show profiles;  //查看结果
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
+----------+------------+-------------------------------------------+
| Query_ID | Duration  | Query                   |
+----------+------------+-------------------------------------------+
|    1 | 0.07298225 | select distinct(num) from test_test    |
|    2 | 0.07319975 | select num from test_test group by num  |
|    3 | 0.07313525 | select num from test_test group by num  |
|    4 | 0.07317725 | select distinct(num) from test_test    |
|    5 | 0.07275200 | select distinct(num) from test_test    |
|    6 | 0.07298600 | select num from test_test group by num  |
|    7 | 0.07500700 | select num from test_test group by num  |
|    8 | 0.07331325 | select distinct(num) from test_test    |
|    9 | 0.57831575 | create index num_index on test_test (num) | //在这儿的时候,我加了索引
|    10 | 0.00243550 | select distinct(num) from test_test    |
|    11 | 0.00121975 | select num from test_test group by num  |
|    12 | 0.00116550 | select distinct(num) from test_test    |
|    13 | 0.00107650 | select num from test_test group by num  |
+----------+------------+-------------------------------------------+
13 rows in set (0.00 sec)

上面的1-8是4组数据,并且是没有加索引的,从中我们可以看出,distinct比group by 会好一点点
10-13是2组数据,是加了索引以后的,从中我们可以看出,group by 比distinct 会好一点点
一般情况,数据量比较大的表,关联字段都会加索引的,,并且加索引后检索时间只有以前的六分之一左右。

延伸 · 阅读

精彩推荐