今天无意中听到有同事在讨论,distinct和group by谁的速度会更快一点,意件不一,其实我也不知道那个好,下午有时间做了一下测试。

1,测试前的准备

//准备一张测试表

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 ;

Query OK, 0 rows affected (0.05 sec)

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||

Query OK, 0 rows affected (0.00 sec)

mysql> call p_test(100000)||

Query OK, 1 row affected (5.66 sec)

mysql> delimiter ;//改变mysql命令结束符为;

mysql> select count(id) from test_test; //数据都进去了

+-----------+

| count(id) |

+-----------+

| 100000 |

+-----------+

1 row in set (0.00 sec)

mysql> show variables like "%pro%"; //查看一下,记录执行的profiling是不是开启动了,默认是不开启的

+---------------------------+-------+

| Variable_name | Value |

+---------------------------+-------+

| profiling | OFF |

| profiling_history_size | 15 |

| protocol_version | 10 |

| slave_compressed_protocol | OFF |

+---------------------------+-------+

4 rows in set (0.00 sec)

mysql> set profiling=1; //开启

Query OK, 0 rows affected (0.00 sec)

2,测试

//做了4组测试

mysql> select distinct(num) from test_test;

mysql> select num from test_test group by num;

mysql> show profiles; //查看结果

+----------+------------+-------------------------------------------+

| 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 会好一点点

一般情况,数据量比较大的表,关联字段都会加索引的,,并且加索引后检索时间只有以前的六分之一左右。

mysql 去重 性能比较_mysql 去重方法distinct 与 group by 性能比较 | 学步园相关推荐

  1. mysql 正则截取字符串_mysql字符串查找截取与正则表达式的联合应用 | 学步园

    /* 判断字符串里的内容是否是数值类型 **************************************************** is_double 输入参数: str:       ...

  2. distinct mysql性能_MySQL中distinct和group by性能比较

    MySQL中distinct和group by性能比较[转] 之前看了网上的一些测试,感觉不是很准确,今天亲自测试了一番.得出了结论(仅在个人计算机上测试,可能不全面,仅供参考) 测试过程: 准备一张 ...

  3. MySQL中distinct和group by性能比较

    MySQL中distinct和group by性能比较[转] 之前看了网上的一些测试,感觉不是很准确,今天亲自测试了一番.得出了结论(仅在个人计算机上测试,可能不全面,仅供参考) 测试过程: 准备一张 ...

  4. mysql数据聚合技术_Mysql 去重 聚合

    示例数据表中的数据: mysql> select * from talk_test; +----+-------+--------+ | id | name  | mobile | +----+ ...

  5. php数据group去重,MongoDB_Mongodb聚合函数count、distinct、group如何实现数据聚合操作, 上篇文章给大家介绍了Mong - phpStudy...

    Mongodb聚合函数count.distinct.group如何实现数据聚合操作 上篇文章给大家介绍了Mongodb中MapReduce实现数据聚合方法详解,我们提到过Mongodb中进行数据聚合操 ...

  6. mysql+distinct+max_MySQL 中 distinct 和 group by 性能比较-Fun言

    看了网上的一些测试,感觉不是很准确,今天亲自测试了一番.得出了结论 仅在个人计算机上测试,可能不全面,仅供参考 测试过程 准备一张测试表 CREATE TABLE `test_test` ( `id` ...

  7. MySQL中distinct和group by性能比较[转]

    之前看了网上的一些测试,感觉不是很准确,今天亲自测试了一番.得出了结论(仅在个人计算机上测试,可能不全面,仅供参考) 测试过程: 准备一张测试表 1 CREATE TABLE `test_test` ...

  8. Mysql中去重的语法_MySQL去重distinct

    去重 在MySQL中需要查询表中不重复的记录时,可以使用distinct关键字过滤重复记录. 语法: select distinct [,...,] from ; 数据表如下: mysql> s ...

  9. mysql 去重取最大值_mysql去重取最大值,逻辑类似oracle的over(partition by)函数

    像下表一样的数据,有重复的合同号,但是我只想保留同一合同号中回款金额最大的那一行,也就是图中红框里的数据. oracle方法: 在oracle中,我们可以简单地用over(partition by)函 ...

  10. mysql去重求次数_Mysql 去重

    需要查询不重复的记录值 例如有如下表结构和值 table fid name sex 1 a  男 2      b    男 3      c    女 4      d    女 5      a  ...

最新文章

  1. 数据挖掘-聚类分析(Python实现K-Means算法)
  2. native版本 修改项目react_react native项目改名(仅针对android)
  3. esp32搭建文件服务器,ESP32入门示例 - SD卡Web服务器
  4. java vector_Java Vector size()方法与示例
  5. 百度优化有感,原创内容只是个传说
  6. Overleaf 显示中文
  7. 小鹏N5申报图曝光 搭载155KW电机、NEDC 600公里与P5相同
  8. bootstrap入门
  9. 前端传递数据超过2M不能传给后台
  10. 【转】NHibernate集合映射中的set, list, map, bag, array
  11. Tomcat压缩传输设置
  12. Codeforces Round #753 (Div. 3) C. Minimum Extraction(最小抽离)
  13. 二分图匹配匈牙利算法BFS实现
  14. 阶段3 2.Spring_10.Spring中事务控制_10spring编程式事务控制2-了解
  15. idea 2017 常用图标
  16. Notepad++--常用的插件
  17. 超干货|城市信息模型介绍CIM1-CIM7级
  18. 算法 64式 7、搜索算法整理_第1部分_1到15题
  19. dongle 工具 蓝牙_bluetooth USB Dongle(蓝牙适配器)怎么用?
  20. 如何将SNS光纤交换机(OEM博科FC交换机)恢复为出厂设置

热门文章

  1. Xshell中文乱码怎么处理?
  2. 电商数据之战背后利益纠葛:触动最敏感神经
  3. 如何使用Orchard搭建敏捷个人的网站(2)
  4. Echarts 全属性 宝典
  5. 发现电脑上装着liteide,就用golang做一个TCP通讯测试(支持先启动client端和断线重连)...
  6. 通读cheerio API-网络爬虫
  7. 查询同一组的最大最小值及明细
  8. [leetcode] Excel Sheet Column Title
  9. java学习:jdbc连接示例
  10. metacube 链接 mysql_2019 年 5月 随笔档案 - rgqancy - 博客园