国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MySQL中distinct與group by之間的性能進行比較

2024-07-24 12:45:29
字體:
來源:轉載
供稿:網友

最近在網上看到了一些測試,感覺不是很準確,今天親自測試了一番。得出了結論,測試過程在個人計算機上,可能不夠全面,僅供參考。

測試過程:
準備一張測試表 

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

 

二、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

這時候我們發現時間太小了 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 | +----------+------------+----------------------------------------+

 

分析:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辽阳县| 独山县| 贵州省| 永昌县| 维西| 施甸县| 土默特左旗| 高碑店市| 咸宁市| 班戈县| 巴楚县| 巧家县| 昌邑市| 兴安县| 盘山县| 北辰区| 晴隆县| 罗源县| 杭锦旗| 清涧县| 武陟县| 平罗县| 宁德市| 施秉县| 遂宁市| 桃园市| 交口县| 阜城县| 富顺县| 尤溪县| 清镇市| 丹江口市| 保靖县| 蒙山县| 浦东新区| 屯留县| 永春县| 永春县| 清水河县| 蒙阴县| 化州市|