2016-11-24 51 views
0

有子查询时,我有以下语句:SQL慢在声明中

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秒)。 我不知道为什么会出现这种情况。有任何想法吗?

+0

你是什么意思'使用常量'和'使用子查询? –

+1

性能问题应该包括'EXPLAIN ANALYZE'和一些关于表格大小,索引,当前时间表现,期望时间等的信息。'Slow'是一个相对术语,我们需要一个真实值来比较。 –

+0

为什么你在('H50')'而不是'foo ='H50''处使用'foo?不知道这与你面临的性能问题有关,但仍然...(当然,第二个条件也一样)。 – FDavidov

回答

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 
;