开发者

oracle - moving data from to identical database

开发者 https://www.devze.com 2023-04-08 08:37 出处:网络
I have two databases with identical table layouts. There are a dozen or so tables of interest. They are a number of FK between them.

I have two databases with identical table layouts. There are a dozen or so tables of interest. They are a number of FK between them.

I have been asked to write a stored procedure to copy data from database A to database B based on the PK of the parent table at the top of the hierarchy. I may receive just one value, or a list of values. I'm supposed to select all records from database A that match the value(s) and insert/update them into database B. This includes all the records in the child tables too.

My questions is whats the best(most efficent/ best practice) way to do this?

Should I write a dozen select from... insert into... statements?

Should I join the tables together an try to insert into all the tables at the same time?

Thanks!

Additional info: The record should be inserted if it is not already there. (based on the PK of the respective table). Otherwise it should be updated.

Obviously I need to traverse down to all child tables, so There would only be one record to copy in the parent table, but the child table might have 10, and the child's child table might have 500. I would of course need to update the record开发者_Python百科 if it already existed, insert if it does not for the child tables too...

UPDATE: I think it would make the solution simpler if I just deleted all records related to the top level key, and then insert all the new records rather than trying to do updates.

So I guess the questions is it best to just do a dozen:

delete from ... where ... in ...

select from ... where ... in ...
insert into...

or is it better to do some kinda of fancy joins to do all the inserts in one sql statement?


I would do this by disabling all the foreign key constraints, then doing a set of MERGE statements to deal with the updates and inserts, then enable all the constraints.

Think about logging. How much redo do you want to generate?

You might find that it's quicker and better to truncate all the target tables and then do inserts of everything with nolog. Could be simpler than the merges.

One major main alternative would be to drop all the target tables and use export and import. Might be a lot faster.

A second alternative would be to use materialized views, particularly if you don't need to do updates on the target tables. That way, Oracle does all the heavy lifting for you. You can force integrity by choosing refresh groups carefully.

There are several ways to deal with this business problem. A PL/SQL program may not be the best.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号