饿虎岗资源网 Design By www.oxmxm.com

最近在网上看到了一些测试,感觉不是很准确,今天亲自测试了一番。得出了结论,测试过程在个人计算机上,可能不够全面,仅供参考。

测试过程:

准备一张测试表 

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

调用存储过程插入数据

call p_test(100000);

开始测试:(不加索引)

 select distinct num from test_test; 
 select num from test_test group by num; 
 
 [SQL] select distinct num from test_test;
 受影响的行: 0
 时间: 0.078ms
 
 [SQL] 
 select num from test_test group by num;
 受影响的行: 0
 时间: 0.031ms

MySQL中distinct与group by之间的性能进行比较

二、num字段上创建索引

ALTER TABLE `test_test` ADD INDEX `num_index` (`num`) ;

再次查询   

 select distinct num from test_test; 
 select num from test_test group by num; 
 [SQL] select distinct num from test_test;
 受影响的行: 0
 时间: 0.000ms
 
 [SQL] 
 select num from test_test group by num;
 受影响的行: 0
 时间: 0.000ms

MySQL中distinct与group by之间的性能进行比较

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

 mysql> set profiling=1;
 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.00072550 | select distinct(num) from test_test |
 | 2 | 0.00071650 | select num from test_test group by num |
 +----------+------------+----------------------------------------+

MySQL中distinct与group by之间的性能进行比较

分析:

加了索引之后 distinct 比没加索引的distinct 快了107倍。
加了索引之后 group by 比没加索引的group by 快了43倍。
再来对比 :distinct 和group by
不管是加不加索引group by 都比distinct 快。

因此使用的时候建议选 group by。

以上就是在MySQL中distinct与group by之间的性能进行比较的,通过以上比较是不是对distinct和group by有了更深入的了解,希望对大家的学习有所帮助。

饿虎岗资源网 Design By www.oxmxm.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
饿虎岗资源网 Design By www.oxmxm.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。

更新日志