美文网首页
优化三:多表关联-数据倾斜-空值

优化三:多表关联-数据倾斜-空值

作者: 小呀小芒果 | 来源:发表于2018-02-27 16:30 被阅读34次
现象

原hql,用时6093.318 seconds

create table temp.day_user_play_uuid_split_20180226_test1 as
select 
t.datess,
t.device_id,
t.version_id,
t.province_id,
t.city_id,
t.telecom_id,
t.terminal_id,
t.ip,
case
when play_type_id='02' then 
case when t.uuid is not null and t.uuid !='' and t.uuid !='0' then t.uuid when t2.uuid is not null then t2.uuid when t3.uuid is not null then t3.uuid
else 'unknow' end
when play_type_id='03' then
case when t.uuid is not null and t.uuid !='' and t.uuid !='0' then t.uuid when t1.premiere_channel_uuid is not null then t1.premiere_channel_uuid else 'unknow' end
else t.uuid end,
t.play_type_id,
t.play_type,
t.program_id,
t.program_serise_id,
t.client_time,
t.server_time,
t.open_time,
t.play_time,
t.buffer_count,
t.buffer_aver_time,
t.url_first,
t.url,
t.terminal_type 
from 
    (select * from dm_bas.day_user_play_uuid where play_type<>'vod_replay') t 
left join 
         dim.cms_program t1 on t.program_id=t1.program_id 
left join 
         dim.cms_playbill_channel t2 on t.program_id=t2.channel_id 
left join 
         dim.cms_history_playbill_channel t3 on t.program_id=t3.playbill_id ;

分析:

查看主表关联字段分布,可以看出空值占总量的66.6%的百分比,这导致大部分计算分配到一个reduce导致整个任务计算缓慢,体现为reduce进度长时间处在99%。
--总数据量
select count(1) as num from dm_bas.day_user_play_uuid
where  play_type<>'vod_replay';
390270887
--关联字段分布情况
select program_id,count(1) as num from dm_bas.day_user_play_uuid
where  play_type<>'vod_replay' group by program_id order by num desc limit 6;
    259816668
0   46610174
1   1284378
70564740    357546
70564743    251696
70564694    212326

方案:

关联前将左表为空关联字段设置为一个随机数,再去关联右表,这么做的目的是即使是左表的未关联记录,它的key也分布得十分均匀

create table temp.day_user_play_uuid_split_20180226_test3 as
select 
t.datess,
t.device_id,
t.version_id,
t.province_id,
t.city_id,
t.telecom_id,
t.terminal_id,
t.ip,
case
when play_type_id='02' then 
case when t.uuid is not null and t.uuid !='' and t.uuid !='0' then t.uuid when t2.uuid is not null then t2.uuid when t3.uuid is not null then t3.uuid
else 'unknow' end
when play_type_id='03' then
case when t.uuid is not null and t.uuid !='' and t.uuid !='0' then t.uuid when t1.premiere_channel_uuid is not null then t1.premiere_channel_uuid else 'unknow' end
else t.uuid end,
t.play_type_id,
t.play_type,
t.program_id,
t.program_serise_id,
t.client_time,
t.server_time,
t.open_time,
t.play_time,
t.buffer_count,
t.buffer_aver_time,
t.url_first,
t.url,
t.terminal_type 
from 
   (select 
   datess,device_id,version_id,province_id,city_id,telecom_id,terminal_id,ip,uuid,play_type_id,play_type,
   case when program_id is not null and trim(program_id) != '' then program_id else RAND() end as program_id,
   program_serise_id,client_time,server_time,open_time,play_time,buffer_count,
   buffer_aver_time,url_first,url,terminal_type
   from dm_bas.day_user_play_uuid where play_type<>'vod_replay') t 
left join 
        dim.cms_program t1 on t.program_id=t1.program_id 
left join 
        dim.cms_playbill_channel t2 on t.program_id=t2.channel_id 
left join 
        dim.cms_history_playbill_channel t3 on t.program_id=t3.playbill_id ;

把program_id为null和空字符串的转换为随机数即可,用时856.342 seconds,速度提升了7倍。

误区:

刚开始使用的是coalesce(program_id,RAND()) as program_id替换的program_id,速度还是提升不上去,后来才反应过来coalesce只替换数据为null的而空字符串的数据还是保留原样。

相关文章

  • 优化三:多表关联-数据倾斜-空值

    现象 原hql,用时6093.318 seconds 分析: 查看主表关联字段分布,可以看出空值占总量的66.6%...

  • Mysql 一些实用方法笔记!

    sql 注入: sql 导入数据: 批量插入数据: sql 关联更新多表数据: sql 关联删除多表数据: mys...

  • Hive优化

    Hive数据倾斜优化总结 Hive数据倾斜优化分为配置优化和SQL优化 优先原则: 数据不怕多,避免倾斜。 减少J...

  • 2018-09-21数据库&多表关联&增删改&a

    数据库&多表关联&增删改&数据库备份 多表关联 两表关联,必须有一个关联字段 关联方法 where(原理) 第一步...

  • 2018-10-26数据库&多表关联&增删改&数据库备份

    数据库&多表关联&增删改&数据库备份

  • 2018-10-26

    数据库&多表关联&增删改&数据库备份 数据库关系图 数据库ID关联

  • hive 优化-1

    join优化-数据倾斜hive.optimize.skewjoin=true; 【TODO 细节】数据倾斜时启动两...

  • MySQL的多表关联查询

    一、多表关联查询 多表关联查询是使用一条SQL语句,将关联的多张表的数据查询出来。 1.1 交叉查询 交叉查询就是...

  • MapReduce实现‘多表关联’

    多表关联和单表关联相似,都类似于数据库中的自然连接。相比单表关联,多表关联的左右表和连接列更加清楚。所以可以采用和...

  • hive 数据倾斜优化

    在一个大表关联小表中遇到数据倾斜的问题,优化方法如下 mapjoin 类似写MR时,将小数据直接放入map缓存,通...

网友评论

      本文标题:优化三:多表关联-数据倾斜-空值

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