2011-09-05 84 views
2

我試圖讓我的查詢儘可能快地運行,但是我正努力讓它在5秒以內。oracle查詢的優化

我想這是因爲我引用兩個相連的數據庫

這裏是我的查詢

select column2, column3, column4 
    from [email protected] 
where column1 in (
      select distinct column2 
      from [email protected] 
      where column3 > 0 
       ) 
order by column1 

有沒有辦法來優化這個查詢了嗎?

我一直在使用join嘗試,但它似乎使查詢運行較長

在此先感謝

編輯

進一步調查DRIVING_SITE使它像這樣運行

非常快
select /*+ DRIVING_SITE(table1) */ t1.column2, t1.column3, t1.column4 
from [email protected] t1, [email protected] t2 
WHERE t2.column3 > 0 

但是,只要我將distinct column2在它使它運行非常緩慢

+1

一個綜合指數。 http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/sql_elements006.htm#SQLRF50704 –

+1

你不需要'IN'(SELECT ...)裏的'distinct' –

+1

什麼你有2張桌子上的索引嗎? –

回答

1

首先,不需要distinct。該查詢可以寫爲:

select * 
    from [email protected] 
where column1 in (
      select column2 
      from [email protected] 
      where column3 > 0 
       ) 
order by column1 

其次,至少有兩種方法來編寫它。無論是與JOIN

select t1.* 
    from [email protected] t1 
    join [email protected] t2 
where t2.column2 = t1.column1 
    and t2.column3 > 0 
group by 
     t1.id, t1.column1, ... 

order by t1.column1 

或(我的偏好)與EXISTS

select t1.* 
    from [email protected] t1 
where exists 
     (select * 
      from [email protected] 
      where t2.column2 = t1.column1 
      and t2.column3 > 0 
       ) 
order by column1 

在任何情況下,你應該檢查所有他們的執行計劃。

我希望,如果你對table1.column1table2有一個指數的表現是最好的,無論是在column2索引或您可能需要調查`DRIVING_SITE`提示上(column3, column2)

+0

第三選擇完美工作 - 謝謝:) –

0

我同意上面的香農,但你是否能夠在開發服務器上創建一個視圖?

選擇*是有點調皮 - 最好命名你真正想要的領域。對於非常大的數據集,也會給你提高性能。

0

我錯過了相信這會起作用的東西嗎?

select t1.* 
from table1 t1, table2 t2 
where t1.column1 = t2.column2(+) 
and t2.column3 > 0;