2011-09-05 85 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;