英文:
Reading changes done in table and replicate them to another system
问题
我必须通过调用API的POST方法服务将数据插入Oracle数字助手(ODA)。我正在从Oracle表中读取所有在一天内发生的所有交易数据,然后需要将其插入到ODA
(将其视为另一个数据库
)中。
我在主表上创建了一个触发器,它将把一天内发生的所有交易插入到另一个表中。使用jdbc
,我将读取此表并将数据写入文件
中。然后,我将调用API
,该API将向ODA
中注入数据。一旦数据插入到ODA
中,我将截断该表
,以便第二天插入新的数据
。
我在主表上编写触发器,所以在表
上发生的任何DML操作都需要被插入到另一个表
中。我也没有意识到我必须反映更新或删除所做的更改。
在读取所有更改后,我将截断存储更改的复制表。
是否有比触发器DML更好的方法?
英文:
I have to insert data into Oracle digital assistant(ODA) by calling an API POST method service. I am reading data from an Oracle table where all the transactions happened in a day, and my system needs to be inserted into ODA
(Consider it as another database
).
I have created a trigger
on the main table which will insert all the transaction which happens in a day to another table. Using jdbc
I will read this table and write the data into file
. Then I will call the API
which will inject the data into ODA
. Once data will be inserted into ODA
I will truncate the table
so that next day it inserts
fresh data
.
I am writing trigger on main table so any DML operation happened on the table
needs to be inserted into another table
. I was also not aware that I had to reflect the changes done by updates or deletes.
After reading all changes I will truncate the replicated table where the changes are located.
Is there any better approach that a trigger DML ?
答案1
得分: 1
我不会在这种情况下使用触发器。如果主表中有删除或更新操作,并且您仅对插入操作使用触发器,会发生什么情况呢?在这种情况下,我会使用一个"物化视图日志"来获取更改情况。
让我来演示一下:
SQL> create table t ( c1 number primary key, c2 number ) ;
Table created.
SQL> declare
begin
for i in 1 .. 100000
loop
insert into t values ( i , dbms_random.value ) ;
end loop;
commit ;
end;
/ 2 3 4 5 6 7 8 9
PL/SQL procedure successfully completed.
SQL> create materialized view log on t with primary key ;
Materialized view log created.
SQL> select count(*) from t ;
COUNT(*)
----------
100000
现在我们有一个物化视图日志,它将使用主键复制源表中的所有更改情况。
SQL> insert into t values ( 100001 , dbms_random.value ) ;
1 row created.
SQL> select count(*) from t ;
COUNT(*)
----------
100001
SQL> select count(*) from mlog$_t ;
COUNT(*)
----------
1
现在,在晚上,您只想反映目标物化视图中的更改情况。
1.首先锁定原始表以避免任何操作
SQL> LOCK TABLE MY_TEST ;
2.更改在日志物化视图中
SQL> select * from t where c1 in ( select c1 from mlog$_t ) ;
C1 C2
---------- ----------
10000! .276556329
3.一旦将所有更改复制到API中,然后清除日志
SQL> BEGIN
DBMS_SNAPSHOT.PURGE_LOG (
master => 'T',
num => 1,
flag => 'DELETE');
END;
/ 2 3 4 5 6 7
PL/SQL procedure successfully completed.
SQL> select count(*) from mlog$_t ;
COUNT(*)
----------
0
通过这样做,您可以确保所有更改都被覆盖。
在您的情况下,我认为您甚至不需要物化视图,只需要物化视图日志。
英文:
I won't use a trigger in this case. What happens if you have deletes or updates in the main table and you use only a trigger for inserts ? In this case, I would use a MATERIALIZED VIEW LOG to get the changes
let me show you how
SQL> create table t ( c1 number primary key, c2 number ) ;
Table created.
SQL> declare
begin
for i in 1 .. 100000
loop
insert into t values ( i , dbms_random.value ) ;
end loop;
commit ;
end;
/ 2 3 4 5 6 7 8 9
PL/SQL procedure successfully completed.
SQL> create materialized view log on t with primary key ;
Materialized view log created.
SQL> select count(*) from t ;
COUNT(*)
----------
100000
Now we have a materialized view log which will replicate all changes done in the source table using the primary key
SQL> insert into t values ( 100001 , dbms_random.value ) ;
1 row created.
SQL> select count(*) from t ;
COUNT(*)
----------
100001
SQL> select count(*) from mlog$_t ;
COUNT(*)
----------
1
Now, during the night you want to reflect only the changes done in the target materialized view
1.First lock the original table to avoid any operation
SQL> LOCK TABLE MY_TEST ;
2.Changes are in the log mview
SQL> select * from t where c1 in ( select c1 from mlog$_t ) ;
C1 C2
---------- ----------
10000! .276556329
3.Once you got all changes copied to the API , then purge the log
SQL> BEGIN
DBMS_SNAPSHOT.PURGE_LOG (
master => 'T',
num => 1,
flag => 'DELETE');
END;
/ 2 3 4 5 6 7
PL/SQL procedure successfully completed.
SQL> select count(*) from mlog$_t ;
COUNT(*)
----------
0
With this you will be sure that all changes are covered.
In your case, I think you don't even need the materialized view, only the materialized view log
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论