文档首页> 数据湖探索 DLI> 常见问题> SQL作业相关问题> 运维指导> join表时没有添加on条件,造成笛卡尔积查询,导致队列资源爆满,作业运行失败
更新时间:2022-11-09 GMT+08:00

join表时没有添加on条件,造成笛卡尔积查询,导致队列资源爆满,作业运行失败

问题现象

运行的SQL语句中存在join表,但是join没有添加on条件,多表关联造成笛卡尔积查询,最终导致队列资源占满,该队列上的作业运行失败。

例如,如下问题SQL语句,存在三个表的left join,并且没有指定on条件,造成笛卡尔积查询。
select 
     case 
        when to_char(from_unixtime(fs.special_start_time), 'yyyy-mm-dd') < '2018-10-12' and row_number() over(partition by fg.goods_no order by fs.special_start_time asc) = 1 then 1
        when to_char(from_unixtime(fs.special_start_time), 'yyyy-mm-dd') >= '2018-10-12' and fge.is_new = 1 then 1 
        else 0 end as is_new
from testdb.table1 fg 
left join testdb.table2 fs
left join testdb.table3  fge
where to_char(from_unixtime(fs.special_start_time), 'yyyymmdd') = substr('20220601',1,8)

解决措施

在使用join进行多表关联查询时,不管表数据量大小,join时都需要指定on条件来减少多表关联的数据量,从而减轻队列的负荷,提升查询效率。

例如,问题现象中的问题语句可以根据业务场景,在join时通过指定on条件来进行优化,这样会极大减少关联查询的结果集,提升查询效率。
select 
     case 
        when to_char(from_unixtime(fs.special_start_time), 'yyyy-mm-dd') < '2018-10-12' and row_number() over(partition by fg.goods_no order by fs.special_start_time asc) = 1 then 1
        when to_char(from_unixtime(fs.special_start_time), 'yyyy-mm-dd') >= '2018-10-12' and fge.is_new = 1 then 1 
        else 0 end as is_new
from testdb.table1 fg 
left join testdb.table2 fs on fg.col1 = fs.col2
left join testdb.table3  fge on fg.col3 = fge.col4
where to_char(from_unixtime(fs.special_start_time), 'yyyymmdd') = substr('20220601',1,8)

运维指导 所有常见问题

more