MySQL中distinct和group by性能比较

一、不加索引

select distinct num from test_test;
时间: 0.078ms

select num from test_test group by num;
时间: 0.031ms

二、加上索引

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

再次查询

select distinct num from test_test;
时间: 0.000ms

select num from test_test group by num;
时间: 0.000ms

三、结论

不管是加不加索引 group by 都比 distinct 快。因此使用的时候建议选 group by
加了索引之后 distinct 比没加索引的 distinct 快了 107倍。
加了索引之后 group by 比没加索引的 group by 快了 43倍。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值