Oracle: Insertion on an indexed table, avoiding duplicates. Looking for tips and advice.

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2010-03-29T18:02:29Z Indexed on 2010/03/29 18:43 UTC
Read the original article Hit count: 341

Filed under:
|
|

Hi everyone,

Im looking for the best solution (performance wise) to achieve this.

I have to insert records into a table, avoiding duplicates.

For example, take table A

Insert into A (
 Select DISTINCT [FIELDS] from B,C,D.. 
 WHERE (JOIN CONDITIONS ON B,C,D..)
 AND 
 NOT EXISTS
 ( 
   SELECT * FROM A ATMP WHERE
   ATMP.SOMEKEY = A.SOMEKEY
 )
);

I have an index over A.SOMEKEY, just to optimize the NOT EXISTS query, but i realize that inserting on an indexed table will be a performance hit.

So I was thinking of duplicating Table A in a Global Temporary Table, where I would keep the index. Then, removing the index from Table A and executing the query, but modified

Insert into A (
 Select DISTINCT [FIELDS] from B,C,D.. 
 WHERE (JOIN CONDITIONS ON B,C,D..)
 AND 
 NOT EXISTS
 ( 
   SELECT * FROM GLOBAL_TEMPORARY_TABLE_A ATMP WHERE
   ATMP.SOMEKEY = A.SOMEKEY
 )
);

This would solve the "inserting on an index table", but I would have to update the Global Temporary A with each insertion I make.

I'm kind of lost here,

Is there a better way to achieve this?

Thanks in advance,

© Stack Overflow or respective owner

Related posts about Oracle

Related posts about plsql