join表时没有添加on条件,造成笛卡尔积查询,导致队列资源爆满,作业运行失败怎么办?
问题现象
运行的SQL语句中存在join表,但是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条件来减少多表关联的数据量,从而减轻队列的负荷,提升查询效率。
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)