背景
当多个 distinct 操作同时出现在 select 中,数据会分发多次。容易造成Reduce数据倾斜
优化点
1、如果不要求精确值,可以使用 spark-sql approx_count_distinct函数 (基数计数 hyperloglog)
2、修改SQL
基础数据准备如下, 需要计算 不同渠道下的 不同周期 的访问uv
presto:bi> desc tmp.multi_distinct_test;
Column | Type | Extra | Comment
---------+---------+-------+----------
user_id | bigint | | 用户ID
channel | varchar | | 渠道名称
day | varchar | | 访问日期
presto:bi> select * from tmp.multi_distinct_test;
user_id | channel | day
---------+---------+------------
1 | A | 2020-01-01 -- 和下一行 数据一模一样
1 | A | 2020-01-01 -- 👆
1

本文探讨了在Hive SQL中遇到多个distinct操作导致的数据倾斜问题及其优化策略。建议使用spark-sql的approx_count_distinct函数进行基数估算,或者调整SQL语句结构,先进行数据去重,再对特定字段进行聚合和distinct操作,以提高查询效率。
最低0.47元/天 解锁文章
1617

被折叠的 条评论
为什么被折叠?



