Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

slow queries with DB links

Status
Not open for further replies.

drkestrel

MIS
Sep 25, 2000
439
GB
I have two DB schemas with same structures, and I would like to compare the contents of one table in the two schemas as follows.

I have created on schema1 (ORACLE_SID=schema1) a Database Link to schema2(ORACLE_SID=schema2) using alias schema2.

I tried running the following queries
Code:
SELECT
    code
FROM
    stock
WHERE
    code
    NOT IN
    (SELECT
        code
     FROM
        stock@schema2);

Unforunately, the query seems to take forever to run.

Note, there are more records in the stock table in schema1 than in schema2, so the number of records needed to be pulled back should be less than that if I have the DB link the other way round.

any ways of speeding up the query?
 
Using EXISTS might speed it up:

SELECT
code
FROM
stock l
WHERE NOT EXISTS
(SELECT 'x' FROM
stock@schema2 r
WHERE r.code = l.code);
 
My guess is that for every row of stock in schema1, a select is done thru the dblink on stock in schema2. If so, your are executing as many SELECT statement thru the dblink on the second machine as there is rows in the schema1.stock table.

Doing:
select code from schema1.stock
minus
select code from schema2.stock;
would give you what is in schema1 and not in 2.
I am pretty shure that by using this approach, you will get the best performance possible.

 

Depending on your application, but you can also check snapshots for this matter.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top