轉(zhuǎn)載請在文章起始處注明出處睛廊,謝謝滋恬。
Hive是將符合SQL語法的字符串解析生成可以在Hadoop上執(zhí)行的MapReduce的工具浅乔。使用Hive盡量按照分布式計算的一些特點來設(shè)計sql挑围,和傳統(tǒng)關(guān)系型數(shù)據(jù)庫有區(qū)別礁竞,所以需要去掉原有關(guān)系型數(shù)據(jù)庫下開發(fā)的一些固有思維。
基本原則:
1杉辙、盡量盡早地過濾數(shù)據(jù)模捂,減少每個階段的數(shù)據(jù)量,對于分區(qū)表要加分區(qū),同時只選擇需要使用到的字段
select ...
from A join B
on A.key = B.key
where A.userid > 10
and B.userid < 10
and A.dt = '2018-05-17'
and B.dt = '2018-05-17';
改寫為:
select ....
from
( select ....
from A
where dt='2018-05-17'
and userid > 10
) a
join
( select ....
from B
where dt = '2018-05-17'
and userid < 10
) b
on a.key = b.key;
2、分區(qū)表統(tǒng)計盡量使用分區(qū)
3狂男、盡量避免一個SQL包含復(fù)雜邏輯
建議盡量使用中間表來完成復(fù)雜的邏輯處理综看。
4 、join操作
小表要注意放在join的左邊(關(guān)系型數(shù)據(jù)庫查詢中里面很多都小表放在join的右邊)岖食,否則會引起磁盤和內(nèi)存的大量消耗
5红碑、如果union all的部分個數(shù)大于2,或者每個union部分?jǐn)?shù)據(jù)量大泡垃,應(yīng)該拆成多個insert into 語句析珊,實際測試過程中,執(zhí)行時間能提升50%
舉例:
insert overwrite table tablename partition (dt= ....)
select ..... from (
select ... from A
union all
select ... from B
union all
select ... from C
) t01
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 ...;
6蔑穴、寫SQL要先了解數(shù)據(jù)本身的特點忠寻,如果有join ,group操作的話,要注意是否會有數(shù)據(jù)傾斜
如果出現(xiàn)數(shù)據(jù)傾斜存和,應(yīng)當(dāng)做如下處理:
set hive.exec.reducers.max=200;
set mapred.reduce.tasks = 200;---增大Reduce個數(shù)
set hive.groupby.mapaggr.checkinterval=100000 ;--這個是group的鍵對應(yīng)的記錄條數(shù)超過這個值則會進行分拆,值根據(jù)具體數(shù)據(jù)量設(shè)置
set hive.groupby.skewindata=true; --如果是group by過程出現(xiàn)傾斜 應(yīng)該設(shè)置為true
set hive.skewjoin.key=100000; --這個是join的鍵對應(yīng)的記錄條數(shù)超過這個值則會進行分拆,值根據(jù)具體數(shù)據(jù)量設(shè)置
set hive.optimize.skewjoin=true;--如果是join 過程出現(xiàn)傾斜 應(yīng)該設(shè)置為true
PS:Hive優(yōu)化參數(shù)(寫在執(zhí)行語句前)
set hive.input.format = org.apache.hadoop.hive.ql.io.CombineHiveInputFormat;
set hive.hadoop.supports.splittable.combineinputformat=true;
set mapreduce.input.fileinputformat.split.maxsize = 256000000;
set mapreduce.input.fileinputformat.split.minsize.per.node = 128000000;
set mapreduce.input.fileinputformat.split.minsize.per.rack = 128000000;
set hive.merge.mapfiles = true;
set hive.merge.mapredfiles = true;
set hive.merge.size.per.task = 256000000;
set hive.merge.smallfiles.avgsize = 256000000;
set hive.exec.parallel = true;