2014-10-07 74 views
0

我有一個SQL查詢需要很長時間才能執行。SQL代碼性能

它是這樣的

select 
    columns 
from 
    tab1 
where 
    tab1.id in (select col from tab2 where conditions) --32000 rows 
    or 
    tab1.id in (select col from tab3 where conditions) ---14000 rows 
    or 
    tab1.id in (select col from tab4 where conditions) --6000 rows 

有什麼辦法,我可以在這裏提高性能?

我試過使用EXISTS(),但這並沒有幫助。

+0

你嘗試做你想要使用JOIN的做什麼? – Philipp 2014-10-07 20:29:20

+3

解釋「條件」。爲什麼不加入? – Horaciux 2014-10-07 20:29:33

+2

什麼是查詢計劃?哪些索引可用? 「很多時間執行」是什麼意思?你自己的三條「IN」條款聲明各有多貴? – 2014-10-07 20:30:34

回答

0

對於優化具有子查詢in的查詢,Oracle應該非常好。你最好的選擇是增加索引。但是,您的查詢不足以提供特定索引。您需要明確說明where條款。

0

選項1:

select 
    columns 
from 
    tab1 
where 
    tab1.id in (select col from tab2 where conditions --32000 rows 
       union all 
       select col from tab3 where conditions ---14000 rows 
       union all  
       select col from tab4 where conditions --6000 rows 
       ); 

選項2:

select 
    columns 
from 
    tab1 
inner join (select distinct col 
      from (select col from tab2 where conditions --32000 rows 
       union all 
       select col from tab3 where conditions ---14000 rows 
       union all  
       select col from tab4 where conditions --6000 rows 
       ) 
      ) x 
    on tab1.id = x.col; 

選項3:

select 
    columns 
from 
    tab1 
where 
    exists (select col from tab2 where conditions --??? rows 
      where col = tab1.id 
       union all 
       select col from tab3 where conditions ---??? rows 
       where col = tab1.id 
       union all  
       select col from tab4 where conditions --??? rows 
       where col = tab1.id 
       );