hive调优 ------- 竖表变横表

有这这样一张表t_buy_buyer_time_hongbao_asc

 

 

用户id  次序        购买时间

25560   1       1325345254     

25560   2       1331043510     

25560   3       1331999999     

25720   1       1320381121     

25720   2       1320461154     

25720   3       1320639271     

26840   1       1337214675    

26840   2       1337214694     

26840   3       1337214768    

37160   1       1328583075

 

需求是在某张表中罗列出某用户的第一次购买时间,第二次购买时间,第三次购买时间

比如

用户id       第一次购买         第二次购买           第三次购买

25560   1325345254    1331043510     1331999999  

25720   1320381121    1320461154     1320639271     

26840   1337214675    1337214694     1337214768    

......

 

 

于是呢 打一个很形象的比方就是 把竖表变横表的要求

 

使用两种hive脚本来查询hive1

Sql代码   收藏代码
  1. select   
  2.         tb1.uid as uid,   
  3.         tb1.order_time as s1t_deal_time,   
  4.         tb2.order_time as c2d_deal_time,   
  5.         tb3.order_time as r3d_deal_time  
  6. from   
  7.                 (select * from t_buy_buyer_time_hongbao_asc where row_num=1 and pt='20121010000000')tb1    
  8.         left outer join   
  9.         (select * from t_buy_buyer_time_hongbao_asc where row_num=2 and pt='20121010000000')tb2    
  10.         on tb1.uid=tb2.uid   
  11.         left outer join   
  12.         (select * from t_buy_buyer_time_hongbao_asc where row_num=3 and pt='20121010000000')tb3    
  13.         on tb1.uid=tb3.uid   

本hive脚本只需要一个job,执行时间376.005 s 

 

 

hive2

Sql代码   收藏代码
  1. select   
  2.         tb1.uid as uid,   
  3.         s1t_deal_time,   
  4.         c2d_deal_time,   
  5.         r3d_deal_time  
  6. from   
  7.         (select uid,sum(if(row_num=1,order_time,0)) as s1t_deal_time,sum(if(row_num=2,order_time,0)) as c2d_deal_time,sum(if(row_num=3,order_time,0)) as r3d_deal_time from t_buy_buyer_time_hongbao_asc where pt='20121010000000' group by uid)tb1  

本hive脚本也只需要一个job,执行时间是328.733 s

额,不要嫌慢,在hadoop上跑数据的确是很慢很慢的

 

 

其实执行效率被优化了五十多s,但疑问是为什么hive1会只生成一个job呢?原因在于我们的连接条件是同一张表的同一个uid,于是呢,hive会做一个效率优化。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值