hive優(yōu)化

1.Hive自己如何確定reduce數(shù):

reduce個(gè)數(shù)的設(shè)定極大影響任務(wù)執(zhí)行效率,不指定reduce個(gè)數(shù)的情況下,hive會(huì)猜測確定一個(gè)reduce個(gè)數(shù),基于以下兩個(gè)設(shè)定:

hive.exec.reducers.bytes.per.reducer(每個(gè)reduce任務(wù)處理的數(shù)據(jù)量,默認(rèn)為1000^3=1G)

hive.exec.reducers.max(每個(gè)任務(wù)最大的reduce數(shù),默認(rèn)為999)

計(jì)算reducer數(shù)的公式很簡單N=min(參數(shù)2,總輸入數(shù)據(jù)量/參數(shù)1)

即,如果reduce的輸入(map的輸出)總大小不超過1G,那么只會(huì)有一個(gè)reduce任務(wù);

如:select pt,count(1) from popt_tbaccountcopy_mes where pt = '2012-07-04' group by pt;

/group/p_sdo_data/p_sdo_data_etl/pt/popt_tbaccountcopy_mes/pt=2012-07-04 總大小為9G多,因此這句有10個(gè)reduce

2. 調(diào)整reduce個(gè)數(shù)方法一:

調(diào)整hive.exec.reducers.bytes.per.reducer參數(shù)的值;

set hive.exec.reducers.bytes.per.reducer=500000000; (500M)

select pt,count(1) from popt_tbaccountcopy_mes where pt = '2012-07-04' group by pt; 這次有20個(gè)reduce

3. 調(diào)整reduce個(gè)數(shù)方法二;

set mapred.reduce.tasks = 15;

select pt,count(1) from popt_tbaccountcopy_mes where pt = '2012-07-04' group by pt;這次有15個(gè)reduce

4. reduce個(gè)數(shù)并不是越多越好;

同map一樣,啟動(dòng)和初始化reduce也會(huì)消耗時(shí)間和資源;

另外,有多少個(gè)reduce,就會(huì)有多少個(gè)輸出文件,如果生成了很多個(gè)小文件,那么如果這些小文件作為下一個(gè)任務(wù)的輸入,則也會(huì)出現(xiàn)小文件過多的問題;

5. 什么情況下只有一個(gè)reduce;

很多時(shí)候你會(huì)發(fā)現(xiàn)任務(wù)中不管數(shù)據(jù)量多大,不管你有沒有設(shè)置調(diào)整reduce個(gè)數(shù)的參數(shù),任務(wù)中一直都只有一個(gè)reduce任務(wù);

其實(shí)只有一個(gè)reduce任務(wù)的情況,除了數(shù)據(jù)量小于hive.exec.reducers.bytes.per.reducer參數(shù)值的情況外,還有以下原因:

a) 沒有g(shù)roup by的匯總,比如把select pt,count(1) from popt_tbaccountcopy_mes where pt = '2012-07-04' group by pt; 寫成 select count(1) from popt_tbaccountcopy_mes where pt = '2012-07-04';

這點(diǎn)非常常見,希望大家盡量改寫。

b) 用了Order by

c) 有笛卡爾積

通常這些情況下,除了找辦法來變通和避免,我暫時(shí)沒有什么好的辦法,因?yàn)檫@些操作都是全局的,所以Hadoop不得不用一個(gè)reduce去完成;

同樣的,在設(shè)置reduce個(gè)數(shù)的時(shí)候也需要考慮這兩個(gè)原則:使大數(shù)據(jù)量利用合適的reduce數(shù);使單個(gè)reduce任務(wù)處理合適的數(shù)據(jù)量;

Hive是將符合SQL語法的字符串解析生成可以在hadoop上執(zhí)行的MapReduce的工具。

使用Hive盡量按照分布式計(jì)算的一些特點(diǎn)來設(shè)計(jì)sql,和傳統(tǒng)關(guān)系型數(shù)據(jù)庫有區(qū)別,

所以需要去掉原有關(guān)系型數(shù)據(jù)庫下開發(fā)的一些固有思維。

基本原則:

1:盡量盡早地過濾數(shù)據(jù),減少每個(gè)階段的數(shù)據(jù)量,對于分區(qū)表要加分區(qū),同時(shí)只選擇需要使用到的字段

select ... from A

join B

on A.key = B.key

where A.userid>10

and B.userid<10

and A.dt='20120417'

and B.dt='20120417';

應(yīng)該改寫為:

select .... from (select .... from A

where dt='201200417'

and userid>10

) a

join ( select .... from B

where dt='201200417'

and userid < 10

) b

on a.key = b.key;

2:盡量原子化操作,盡量避免一個(gè)SQL包含復(fù)雜邏輯

可以使用中間表來完成復(fù)雜的邏輯

drop table if exists tmp_table_1;

create table if not exists tmp_table_1 as

select ......;

drop table if exists tmp_table_2;

create table if not exists tmp_table_2 as

select ......;

drop table if exists result_table;

create table if not exists result_table as

select ......;

drop table if exists tmp_table_1;

drop table if exists tmp_table_2;

3:單個(gè)SQL所起的JOB個(gè)數(shù)盡量控制在5個(gè)以下

4:慎重使用mapjoin,一般行數(shù)小于2000行,大小小于1M(擴(kuò)容后可以適當(dāng)放大)的表才能使用,小表要注意放在join的左邊(目前TCL里面很多都小表放在join的右邊)。

否則會(huì)引起磁盤和內(nèi)存的大量消耗

5:寫SQL要先了解數(shù)據(jù)本身的特點(diǎn),如果有join ,group操作的話,要注意是否會(huì)有數(shù)據(jù)傾斜

如果出現(xiàn)數(shù)據(jù)傾斜,應(yīng)當(dāng)做如下處理:

set hive.exec.reducers.max=200;

set mapred.reduce.tasks= 200;---增大Reduce個(gè)數(shù)

set hive.groupby.mapaggr.checkinterval=100000 ;--這個(gè)是group的鍵對應(yīng)的記錄條數(shù)超過這個(gè)值則會(huì)進(jìn)行分拆,值根據(jù)具體數(shù)據(jù)量設(shè)置

set hive.groupby.skewindata=true; --如果是group by過程出現(xiàn)傾斜 應(yīng)該設(shè)置為true

set hive.skewjoin.key=100000; --這個(gè)是join的鍵對應(yīng)的記錄條數(shù)超過這個(gè)值則會(huì)進(jìn)行分拆,值根據(jù)具體數(shù)據(jù)量設(shè)置

set hive.optimize.skewjoin=true;--如果是join 過程出現(xiàn)傾斜 應(yīng)該設(shè)置為true

6:如果union all的部分個(gè)數(shù)大于2,或者每個(gè)union部分?jǐn)?shù)據(jù)量大,應(yīng)該拆成多個(gè)insert into 語句,實(shí)際測試過程中,執(zhí)行時(shí)間能提升50%

insert overwite table tablename partition (dt= ....)

select ..... from (

select ... from A

union all

select ... from B

union all

select ... from C

) R

where ...;

可以改寫為:

insert into table tablename partition (dt= ....)

select .... from A

WHERE ...;

insert into table tablename partition (dt= ....)

select .... from B

WHERE ...;

insert into table tablename partition (dt= ....)

select .... from C

WHERE ...;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容