HiveSql语法优化四 :Bucket Map Join和Sort Merge Bucket Map Join优化

Bucket Map Join

        之前的map join适用场景是大表join小表的情况,但是两张表都相对较大,若采用普通的Map Join算法,则Map端需要较多的内存来缓存数据,当然可以选择为Map段分配更多的内存,来保证任务运行成功。但是,Map端的内存不可能无上限的分配,所以当参与Join的表数据量均过大时,就可以考虑采用Bucket Map Join算法。

        比如下面两张表进行join操作:

表名

大小

order_detail

1176009934(约1122M)

payment_detail

334198480(约319M)

        首先需要依据源表创建两个分桶表,order_detail建议分16个bucket,payment_detail建议分8个bucket,注意分桶个数的倍数关系以及分桶字段

--订单表
hive (default)> 
drop table if exists order_detail_bucketed;
create table order_detail_bucketed(
    id           string comment '订单id',
    user_id      string comment '用户id',
    product_id   string comment '商品id',
    province_id  string comment '省份id',
    create_time  string comment '下单时间',
    product_num  int comment '商品件数',
    total_amount decimal(16, 2) comment '下单金额'
)
clustered by (id) into 16 buckets
row format delimited fields terminated by '\t';

--支付表
hive (default)> 
drop table if exists payment_detail_bucketed;
create table payment_detail_bucketed(
    id              string comment '支付id',
    order_detail_id string comment '订单明细id',
    user_id         string comment '用户id',
    payment_time    string comment '支付时间',
    total_amount    decimal(16, 2) comment '支付金额'
)
clustered by (order_detail_id) into 8 buckets
row format delimited fields terminated by '\t';

然后向两个分桶表导入数据:

--订单表
hive (default)> 
insert overwrite table order_detail_bucketed
select
    id,
    user_id,
    product_id,
    province_id,
    create_time,
    product_num,
    total_amount   
from order_detail
where dt='2020-06-14';

--分桶表
hive (default)> 
insert overwrite table payment_detail_bucketed
select
    id,
    order_detail_id,
    user_id,
    payment_time,
    total_amount
from payment_detail
where dt='2020-06-14';

然后设置以下参数:

--关闭cbo优化,cbo会导致hint信息被忽略,需将如下参数修改为false
set hive.cbo.enable=false;
--map join hint默认会被忽略(因为已经过时),需将如下参数修改为false
set hive.ignore.mapjoin.hint=false;
--启用bucket map join优化功能,默认不启用,需将如下参数修改为true
set hive.optimize.bucketmapjoin = true;

最后在重写SQL语句,如下:

select /*+ mapjoin(pd) */
    *
from order_detail_bucketed od
join payment_detail_bucketed pd on od.id = pd.order_detail_id;

        需要注意的是,Bucket Map Join的执行计划的基本信息和普通的Map Join无异,若想看到差异,可执行如下语句,查看执行计划的详细信息。详细执行计划中,如在Map Join Operator中看到 “BucketMapJoin: true”,则表明使用的Join算法为Bucket Map Join。

explain extended select /*+ mapjoin(pd) */
    *
from order_detail_bucketed od
join payment_detail_bucketed pd on od.id = pd.order_detail_id;

Sort Merge Bucket Map Join

        两张表都相对较大,除了可以考虑采用Bucket Map Join算法,还可以考虑SMB Join。相较于Bucket Map Join,SMB Map Join对分桶大小是没有要求的。

需要设置如下参数:

--启动Sort Merge Bucket Map Join优化
set hive.optimize.bucketmapjoin.sortedmerge=true;
--使用自动转换SMB Join
set hive.auto.convert.sortmerge.join=true;

相关推荐

  1. Go 语言一些常用语法编写优化指南

    2023-12-17 05:08:02       55 阅读
  2. Go 语言一些常用语法编写优化指南

    2023-12-17 05:08:02       51 阅读
  3. 人工智能之函数优化组合优化

    2023-12-17 05:08:02       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-17 05:08:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-17 05:08:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-17 05:08:02       82 阅读
  4. Python语言-面向对象

    2023-12-17 05:08:02       91 阅读

热门阅读

  1. 深入了解惊群问题:Accept、Epoll及Nginx的优化策略

    2023-12-17 05:08:02       53 阅读
  2. 南京邮电大学数据库实验二

    2023-12-17 05:08:02       52 阅读
  3. Axure的交互以及情形的介绍

    2023-12-17 05:08:02       59 阅读
  4. QT作业3

    QT作业3

    2023-12-17 05:08:02      53 阅读
  5. rpc和http的区别,使⽤场景

    2023-12-17 05:08:02       56 阅读
  6. AR游戏开发

    2023-12-17 05:08:02       53 阅读
  7. 【Linux应用编程笔记】输入设备

    2023-12-17 05:08:02       53 阅读
  8. Python学习笔记第七十六天(OpenCV视频应用)

    2023-12-17 05:08:02       55 阅读