I have a poorly performing SQL that contains a sub-query that is not co-related. I replaced it with a co-related sub-query but performance got worse. Is there a way to rewrite this SQL by removing the sub-query entirely and replacing it with a join?
Below is a simplified version of the query:
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'
)
)
Any tip or assistance is appreciated. Thank you.
Not sure how much help this will be, but I would like to hear back on how it does...
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';
Try this:-
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)
Hope this helps:-)
try this
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')
)
other solution
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.