在语句中使用子查询时SQL很慢

时间:2016-11-24 14:37:05

标签: sql oracle

我有以下声明:

 WITH foos AS (
    select regexp_substr('H50','[^,]+', 1, level) x from dual
    connect by regexp_substr('H50', '[^,]+', 1, level) is not null
), baars AS (
    select regexp_substr('G30','[^,]+', 1, level) x from dual
    connect by regexp_substr('G30', '[^,]+', 1, level) is not null
) 
  select 
  count(*) 
  from VIEW
  where foo in (
    'H50'
    -- select x from foos
  )
  and bar in (
   'G30'
   -- select x from bars
  );

使用常量G30H50时,速度非常快。然而,当我使用subquerys时,它真的很慢(~5秒)。 我不知道为什么会这样。任何想法?

1 个答案:

答案 0 :(得分:1)

首先,在第一个用例中没有使用内联视图,因为根本没有执行。

with    t1 (n) as (select 1 from dual union all select n+1 from t where n <= 10)
       ,t2 (n) as (select 1 from dual union all select n+1 from t where n <= 1000000000000)

select  *
from    t1
;