mysql distinct跟group by性能
- //准备一张测试表
- 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)
//准备一张测试表
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)
//做了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 distinct跟group by性能的更多相关文章
- MySQL中distinct和group by性能比较[转]
MySQL中distinct和group by性能比较[转] 之前看了网上的一些测试,感觉不是很准确,今天亲自测试了一番.得出了结论(仅在个人计算机上测试,可能不全面,仅供参考) 测试过程: 准备一张 ...
- Mysql distinct、group by
具体业务场景:根据某些字段组合去重得到所有字段结果. 遇到的error:sql_mode=only_full_group_by. 原因是mysql配置问题. distinct: distinct这个关 ...
- mysql下distinct和group by区别对比
在数据表中记录了用户验证时使用的书目,现在想取出所有书目,用DISTINCT和group by都取到了我想要的结果,但我发现返回结果排列不同,distinct会按数据存放顺序一条条显示,而group ...
- 44 答疑(三)--join的写法/Simple nested loop join的性能问题/Distinct和group by的性能/备库自增主键问题
44 答疑(三) Join的写法 35节介绍了join执行顺序,加了straight_join,两个问题: --1 如果用left join,左边的表一定是驱动表吗 --2 如果两个表的join包含多 ...
- distinct和group by的性能比较
distinct和group by的性能比较 当去重复的字段 的个数比较多的时候,group by 比distinct要快很多 当去重复的字符 的个数比较少的时候,distinct 比group by ...
- mysql数据去重复distinct、group by
使用distinct 和group by都可以实现数据去重. select distinct 字段 group by 一般放在where条件后
- Mysql视图的作用及其性能分析
定义:视图是从一个或几个基本表导出的表,它与基本表不同,是一个虚表. 作用: 1.简化操作,不用进行多表查询. 2.当不同种类的用用户共享同一个数据库时,非常灵活,(用户以不同的 方式看待同一数据. ...
- 高性能mysql 第6章 查询性能优化
查询缓存: 在解析一个sql之前,如果查询缓存是打开的,mysql会去检查这个查询(根据sql的hash作为key)是否存在缓存中,如果命中的话,那么这个sql将会在解析,生成执行计划之前返回结果. ...
- MYSQL索引结构原理、性能分析与优化
[转]MYSQL索引结构原理.性能分析与优化 第一部分:基础知识 索引 官方介绍索引是帮助MySQL高效获取数据的数据结构.笔者理解索引相当于一本书的目录,通过目录就知道要的资料在哪里, 不用一页一页 ...
随机推荐
- MYSQL使用二进制日志来恢复数据
mysqlbinlog工具的使用,大家可以看MySQL的帮助手册.里面有详细的用, 在这个例子中,重点是--start-position参数和--stop-position参数的使用. ·--star ...
- android 打包签名
1.Eclipse工程中右键工程,弹出选项中选择 android工具 → 生成签名应用包 2.选择需要打包的android项目工程 3.如果已有私钥文件,选择私钥文件 输入密码,如果没有私钥文件见第6 ...
- android中GridView关于间距的属性值介绍
android:columnWidth 设置列的宽度.关联的方法为:setColumnWidth(int) stretchMode属性值的作用是设置GridView中的条目以什么缩放模式去填充空间 ...
- log4j和web.xml配置webAppRootKey 的问题
在tomcat下部署两个或多个项目时,web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为“webapp.root”,如下: <!-- 应用路径 --> &l ...
- tpl + ccr
不是非此即彼的场景.如下混合使用CCR+TPL的代码说明问题:It's not an either/or scenario.You can intermix CCR and TPL code like ...
- LightOJ 1422 Halloween Costumes
dp[i]][j]=min(dp[i+1][j]+1,dp[i+1][k-1]+dp[k][j]) 表示第i天到j的最小数量.如果第i天的衣服只自己穿的话,不考虑后面的就是dp[i][j]=dp[i+ ...
- Linode各机房在中国访问速度性能测试
最近因为google的各种被X的原因,想自己弄个VPS玩玩,比来比去都推荐linode. 因为各种性能测试工具都不靠谱,还是自己机器来的直接,虽然笨拙但是真实可信. 从测试结果上看,明显东京机房的速度 ...
- [原创]git使用入门
创建git项目并初始化 建立一个新文件夹,然后将该文件夹定义为git项目 Lilis-MacBook-Pro:GitDir lili$ mkdir testgit Lilis-MacBook-Pro: ...
- 【Zookeeper学习】Apache Zookeeper项目简介
正在撰写,稍后来访……
- HDU-4611 Balls Rearrangement 循环节,模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4611 先求出循环节,然后比较A和B的大小模拟过去... //STATUS:C++_AC_15MS_43 ...