Skip to Main Content
  • Questions
  • Performance issue for accessing table of a remote database

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Subrata.

Asked: August 20, 2002 - 8:16 am UTC

Last updated: August 28, 2006 - 8:41 am UTC

Version: 8.1.7

Viewed 1000+ times

You Asked

I need to access a table of a remote database. I have a number of options. Which one
should be faster and why?


1. select from T1
where (T1.f1, T1.f2) in (select T2.f1, T2.f2
from T2.@remote.world
where T2.f3 <= c1
and T2.f3 > c2)


2. select * from T1
where exists (select T2.f1, T2.f2
from T2.@remote.world
where T2.f3 <= c1
and T2.f3 > c2
and T1.f1 = T2.f1
and T1.f2 = T2.f2)

3. Copy the data from remote database to a temporary table and process either by option 1 or 2.
(This data is being required by three more SQL in the procedure)

Please note that
A.necessary indexes are there on
T1(f1,f2)
T2(f1,f2)
T2(f3)

B.When I tried, option 1 (ie. the "in" statement) was much much faster than option 2.

Thanks


and Tom said...

option 1 and 3 are effectively the same.

1 and 2's performance is always dictated by the size of the inner and outer query (even on a single machine).

#2 is like this

for every row in T1
run a subquery against T2


if T1 is big and T2 is small -- that is the wrong way to do it. #1 would be better.

This isn't really to do with dblinks or anything, it is the old "in vs exists" question itself.

See
</code> http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:953229842074 <code>
for a discussion on this.


Rating

  (16 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Subrata Saha, August 20, 2002 - 10:15 am UTC

The link was very helpful.
But when I used explain plan it was not clear the way Oracle handles (or converts) the "in" and "exists". Does the explain plan fails to show how the query is rewritten in these cases, or did I fail to interprete the explain plan?

Thanks

Tom Kyte
August 20, 2002 - 10:25 am UTC

The query is not "rewritten", the query is processed and processed differently when you use an in vs where exists. The plans show that.

If you have a specific question -- use an example, maybe that will make it more clear.

Juan Carlos Reyes, February 04, 2003 - 7:48 pm UTC

Hi Tom, I has a question
I did this remote select

SELECT
NVL( SUM( DB_TCS_CAMBIOMONEDA_SE( dFecha, EMN_MONEDA, cMon, ( HCA_PRVAL - VEP_PRVEN ) * ( VEP_NUMTITS - VEP_LIQANT ) )), 0)

FROM VENTA_PACTO@SOA.WORLD, CARTERA@SOA.WORLD, EMISION@SOA.WORLD
WHERE VEP_LIQ = 'F'
AND CAR_CODCART = VEP_CODCART
AND CAR_CODCLI = nCodCli
AND CAR_INST = EMN_CODINST
AND CAR_SERIE = EMN_SERIE
AND EMN_METODO_VALUACION IN ( 'CUP', 'CUK' );

It took about 8:00 mn

then I changed
DB_TCS_CAMBIOMONEDA_SE to DB_TCS_CAMBIOMONEDA_SE@SOA.WORLD


SELECT
NVL( SUM( DB_TCS_CAMBIOMONEDA_SE@soa.world( dFecha, EMN_MONEDA, cMon, ( HCA_PRVAL - VEP_PRVEN ) * ( VEP_NUMTITS - VEP_LIQANT ) )), 0)
.............

And it took 0.00 seconds

Why this happened

Thanks

Tom Kyte
February 04, 2003 - 7:58 pm UTC

well, the first one probably pulled all of the data locally, executed the query and then ran the function.

the other probably ran the query in its entirety at the remote site, returning only the answer.

an explain plan using autotrace traceonly explain would be most illuminating to you...

A reader, February 05, 2003 - 9:06 am UTC

:) Thanks

I only want to be sure, I supposed initially that the query returned the row from the remote database and once then processed locally the row, but it's not in this way.

here are the trace info.

Using remote function ( o seconds )
----------------------
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT (REMOTE) Optimizer=CHOOSE (Cost=248 Card=1
Bytes=78)

1 0 SORT (AGGREGATE)
2 1 NESTED LOOPS (Cost=248 Card=1 Bytes=78)
3 2 MERGE JOIN (CARTESIAN) (Cost=8 Card=2399 Bytes=88763)
4 3 TABLE ACCESS (FULL) OF 'VENTA_PACTO' (Cost=2 Card=1 SAFI
Bytes=21)

5 3 BUFFER (SORT) (Cost=6 Card=2399 Bytes=38384)
6 5 TABLE ACCESS (FULL) OF 'EMISION' (Cost=6 Card=2399 SAFI
Bytes=38384)

