6.Hive优化-SQL优化

6.Hive优化-SQL优化

Hive是将符合SQL语法的字符串解析生成可以在Hadoop上执行的MapReduce的工具。使用Hive尽量按照分布式计算的一些特点来设计sql,和传统关系型数据库有区别,

6.1 减少查询范围

所以需要去掉原有关系型数据库下开发的一些固有思维。

基本原则:

尽量尽早地过滤数据,减少每个阶段的数据量,对于分区表要加分区,同时只选择需要使用到的字段

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 Awhere dt='201200417' and userid>10) a

join ( select .... from B where dt='201200417' and userid < 10 ) b

on a.key = b.key;

6.2 解决user_id为空不参与关联:

 select * from log a join users b on a.user_id is not null and a.user_id = b.user_id unoin all select * from loga where a.user_id is null;

 或者

 select * from log a left outer join users b on case when a.user_id is null then concat('hive',rand()) else a.user_id end =b.user_id;

6.3 使用分区

对历史库的计算经验  (这项是说根据不同的使用目的优化使用方法)

   历史库计算和使用,分区

6.4 尽量原子化操作,尽量避免一个SQL包含复杂逻辑

可以使用中间表来完成复杂的逻辑   

6.5  jion操作   

小表要注意放在join的左边(目前TCL里面很多都小表放在join的右边)。

否则会引起磁盘和内存的大量消耗

6.6 union all操作

如果union all的部分个数大于2,或者每个union部分数据量大,应该拆成多个insert into 语句,实际测试过程中,执行时间能提升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 ...;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值