Hive是將符合SQL語法的字符串解析生成可以在Hadoop上執(zhí)行的MapReduce的工具。
使用Hive盡量按照分布式計算的一些特點來設計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='20120417'
?????? ?and B.dt='20120417';
應該改寫為:
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:盡量原子化操作,盡量避免一個SQL包含復雜邏輯
可以使用中間表來完成復雜的邏輯
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:單個SQL所起的JOB個數(shù)盡量控制在5個以下
?
4:慎重使用mapjoin,一般行數(shù)小于2000行,大小小于1M(擴容后可以適當放大)的表才能使用,小表要注意放在join的左邊(目前TCL里面很多都小表放在join的右邊)。
否則會引起磁盤和內(nèi)存的大量消耗
?
5:寫SQL要先了解數(shù)據(jù)本身的特點,如果有join ,group操作的話,要注意是否會有數(shù)據(jù)傾斜
如果出現(xiàn)數(shù)據(jù)傾斜,應當做如下處理:
set hive.exec.reducers.max=200;
set mapred.reduce.tasks= 200;---增大Reduce個數(shù)
set hive.groupby.mapaggr.checkinterval=100000 ;--這個是group的鍵對應的記錄條數(shù)超過這個值則會進行分拆,值根據(jù)具體數(shù)據(jù)量設置
set hive.groupby.skewindata=true; --如果是group by過程出現(xiàn)傾斜 應該設置為true
set hive.skewjoin.key=100000; --這個是join的鍵對應的記錄條數(shù)超過這個值則會進行分拆,值根據(jù)具體數(shù)據(jù)量設置
set hive.optimize.skewjoin=true;--如果是join 過程出現(xiàn)傾斜 應該設置為true
?
6:如果union all的部分個數(shù)大于2,或者每個union部分數(shù)據(jù)量大,應該拆成多個insert into 語句,實際測試過程中,執(zhí)行時間能提升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 ...;
?
原文地址:http://hbase.iteye.com/blog/1488745
更多文章、技術(shù)交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
