我的SQL性能很差,其中包含一个不相关的子查询。我用一个共同相关的子查询替换它,但性能变得更糟。有没有办法通过完全删除子查询并用连接替换它来重写此SQL?
以下是查询的简化版本:
select distinct tab1.app_id,
tab1.name,
tab1.stat_cd,
tab1.qr
from apps tab1 , issues tab2
where
tab1.app_id = tab2.app_id and
tab1.qr = 'm' and
tab2.iqr = 'm' and
tab1.app_id not in
(
select distinct tab3.app_id
from issues tab3
where tab3.iqr = 'm' and
(
tab3.i_cd = 'f' or
tab3.i_cd = 'r' or
tab3.i_cd = 'c'
)
)
任何提示或帮助表示赞赏。谢谢。
答案 0 :(得分:0)
不确定这会有多大帮助,但我想听听它是如何做到的......
SELECT DISTINCT tab1.app_id,
tab1.name,
tab1.stat_cd,
tab1.qr
FROM apps tab1
JOIN issues tab2 ON tab1.app_id = tab2.app_id
JOIN ( SELECT DISTINCT app_id AS app_id
FROM issues
WHERE iqr = 'm'
AND ( i_cd = 'f' OR
i_cd = 'r' OR
i_cd = 'c'
)
) tab3 ON tab1.app_id = tab3.app_id
WHERE tab1.qr = 'm'
AND tab2.iqr = 'm';
答案 1 :(得分:0)
试试这个: -
select distinct tab1.app_id,
tab1.name,
tab1.stat_cd,
tab1.qr
from apps tab1
inner join
issues tab2
on
tab1.app_id = tab2.app_id
left join
issues tab3
on
tab1.app_id = tab3.app_id
where tab1.qr = 'm' and
tab2.iqr = 'm' and
concat(tab3.iqr,tab3.i_cd) not in ('mf','mr','mc)
希望这会有所帮助: - )
答案 2 :(得分:0)
试试这个
select distinct tab1.app_id,
tab1.name,
tab1.stat_cd,
tab1.qr
from apps tab1
inner join issues tab2 on ( tab1.app_id, tab1.qr)= ( tab2.app_id, tab2.iqr) and tab1.qr='m'
where not exists
(
select * from issues tab3
where ( tab1.app_id, tab1.qr)= ( tab3.app_id, tab3.iqr)
and tab3.i_cd in ('f', 'r', 'c')
)
答案 3 :(得分:0)
其他解决方案
select distinct tab1.app_id,
tab1.name,
tab1.stat_cd,
tab1.qr
from apps tab1
inner join issues tab2 on ( tab1.app_id, tab1.qr)= ( tab2.app_id, tab2.iqr) and tab1.qr='m'
left outer join issues tab3 on ( tab1.app_id, tab1.qr)= ( tab3.app_id, tab3.iqr) and tab3.i_cd in ('f', 'r', 'c')
where tab3.app_id is null