7 2 VIEW OF 'HICARTERA' SAFI
8 7 UNION-ALL (PARTITION)
9 8 TABLE ACCESS (BY INDEX ROWID) OF 'HICARTERA_RW' (C SAFI
ost=2 Card=1 Bytes=30)

10 9 INDEX (RANGE SCAN) OF 'IDX_FEINSTSERIE_RO' (NON- SAFI
UNIQUE) (Cost=1 Card=1)

11 8 TABLE ACCESS (BY INDEX ROWID) OF 'HICARTERA_RO' (C SAFI
ost=2 Card=1 Bytes=30)

12 11 INDEX (RANGE SCAN) OF 'IDX_FEINSTSERIE' (NON-UNI SAFI
QUE) (Cost=3 Card=1)





Statistics
----------------------------------------------------------
10 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
510 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed




Using local function (8 mn)
--------------------


Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=6 Card=1 Bytes=146)
1 0 SORT (AGGREGATE)
2 1 NESTED LOOPS (Cost=6 Card=1 Bytes=146)
3 2 NESTED LOOPS (Cost=4 Card=1 Bytes=83)
4 3 REMOTE* (Cost=2 Card=1 Bytes=64) SOA.WORL
D

5 3 REMOTE* SOA.WORL
D

6 2 REMOTE* (Cost=2 Card=1 Bytes=63) SOA.WORL
D



4 SERIAL_FROM_REMOTE SELECT "HCA_FECHA","HCA_PRVAL","HCA_CODCART"
,"HCA_INST","HCA_SERIE","HCA_CODCLI"

5 SERIAL_FROM_REMOTE SELECT "EMN_SERIE","EMN_CODINST","EMN_MONEDA
" FROM "EMISION" "EMISION" WHERE :1=

6 SERIAL_FROM_REMOTE SELECT "VEP_CODCART","VEP_FECHAVEN","VEP_PRV
EN","VEP_NUMTITS","VEP_LIQ","VEP_LIQ



Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
500 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed


remote queries and temporary tablespaces (remote and local)

A reader, May 29, 2003 - 6:30 am UTC

hi

I have some doubts regading queries against a remote database using db links.

If I have a query say

select /*+ USE_MERGE(emp v) */ ename, dname
from emp, dept@db1 v
where emp.deptno = v.deptno

I understand the sort will take place in the local database

If I have a query says

select count(*)
from dept@db1
group by deptno, location;

in the remote v$sql I can see the query is rewritten to

select count(*)
from dept
group by deptno, location;

however is that group by using remote temporary tablespace or the whole table is downloaded to my local database and doing the sort in my local temp tablespace?

Tom Kyte
May 29, 2003 - 8:27 am UTC

if the remote sql is that -- then the group by happens there, on the remote site.

if you saw "select ... from dept" on the remote site, then the group by would be happening locally.


(group bys don't have to use temp by the way. they don't even have to sort in all cases)

doesnt aggreate function sort?

A reader, May 29, 2003 - 11:55 am UTC

Hi

doesnt count(*) sort?

also group by?
from concepts guide

All operations that use sorts, including joins, index builds, ordering, computing aggregates (GROUP BY), and collecting optimizer statistics, benefit from temporary tablespaces. The performance gains are significant with Real Application Clusters.

:-0

Tom Kyte
May 29, 2003 - 1:40 pm UTC

no, aggregates do not have to sort.


that should perhaps say "all operations that might sort, including....."

joins don't have to sort (in fact, I would say most DO NOT)
index creation doesn't always have to sort...
group bys don't have to sort (there is a group by NOSORT)

download the whole table

A reader, May 29, 2003 - 2:37 pm UTC

Hi

in this query

select /*+ USE_MERGE(emp v) */ ename, dname
from emp, dept@db1 v
where emp.deptno = v.deptno;

Entire table dept is transferred to local database right? Is it stored in local temp tablespace? Or where is it stored?



Tom Kyte
May 29, 2003 - 4:36 pm UTC

don't know, don't see a plan. the plan will tell you.

Muhammad, May 29, 2003 - 5:20 pm UTC

Hi,

I want known the codition for which the the remote table will be brought to local database. if the remote table is brought to local database. then what happen to blocks of remote table. is it copied to log_buffer of both the database.

Thanks

Tom Kyte
May 29, 2003 - 7:06 pm UTC

the log buffer ? that is for redo and that will be only on the instance that does the modification.


there are many conditions, more then I'll willing to list that bring a remote table locally.

The data is either used and discarded or it is put into temp to be used in the processing of the query.

Accessing columns from remote database

Balaji, November 17, 2003 - 12:35 am UTC

Hi
My req is slightly complicated.
I am accessing columns from the remote table in the select clause.
SO it is not possible for me to move the table to the subquery.
also replcation of the remote table is not feasible ( 2 milion records) and also not allowed.
is there any was i can access the column.

