单列去重
select distinct num from test_test;
select num from test_test group by num;
多列去重
select distinct id,num from test_test;
select id,num from test_test group by id,num;
最后得到结果都是一样的,下面讨论一下效率的比较,
先了解一个概念,sql的聚合
group by使用了聚合,所以它只计算了已经聚合后的数据
而distinct不使用聚合,使用索引去快速遍历数据
由上可知
若重复的数据量很少,且有索引的情况下distinct效率要高,
若重复的数据量很大,则group by的效率要高很多。
SQL中groupby和distinct的区别
最新推荐文章于 2024-04-07 17:30:56 发布