实测distinct group和GROUP BY的区别

MySQL中distinct与group by之间的性能进行比较
投稿:lijiao
字体:[ ] 类型:转载 时间:
这篇文章主要针对MySQL中distinct与group by之间的性能进行比较,内容比较详细,很直观的能看出比较结果,感兴趣的小伙伴们可以参考一下
最近在网上看到了一些测试,感觉不是很准确,今天亲自测试了一番。得出了结论,测试过程在个人计算机上,可能不够全面,仅供参考。
测试过程:
准备一张测试表&
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 ;
建个储存过程向表中插入10W条数据&&&&
create procedure p_test(pa int(11))
declare max_num int(11) default 100000;
declare i int default 0;
declare rand_
select count(id) into max_num from test_
while i & pa do
if max_num & 100000 then
select cast(rand()*100 as unsigned) into rand_
insert into test_test(num)values(rand_num);
set i = i +1;
调用存储过程插入数据
call p_test(100000);
开始测试:(不加索引)
select distinct num from test_
select num from test_
[SQL] select distinct num from test_
受影响的行: 0
时间: 0.078ms
select num from test_
受影响的行: 0
时间: 0.031ms
二、num字段上创建索引
ALTER TABLE `test_test` ADD INDEX `num_index` (`num`) ;
再次查询&&&
select distinct num from test_
select num from test_
[SQL] select distinct num from test_
受影响的行: 0
时间: 0.000ms
select num from test_
受影响的行: 0
时间: 0.000ms
这时候我们发现时间太小了 0.000秒都无法精确了。
我们转到命令行下测试
mysql& set profiling=1;
mysql& select distinct(num) from test_
mysql& select num from test_
+----------+------------+----------------------------------------+
| Query_ID | Duration | Query
+----------+------------+----------------------------------------+
1 | 0. | select distinct(num) from test_test |
2 | 0. | select num from test_test group by num |
+----------+------------+----------------------------------------+
加了索引之后 distinct 比没加索引的distinct 快了107倍。
加了索引之后 group by 比没加索引的group by 快了43倍。
再来对比 :distinct&和group by
不管是加不加索引group by 都比distinct 快。
因此使用的时候建议选 group by。
以上就是在MySQL中distinct与group by之间的性能进行比较的,通过以上比较是不是对distinct和group by有了更深入的了解,希望对大家的学习有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具博客访问: 1936
博文数量: 1
注册时间:
ITPUB论坛APP
ITPUB论坛APP
APP发帖 享双倍积分
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
给主人留下些什么吧!~~
请登录后留言。<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&

我要回帖

更多关于 linq distinctby 的文章

 

随机推荐