美文网首页
Hive碎碎念(6):HIVE优化

Hive碎碎念(6):HIVE优化

作者: xxlee | 来源:发表于2019-06-27 14:21 被阅读0次

    转载请在文章起始处注明出处,谢谢。


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

    基本原则:

    1、尽量尽早地过滤数据,减少每个阶段的数据量,对于分区表要加分区,同时只选择需要使用到的字段
    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、分区表统计尽量使用分区
    3、尽量避免一个SQL包含复杂逻辑

    建议尽量使用中间表来完成复杂的逻辑处理。

    4 、join操作

    小表要注意放在join的左边(关系型数据库查询中里面很多都小表放在join的右边),否则会引起磁盘和内存的大量消耗

    5、如果union all的部分个数大于2,或者每个union部分数据量大,应该拆成多个insert into 语句,实际测试过程中,执行时间能提升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要先了解数据本身的特点,如果有join ,group操作的话,要注意是否会有数据倾斜

    如果出现数据倾斜,应当做如下处理:

    set hive.exec.reducers.max=200;
    set mapred.reduce.tasks = 200;---增大Reduce个数
    set hive.groupby.mapaggr.checkinterval=100000 ;--这个是group的键对应的记录条数超过这个值则会进行分拆,值根据具体数据量设置
    set hive.groupby.skewindata=true; --如果是group by过程出现倾斜 应该设置为true
    set hive.skewjoin.key=100000; --这个是join的键对应的记录条数超过这个值则会进行分拆,值根据具体数据量设置
    set hive.optimize.skewjoin=true;--如果是join 过程出现倾斜 应该设置为true
    
    PS:Hive优化参数(写在执行语句前)
    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;
    

    相关文章

      网友评论

          本文标题:Hive碎碎念(6):HIVE优化

          本文链接:https://www.haomeiwen.com/subject/ghplcctx.html