天天看點

hive中distinct和group by優化

1、避免使用count distinct ,容易引起性能問題

select distinct(user_id) from a ;

由于必須去重,是以Hive會把map階段的輸出全部分布到一個reduce task中,容易引起性能問題,可以通過先group by ,再count得方式進行優化

優化後:select count(*)

from(

select user_id from a group by user_id

)tmp

2、group by引起得傾斜

比如按照銷售商都銷售明細表來進行統計訂單數,那麼部分大供應商的訂單量就非常多,而多數供應商得訂單量就一般,由于Group by 得時候是按照供貨商得Id分發到每個reduce task,那麼配置設定到大供應商得reduce task就配置設定了更多得訂單,進而導緻資料傾斜。

優化措施:set hive.map.aggr = true

set hive.groupby.skewindata=true

hive中distinct和group by優化

繼續閱讀