HiveSql(1)mapjoin、分割槽表
mapjoin、分割槽表、不等值匹配
業務需求:
A表,小表,窮舉的100個動漫電影的IP,表結構ip(id string, key string)
B表,大表,每天的交易資料,上億條,表結構order(ds string, item_titlestring,buyer_id string, fee string)
現在想要得到這100個動漫電影相關產品的每天的銷售額、購買人數。
SQL:
1、建表
create table if not exists jieguo
(
ds string,
fee string ,
cnt string
)
partitioned by (pt string )
2、跑資料
Insert overwrite table jieguo partition (pt=201503)
Select /*+MAPJOIN(a)*/
b.ds,sum(b.fee),count(b.buyer_id)
from ip b
Join
(Select ds, fee,buyer_id
From order
Where ds>=20150101 and ds<=20150331
)b
On instr(b.item_title,a.key)>0
Group by b.ds
學習要點:
1、/*+MAPJOIN*/的用法
如果關聯的2張表大小差距懸殊或者進行不等值連線時,一般的join會導致執行速度很慢,使用mapjoin可以有效提高效率
使用場景:
(1)關聯操作中的一張表非常小
(2)不等值連線
Select /*+MAPJOIN(a)*/,括號里加的是小表的名稱
2、分割槽表
如果輸出的資料比較多,可以採取分割槽的形式
建表時加一條語句partitioned by
跑資料時Insert overwrite table jieguo partition (pt=201503)