Hive優化(SQL)

Hive優化(SQL)
1、where語句優化
select m.cid,u.id from order m join customer u on( m.cid =u.id )where m.dt=‘20180808’;
可優化爲
select m.cid,u.id from (select * from order where dt=‘20180818’) m join customer u on( m.cid =u.id);
2、union優化
儘量不要使用union (union 去掉重複的記錄)而是使用 union all 然後在用group by 去重
3、count distinct優化
不要使用count (distinct cloumn) ,而要使用子查詢實現count(distinct)
select count(1) from (select id from tablename group by id) tmp;
4、如果需要根據一張表的字段約束另一個張表,用in代替join
select id,name from tb1 a join tb2 b on(a.id = b.id);
可優化爲
select id,name from tb1 where id in(select id from tb2); in 要比join 快
5、消滅子查詢內的 group by 、 COUNT(DISTINCT),MAX,MIN。 可以減少job的數量。
6、join優化
map端join
set hive.auto.convert.join = true; 默認爲true
set hive.mapjoin.smalltable.filesize=25000000; 設置小表的閾值
7、本地模式
當 Hive 查詢處理的數據量比較小時,其實沒有必要啓動分佈式模式去執行,因爲以分佈式方式執行就涉及到跨網絡傳輸、多節點協調 等,並且消耗資源。這個時間可以只使用本地模式來執行 mapreduce job,只在一臺機器上執行,速度會很快
set hive.exec.mode.local.auto=true 是打開 hive 自動判斷是否啓動本地模式的開關,但是隻 是打開這個參數並不能保證啓動本地模式,要當 map 任務數不超過
hive.exec.mode.local.auto.input.files.max 的個數並且 map 輸入文件大小不超過
hive.exec.mode.local.auto.inputbytes.max 所指定的大小時,才能啓動本地模式。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章