SQL常见小问题
作者:互联网
本文章持续记录工作中遇到的SQL的问题,持续更新中……
SQL常见问题
一、full join导致数据量翻倍
原因:空值会导致数据重复
错误SQL
select coalesce(a.user_id,b.user_id,c.user_id,d.user_id,e.user_id,f.user_id) as user_id
from
(select user_id from table_06)a full join
(select user_id from table_05)b on a.user_id=b.user_id full join
(select user_id from table_04)c on a.user_id=c.user_id full join
(select user_id from table_03)d on a.user_id=d.user_id full join
(select user_id from table_02)e on a.user_id=e.user_id full join
(select user_id from table_01)f on a.user_id=f.user_id
正确SQL
select coalesce(a.user_id,b.user_id,c.user_id,d.user_id,e.user_id,f.user_id) as user_id
from
(select user_id from table_06)a full join
(select user_id from table_05)b on a.user_id=b.user_id full join
(select user_id from table_04)c on coalesce(a.user_id,b.user_id)=c.user_id full join
(select user_id from table_03)d on coalesce(a.user_id,b.user_id,c.user_id)=d.user_id full join
(select user_id from table_02)e on coalesce(a.user_id,b.user_id,c.user_id,d.user_id)=e.user_id full join
(select user_id from table_01)f on coalesce(a.user_id,b.user_id,c.user_id,d.user_id,e.user_id)=f.user_id
二、left join 导致broadcast/mapjoin失效
原因:broadcast/mapjoin不经过reduce,读取文件后直接就会产生结果
小表有的key,left过程中不知道怎么处理。只能sortmergejoin
错误SQL
select count(1) from (
select count(1) from
(select pkg from trandw.dim_pub_app)a left join
(select gazj,pkg from trandw.dws_log_app_open_ds where dt='20220615' )b on a.pkg = b.pkg
)t ;
正确SQL
select count(1) from (
select count(1) from
(select pkg from trandw.dim_pub_app)a inner join
(select gazj,pkg from trandw.dws_log_app_open_ds where dt='20220615' )b on a.pkg = b.pkg
)t ;
标签:full,join,常见,问题,user,SQL,table,id,select 来源: https://www.cnblogs.com/wuxiaolong4/p/16426236.html