select c.co1l
c.col2,
c1.col1,
c1.col1
where
c1.col1 in ( select col1 from c@dblink where..)

this cannot be done as c is in x database.
i need c.col1 and col2 in select query
and i am returning this output as a ref cursor.





Tom Kyte
November 17, 2003 - 6:43 am UTC

join

select .... from c1, c@dblink c
where c1.col1 = c.col1



accessing a remote query result to join with some local tables

A reader, May 06, 2004 - 11:45 am UTC

I have the above requirement - I can not use a dblink
unfortunately.
This is being done using JDBC.
One way I could think was to
1. select the remote table result set in Java
and put it into a temporary table
insert into temp_table
<select from remote table here>;
2. Join the temp table with local tables
to get the final results.
3. delete the temp table data.

Can you think of a better way?

btw, are there any "issues" with using dblink
that you are aware of? My hunch is that any performance
issues related to queries using db link are inherent
in the query itself and it certainly can not be
overcome using two queries (one remote and the
other joining local with it - in fact the latter
would be slower since you are doing more work)
Thanx a lot!:)



Re: accessing a remote query result to join with some local tables

A reader, May 06, 2004 - 12:09 pm UTC

I could use a collection( a nested table) instead of
the temporary table to insert my records but I think
the temporary table method is better because:
1. effectively they are doing the same thing - the overhead
of inserts/deletes in temporary table is quite tolerable.
2. you can create indexes, use statistics (as you show
in your book) on temporary tables.


Re: accessing a remote query result to join with some local tables

A reader, May 06, 2004 - 12:46 pm UTC

Sorry
in the solution proposed you can not use "insert into
select" unless you use dblink.
So I guess you will have to a row by row insert in JDBC.

You run the select from remote table
in the JDBC and then for each entry insert into
the temporary table.



thanx!

A reader, May 06, 2004 - 3:31 pm UTC

"funny...

read:
"
Yeah I actually had read that link before posting -
I do think that db links are the way to go. I am not
in a position to find out why these have been ruled
out though. Assuming this is not an option, would you
go with the GTT solution I suggested earlier?:)

Thanx!

Tom Kyte
May 06, 2004 - 8:32 pm UTC

definitely the gtt.

the problem with object types is, they are not "natural" to use in java. A table, thats naturual (simple sql)

thanx!

A reader, May 06, 2004 - 8:40 pm UTC


accessing remote database reference table via view Vs keep the table local

Malar, August 27, 2006 - 12:07 pm UTC

Hi Tom,

Our application has a need to access the remote table (reference table in remote oracle database, which is static and medium sized table).
Both source and destination databases are in oracle9i.

Following are the choices we have in accessing the data from the remote database :

Option 1 :
***********

a) ask external system to send us the daily feed/weekly feed in the form of "pipe delimited text file or export dump file" based on the frequency of table changes. Load daily feed into local database.


Option 2 :
**********

a) Directly access the remote database via JDBC queries
b) Create a view for the remote table in local database using database link.

External system recommends us to use Option 2 mentioned above by saying they can save money in code development.


Appreciate your suggestions/comments on this approach, which is one is the right approach.


Option 3 :
**********

For high availablity and performance reasons/better control on table maintenance, as a last resort (if at all external system cannot send us daily feed), we are planning to use this option of creating materialized views on local database against remote database for periodic sync up.


Appreciate your valuable suggestions/feedback on pros/cons with each approach given above from performance/availability/tuning perspective. Particulary option 2 and 3.



Tom Kyte
August 27, 2006 - 9:16 pm UTC

if option 1/2 are options then so is

option 2.5 - they create a materialized view log, you refresh on whatever period you both feel is "good".

If option 2 can happen, then they can just create a materialized view log on the table and you can refresh. since option 1 is an option - the fact that your view of the data goes stale (the longer between refreshes the more stale), is OK apparently.



ahh - you got it in option three :) I shouldn't have stopped reading.

Malar

A reader, August 27, 2006 - 10:11 pm UTC

Tom,

Thank you.

If you are given Option 2 (from above options), which of the following approach you would take and the reasons why ?

a) Application directly access the remote database

b) Create a view for the remote table in local database using database link and application will query the remote table via local database view.

Appreciate your suggestions/comments on this approach, which is one is the right approach.

Tom Kyte
August 28, 2006 - 8:41 am UTC

I would refuse option 2 since it means your systems availability is now coupled with theirs, your performance is entirely negatively impacted, their performance is negatively impacted.

I would always hide it via a view, but I would fight having to do it - period.

Malar

A reader, August 28, 2006 - 11:08 am UTC

Tom,

Thank you .

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library