有外键关系但不受外键约束的两个远程表A_REM和B_REM,表A_REM 每天新增10,000行,表B_REM 每天新增50,000行。大多数操作是插入。现在,我想将数据从A_REM和B_REM移动到本地表A_LOC和B_LOC,并在处理时锁定行。移动后,应该删除表A_REM和表B_REM中的行。
A_REM B_REM
1 ----- |_ 1
|_ 2
|_ 3
--------------------
2 ----- |_ 4
|_ 5 rows 2 and 4-6 are locked while moving
|_ 6
--------------------
3 ----- |_ 7
|_ 8
|_ 9
在保持表A_REM和表B_REM之间的关系(一致性)的情况下,移动数据的最佳方法是什么?如果只有一个表,我会将"FOR UPDATE OF"语句与游标(http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/static.htm#CHDGEHBF)结合使用。
提前感谢。
如果事务中只有一个远程数据库,则它不是分布式事务,因此它的行为与本地事务相同(与跨多个远程数据库的分布式事务相反)。下面的示例展示了如何获得适当的行级锁,并在单个工作单元中移动数据,以达到所需的数据一致性。
create table a_rem ( aid number, acontent varchar2(10) );
create table b_rem ( bid number, aid number, bcontent varchar(10) );
create table a_loc ( aid number, acontent varchar2(10) );
create table b_loc ( bid number, aid number, bcontent varchar(10) );
insert into a_rem values ( 1, 'A-One' );
insert into a_rem values ( 2, 'A-Two' );
insert into a_rem values ( 3, 'A-Three' );
insert into b_rem values ( 1, 1, 'B-One' );
insert into b_rem values ( 2, 1, 'B-Two' );
insert into b_rem values ( 3, 2, 'B-Three' );
insert into b_rem values ( 4, 3, 'B-Four' );
insert into b_rem values ( 5, 3, 'B-Five' );
commit;
-- look Ma, no data integrity! :(
-- let us pretend I want to move a_rem.aid = 2 information from both tables
declare
cursor row_level_locks is
select a.*, b.*
from a_rem a, b_rem b
where a.aid = b.aid and a.aid = 2
for update;
begin
open row_level_locks; -- begins transaction, obtains proper row level locking
insert into a_loc select * from a_rem where aid = 2;
insert into b_loc select * from b_rem where aid = 2;
delete a_rem where aid = 2;
delete b_rem where aid = 2;
commit;
close row_level_locks;
end;
最后注意:如果两个表之间的数据完整性很重要,请继续在引用A_REM的B_REM上创建外键约束。如果你不能/不愿意,那么数据完整性就不重要了。它不能既重要又微不足道,以确保不被采取