繁体   English   中英

删除子查询所需的SQL帮助

[英]SQL help needed in removing a sub-query

我有一个性能不佳的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' 
              ) 
            ) 

任何提示或协助,不胜感激。 谢谢。

不知道这将有多少帮助,但我想听听它的作用...

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';

尝试这个:-

 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)  

希望这可以帮助:-)

尝试这个

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')
) 

其他解决方案

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM