2016-11-24 73 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 
;