oracle 嵌套SQL -更快?

w8ntj3qf  于 2023-08-03  发布在  Oracle
关注(0)|答案(2)|浏览(107)

我有一个SQL代码段,它需要很长时间才能运行,因为它是嵌套的。我只是想知道,是否有一种方法可以简化它,使它运行得更快。
下面是SQL:

select nid 
from table_A 
where bid in (select bid
              from Table_B
              where xid in (select xid 
                            from Table_C 
                            where name_l in (select name_l
                                             from accs 
                                             where prod like '%arlehen%')))

字符串
先谢谢你。

ugmeyewa

ugmeyewa1#

我认为查询可以写成这样:

select nid 
from table_A 
where bid in (
  select b.bid
  from Table_B b
  join Table_C c on b.xid=c.xid
  join accs on c.name_l=accs.name_l
  where accs.prod like '%arlehen%');

字符串
最新的统计信息(意味着不是陈旧的统计信息)可能有助于优化器选择最佳执行路径。
或者,重写查询的另一种方法如下:

with tba as (
select a.*
from accs accs
where a.prod like '%arlehen%'
)
select nid 
from table_A 
where bid in (
  select b.bid
  from Table_B b
  join Table_C c on b.xid=c.xid
  join tba on c.name_l=tba.name_l)
;

hxzsmxv2

hxzsmxv22#

您可以尝试用EXISTS()替换IN(),这可能会减少I/O成本-当然,这需要在您的数据集上进行检查:

select nid 
from table_A a
where exists (
    select 1
    from Table_B b
    where b.bid = a.bid 
    and exists (
        select 1 
        from Table_C c
        where b.xid = c.xid
        and exists (
            select 1
             from accs ac
             where ac.prod like '%arlehen%'
             and ac.name_l = c.name_l
        )
    )
)
;

字符串

相关问题