欢迎来到天天文库
浏览记录
ID:34724660
大小:180.68 KB
页数:3页
时间:2019-03-10
《mysql的distinct和groupby对比》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、MySQL中distinct和groupby比较之前看了网上的一些测试,感觉不是很准确,今天亲自测试了一番。得出了结论(仅在个人计算机上测试,可能不全面,仅供参考)测试过程:准备一张测试表 1CREATETABLE`test_test`(2`id`int(11)NOTNULLauto_increment, 3`num`int(11)NOTNULLdefault'0', 4PRIMARYKEY(`id`)5)ENGINE=MyISAMDEFAULTCHARSET=utf8AUTO_INCREMENT=1;建个储存过程向表中插入10W条数据 01create
2、procedurep_test(paint(11))02begin03 04declaremax_numint(11)default100000;05declareiintdefault0;06declarerand_numint;07 08selectcount(id)intomax_numfromtest_test;09 10whilei3、rand_num);14endif;15seti=i+1;16endwhile;17end调用存储过程插入数据1callp_test(100000);开始测试:(不加索引)01selectdistinctnumfromtest_test;02selectnumfromtest_testgroupbynum;03 04[SQL]selectdistinctnumfromtest_test;05受影响的行:0 06时间:0.078ms07 08[SQL]09selectnumfromtest_testgroupbynum;10受影响的行:0 11时间:0.04、31ms二、num字段上创建索引 1ALTERTABLE`test_test`ADDINDEX`num_index`(`num`); 再次查询01selectdistinctnumfromtest_test;02selectnumfromtest_testgroupbynum;03[SQL]selectdistinctnumfromtest_test;04受影响的行:0 05时间:0.000ms06 07[SQL]08selectnumfromtest_testgroupbynum;09受影响的行:0 10时间:0.000ms这时候我们发现时间太小了0.5、000秒都无法精确了。 我们转到命令行下测试01mysql>setprofiling=1;02mysql>selectdistinct(num)fromtest_test;03mysql>selectnumfromtest_testgroupbynum;04mysql>showprofiles;05+----------+------------+----------------------------------------+ 066、Query_ID7、Duration8、Query9、 07+----------+------------+--------10、--------------------------------+ 0811、112、0.0007255013、selectdistinct(num)fromtest_test14、 0915、216、0.0007165017、selectnumfromtest_testgroupbynum18、 10+----------+------------+----------------------------------------+
3、rand_num);14endif;15seti=i+1;16endwhile;17end调用存储过程插入数据1callp_test(100000);开始测试:(不加索引)01selectdistinctnumfromtest_test;02selectnumfromtest_testgroupbynum;03 04[SQL]selectdistinctnumfromtest_test;05受影响的行:0 06时间:0.078ms07 08[SQL]09selectnumfromtest_testgroupbynum;10受影响的行:0 11时间:0.0
4、31ms二、num字段上创建索引 1ALTERTABLE`test_test`ADDINDEX`num_index`(`num`); 再次查询01selectdistinctnumfromtest_test;02selectnumfromtest_testgroupbynum;03[SQL]selectdistinctnumfromtest_test;04受影响的行:0 05时间:0.000ms06 07[SQL]08selectnumfromtest_testgroupbynum;09受影响的行:0 10时间:0.000ms这时候我们发现时间太小了0.
5、000秒都无法精确了。 我们转到命令行下测试01mysql>setprofiling=1;02mysql>selectdistinct(num)fromtest_test;03mysql>selectnumfromtest_testgroupbynum;04mysql>showprofiles;05+----------+------------+----------------------------------------+ 06
6、Query_ID
7、Duration
8、Query
9、 07+----------+------------+--------
10、--------------------------------+ 08
11、1
12、0.00072550
13、selectdistinct(num)fromtest_test
14、 09
15、2
16、0.00071650
17、selectnumfromtest_testgroupbynum
18、 10+----------+------------+----------------------------------------+
此文档下载收益归作者所有