Optimisation of an oracle query

大城市里の小女人 提交于 2019-12-11 00:33:25

问题


I'm trying to make my query run as quickly as possible but i'm struggling to get it under 5 seconds.

I think it's because i'm referencing two linked databases

Here's my query

select column2, column3, column4 
  from table1@dev 
 where column1 in (
          select distinct column2 
            from table2@dev 
           where column3 > 0
                  ) 
order by column1

Is there a way to optimise this query any more?

I've tried using join but it seems to make the query run longer

Thanks in advance

EDIT

From further investigation the DRIVING_SITE makes it run very quick like this

select /*+ DRIVING_SITE(table1) */ t1.column2, t1.column3, t1.column4
from table1@dev t1, table2@dev t2
WHERE t2.column3 > 0

But as soon as I add the distinct column2 in it makes it run really slow


回答1:


First, no need for distinct. The query can be written as:

select * 
  from table1@dev 
 where column1 in (
          select column2 
            from table2@dev 
           where column3 > 0
                  ) 
order by column1

Second, there are (at least) two more ways to write it. Either with JOIN:

select t1.* 
  from table1@dev t1
  join table2@dev t2
 where t2.column2 = t1.column1
   and t2.column3 > 0 
group by
       t1.id, t1.column1, ... 

order by t1.column1

or (my preference) with EXISTS:

select t1.* 
  from table1@dev t1 
 where exists
       ( select *
           from table2@dev 
          where t2.column2 = t1.column1
            and t2.column3 > 0
                  ) 
order by column1

In any case, you should check the execution plans for all of them.

I would expect performance to be best if you have an index on table1.column1 and for table2, either an index on column2 or a composite index on (column3, column2)




回答2:


I agree with Shannon above , but are you able to create a view on the dev server ?

Also select * is a bit naughty - it is better to name the fields you really want. For very large datasets that will give you a performance improvement too.




回答3:


Am I missing something in believing that this will work?

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


来源:https://stackoverflow.com/questions/7310805/optimisation-of-an-oracle-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!