Spark分桶表实战:如何用分桶减少 40%+ 计算时间

技术原理

今年校招面试spark的分桶表这块问的比较多一些,今天借此给小伙伴搞个案例讲讲哈!

分桶本质上是对文件的划分,其执行逻辑是对分桶key的hash值对桶个数取模,在大表join场景的主要优化逻辑在于通过预先设置分桶+排序,其执行效率得以提高有两个重要原因:避免走Shuffle以及不用在内存中保存Hash数据结构。

具体而言,可以参见下面实践指南,对于大表与大表JOIN,不管是不是分桶表,spark引擎通常都会走SMJ,对于非分桶表需要对关联字段进行hash(Exchange流程)和排序(Sort流程),这两步均比较耗时,但分桶表由于预先进行了这个操作,下游JOIN时会省略这两个步骤。

实践指南

  • 本次测试的数据规模较大,两表数据条数均在百亿级别,数据量小的话请谨慎

  • 分桶个数的设置平台最大设置2048个,并且必须是2的倍数,通常建议每256M一个桶

  • 两个分桶表的桶必须成倍数关系,因为这样join的时候才能快速匹配

  • Bucket Join Key不支持字段别名,某些情况下会导致执行计划解析失败(经测试,会都走SMJ,无法利用好已经分桶和排序的特性)

结论

对于大表与大表JOIN,默认都会走SMJ,设置分桶表可以减少下游计算的资源,计算时间相比于非分桶表JOIN能节省大约39%。

环境准备

分桶表参数:两张表分桶均为2048个。单表的行数均在百亿级别。

运行环境参数:资源充足的情况下进行的测试。

分桶表创建

分桶表一:

sql 复制代码
create table if not exists `table1` (
    order_id BIGINT,
    to_type STRING,
    to_id BIGINT,
    amount BIGINT,
    platform_commission_amount BIGINT,
    charge_type STRING,
    official_account STRING,
    pau_amt BIGINT
)
partitioned by (`p_date` STRING COMMENT '分区日期,yyyyMMdd')
CLUSTERED BY (order_id) SORTED BY (order_id) INTO 2048 BUCKETS
STORED AS parquet
;

分桶表二:

sql 复制代码
CREATE TABLE table2 (
    order_id BIGINT,
    type1 BIGINT,
    type BIGINT,
    id BIGINT,
    test1_type STRING,
    test2_source BIGINT
)
partitioned by (`p_date` STRING COMMENT '分区日期,yyyyMMdd')
CLUSTERED BY (order_id) SORTED BY (order_id) INTO 2048 BUCKETS
STORED AS parquet;

Join测试

sql 复制代码
set spark.sql.sources.bucket.generate.enabled=true;
insert overwrite table `table2` partition (p_date='20241030')
SELECT
    t1.order_id,
     type1 ,
    type ,
    id ,
    test1_type ,
    test2_source
FROM
    table3 t1
join table4 t2 -- table3和 table4是非分桶表,结构和数据量级和table1和 table2一致
on   t1.p_date = '20241029'  and t2.p_date = '20241029'
and t1.order_id=cast(t2.settlement_unit_value as bigint)
;
insert overwrite table `table2` partition (p_date='20241031')
SELECT
    t1.order_id,
     type1 ,
    type ,
    id ,
    test1_type ,
    test2_source
FROM
    table1 t1
join table2 t2
on   t1.p_date = '20241029'  and t2.p_date = '20241029' 
and t1.order_id=t2.order_id

测试结果:

非分桶表:1901 seconds,分桶表:1105 seconds,时间缩短41.8%。

Left Join 测试

sql 复制代码
set spark.sql.sources.bucket.generate.enabled=true;
insert overwrite table `table2` partition (p_date='20241030')
SELECT
    t1.order_id,
     
     type1 ,
    type ,
    id ,
    test1_type ,
    test2_source
FROM
    table3 t1
left join table4 t2 -- table3和 table4是非分桶表,结构和数据量级和table1和 table2一致
on   t1.p_date = '20241029'  and t2.p_date = '20241029'
and t1.order_id=cast(t2.settlement_unit_value as bigint)
;
insert overwrite table `table2` partition (p_date='20241031')
SELECT
    t1.order_id,
   
    type1 ,
    type ,
    id ,
    test1_type ,
    test2_source
FROM
    table1 t1
left join table2 t2
on   t1.p_date = '20241029'  and t2.p_date = '20241029' 
and t1.order_id=t2.order_id

测试结果:

非分桶表:1541.296 seconds,分桶表:932.977 seconds,时间缩短39%。

非插入测试

sql 复制代码
set spark.sql.sources.bucket.generate.enabled=true;
SELECT
    t1.order_id,
    type1 ,
    type ,
    id ,
    test1_type ,
    test2_source
FROM
    table3 t1
left join table4 t2 -- table3和 table4是非分桶表,结构和数据量级和table1和 table2一致
on   t1.p_date = '20241029'  and t2.p_date = '20241029'
and t1.order_id=cast(t2.settlement_unit_value as bigint)
;
 
SELECT
    t1.order_id,
     type1 ,
    type ,
    id ,
    test1_type ,
    test2_source
FROM
    table1 t1
left join table2 t2
on   t1.p_date = '20241029'  and t2.p_date = '20241029' 
and t1.order_id=t2.order_id

测试结果:

非分桶表:1021.062 seconds,分桶表:633.202 seconds,时间缩短38%。

DAG图对比

相关推荐
云技纵横1 天前
唯一索引 INSERT 死锁实战:5 秒复现交叉插入的 S 锁循环等待
sql·mysql
得物技术2 天前
从埋点需求到规则资产:Hermes Agent 重构得物数仓工作流
大数据·llm·ai编程
久美子2 天前
AI驱动数仓建设的Harness工程实践——本体建模、知识分层与上下文工程
大数据
大树883 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
大志哥1233 天前
ES和Logstash日志链路系统上线后遭遇切片爆炸(解决)
大数据·elasticsearch
果丁智能3 天前
物联网智能锁赋能集中式住宿:身份核验与远程权限管控的全链路技术实践
大数据·人工智能·物联网·智能家居
ApacheSeaTunnel3 天前
实战演示 | 基于 Apache SeaTunnel 与 Apache DolphinScheduler 实现 MySQL 到 Doris 离线定时增量同步
大数据·mysql·开源·doris·数据集成·seatunnel·数据同步
weixin_397574093 天前
PDF复杂表格的1:1还原引擎:跨页表格自动拼接技术实战
大数据·人工智能·pdf
极光代码工作室3 天前
基于数据仓库的电商数据分析平台
大数据·hadoop·python·spark·数据可视化
BD_Marathon3 天前
SQL学习指南——视图
数据库·sql