SQL/HQL中数据去重的3种方式

30万条数据的去重比较

1.distinct

select distinct * from tableName

hive用时: 40.47秒
impala用时: 11.98秒

2.group by

select c1,c2,c3,c4,c5,max(c6) c6
from tableName
group by c1,c2,c3,c4,c5

hive用时: 22.8秒
impala用时: 2.4秒

3.窗口函数( 这里选用:row_number()over() )

select * from 
	(select c1,c2,c3,c4,c5,c6,
		row_number()over(partition by c1,c2 order by c6 desc) ranking
	from tableName) tmp
where ranking = 1

hive用时: 21.41秒
impala用时: 2.5秒

总结:

distinct 效率最低,不建议使用;
group by 和 窗口函数 的去重效率高,用时差不多,根据实际情况使用.

猜你喜欢

转载自blog.csdn.net/Thomson617/article/details/89145724