MySQL中distinct和group by性能比较[转]
MySQL中distinct和group by性能比较[转]
之前看了网上的一些测试,感觉不是很准确,今天亲自测试了一番。得出了结论(仅在个人计算机上测试,可能不全面,仅供参考)
测试过程:
准备一张测试表
1 CREATE TABLE `test_test` (
2 `id` int(11) NOT NULL auto_increment,
3 `num` int(11) NOT NULL default '0',
4 PRIMARY KEY (`id`)
5 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
建个储存过程向表中插入10W条数据

01 create procedure p_test(pa int(11))
02 begin
03
04 declare max_num int(11) default 100000;
05 declare i int default 0;
06 declare rand_num int;
07
08 select count(id) into max_num from test_test;
09
10 while i < pa do
11 if max_num < 100000 then
12 select cast(rand()*100 as unsigned) into rand_num;
13 insert into test_test(num)values(rand_num);
14 end if;
15 set i = i +1;
16 end while;
17 end

调用存储过程插入数据
1 call p_test(100000);
开始测试:(不加索引)

01 select distinct num from test_test;
02 select num from test_test group by num;
03
04 [SQL] select distinct num from test_test;
05 受影响的行: 0
06 时间: 0.078ms
07
08 [SQL]
09 select num from test_test group by num;
10 受影响的行: 0
11 时间: 0.031ms

二、num字段上创建索引
1 ALTER TABLE `test_test` ADD INDEX `num_index` (`num`) ;
再次查询

01 select distinct num from test_test;
02 select num from test_test group by num;
03 [SQL] select distinct num from test_test;
04 受影响的行: 0
05 时间: 0.000ms
06
07 [SQL]
08 select num from test_test group by num;
09 受影响的行: 0
10 时间: 0.000ms

这时候我们发现时间太小了 0.000秒都无法精确了。
我们转到命令行下 测试

01 mysql> set profiling=1;
02 mysql> select distinct(num) from test_test;
03 mysql> select num from test_test group by num;
04 mysql> show profiles;
05 +----------+------------+----------------------------------------+
06 | Query_ID | Duration | Query |
07 +----------+------------+----------------------------------------+
08 | 1 | 0.00072550 | select distinct(num) from test_test |
09 | 2 | 0.00071650 | select num from test_test group by num |
10 +----------+------------+----------------------------------------+

加了索引之后 distinct 比没加索引的 distinct 快了 107倍。
加了索引之后 group by 比没加索引的 group by 快了 43倍。
再来对比 :distinct 和 group by
不管是加不加索引 group by 都比 distinct 快。因此使用的时候建议选 group by
MySQL中distinct和group by性能比较[转]的更多相关文章
- mysql下distinct和group by区别对比
在数据表中记录了用户验证时使用的书目,现在想取出所有书目,用DISTINCT和group by都取到了我想要的结果,但我发现返回结果排列不同,distinct会按数据存放顺序一条条显示,而group ...
- mysql distinct跟group by性能
mysql distinct和group by性能 1,测试前的准备 //准备一张测试表 mysql> CREATE TABLE `test_test` ( -> `id` int ...
- mysql中distinct的用法
本事例实验用表task,结构如下 MySQL> desc task; +-------------+------------+------+-----+-------------------+- ...
- 向mysql中批量插入数据的性能分析
MYSQL批量插入数据库实现语句性能分析 假定我们的表结构如下 代码如下 CREATE TABLE example (example_id INT NOT NULL,name VARCHAR( 5 ...
- mysql中distinct
1.Distinct 位置 单独的distinct只能放在开头,否则报错,语法错误,与其他函数使用时候,没有位置限制如下 Select player_id,count(distinct(task_id ...
- mysql中影响myisam引擎写入性能的三项设置
一.LOW_PRIORITY1.对于myisam默认是写操作优先,读操作滞后.通过该项更改,可以使读操作优先,写操作在有空闲的时候再写入.但该项可能在理论上造成,写被永远阻塞. SQL语句中使用示例: ...
- 数据库mysql中distinct关键词
在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值.关键词 distinct用于返回唯一不同的值. 例如,在学生信息表中,你想查询该学校有多少个系.例如, ...
- mysql中百万级别分页查询性能优化
前提条件: 1.表的唯一索引 2.百万级数据 SQL语句: select c.* FROM ( SELECT a.logid FROM tableA a where 1 = 1 <#if pho ...
- mysql 中合并查询结果union用法 or、in与union all 的查询效率
mysql 中合并查询结果union用法 or.in与union all 的查询效率 (2016-05-09 11:18:23) 转载▼ 标签: mysql union or in 分类: mysql ...
随机推荐
- sed 命令及shell的if语句的运用实例
if [ -f /etc/syslog.conf ]; #判断文件是否存在thensystest=`sed -n '/^auth.info/p' /etc/syslog.conf`; ...
- Android IOS WebRTC 音视频开发总结(七五)-- WebRTC视频通信中的错误恢复机制
本文主要介绍WebRTC视频通信中的错误恢复机制(我们翻译和整理的,译者:jiangpeng),最早发表在[这里] 支持原创,转载必须注明出处,欢迎关注我的微信公众号blacker(微信ID:blac ...
- Django 基础教程
Django 基础教程 这是第一篇 Django 简介 » Django 是由 Python 开发的一个免费的开源网站框架,可以用于快速搭建高性能,优雅的网站! 你一定可以学会,Django 很简单 ...
- [转]A Guide To using IMU (Accelerometer and Gyroscope Devices) in Embedded Applications.
原文地址http://www.starlino.com/imu_guide.html Introduction There’s now a FRENCH translation of this art ...
- 如何防止ElasticSearch集群出现脑裂现象(转)
原文:http://xingxiudong.com/2015/01/05/resolve-elasticsearch-split-brain/ 什么是“脑裂”现象? 由于某些节点的失效,部分节点的网络 ...
- windows2008r2环境双实例安装mysql5.6
windows2008r2环境双实例安装mysql5.6 环境:windows2008 r2 标准版 1.默认安装了一个mysql5.6端口为3306 2.使用msi文件安装需要.net4.0支持,安 ...
- MySQL的备份的一些策略和方法的总结
1.一般使用的是mysqldump来进行备份,每次dump的数据是1000条,并且在这个过程中会进行锁表. (这种方式是逻辑备份,即直接将数据库中的数据导成sql语句进行备份的过程) 主要的使用方法: ...
- iOS nib file owner
nib文件中的file owner属性,设定后app在运行时加载nib文件的过程中会通过file owner重新建立nib文件中描述的控件与其在file owner中对应的IBOutlet或IBAct ...
- windows下apache+php+mysql 环境配置方法
一 准备 1 下载apache http://httpd.apache.org/download.cgi#apache24 httpd-2.2.22-win32-x86-openssl-0.9.8t. ...
- Linux下查找文件命令——find
find [在哪个目录下查找] -name <文件名> 1.场景:当你知道了某个文件的文件名,而不知道这个文件放到哪个文件夹,甚至是层层套嵌的文件夹里, 也可以使用find命令来查找,如: ...