在兩個資料庫之間進行資料同步

壹頁書發表於2013-11-25
OLAP的第一步就是從業務系統中抽取資料到資料倉儲系統。
除了ETL工具Kettle,也可以使用PL/SQL

建立Source表,模擬業務系統的資料表。
create table source as select sys_guid() id ,o.* from dba_objects o where rownum<1000;
alter table source add constraint PK_source primary key (id);

建立Target表,模擬資料倉儲中的表。
create table target as select * from source where 1=0;
alter table target add constraint PK_target primary key (id);

建立DB LINK,remote模擬業務系統的資料庫
create database link remote connect to username identified by xxxxxx using 'remote';

因為業務系統的資料是變化的,相較於資料倉儲的表,他可能更新了一些資料,也可能修改了一些資料。
比對業務表和資料倉儲表的資料,
    如果ID相同,並且資料有變化
        則根據ID更新資料倉儲的表(target)
    如果業務系統的資料ID(source),還沒有出現在資料倉儲的表中(target)
        則在資料倉儲的表中新增這個記錄。

本質都是Oracle Merge的功能,只不過嘗試另外幾種方法。

1.merge

  1. merge into target t using (select * from source@remote) s
  2. on(t.id=s.id)
  3. when matched
  4. then
  5.     update set  t.owner=s.owner,
  6.                 t.object_name=s.object_name,
  7.                 t.subobject_name=s.subobject_name,
  8.                 t.object_id=s.object_id,
  9.                 t.data_object_id=s.data_object_id,
  10.                 t.object_type=s.object_type,
  11.                 t.created=s.created,
  12.                 t.last_ddl_time=s.last_ddl_time,
  13.                 t.timestamp=s.timestamp,
  14.                 t.status=s.status,
  15.                 t.temporary=s.temporary,
  16.                 t.generated=s.generated,
  17.                 t.secondary=s.secondary,
  18.                 t.namespace=s.namespace,
  19.                 t.edition_name=s.edition_name
  20. when not matched
  21. then
  22.     insert values
  23.     (    
  24.                 s.id,
  25.                 s.owner,
  26.                 s.object_name,
  27.                 s.subobject_name,
  28.                 s.object_id,
  29.                 s.data_object_id,
  30.                 s.object_type,
  31.                 s.created,
  32.                 s.last_ddl_time,
  33.                 s.timestamp,
  34.                 s.status,
  35.                 s.temporary,
  36.                 s.generated,
  37.                 s.secondary,
  38.                 s.namespace,
  39.                 s.edition_name
  40.     );
2.全域性臨時表。
    首先將遠端業務系統的資料放入臨時表,
    然後根據ID更新資料,如果資料的內容沒有變化,則不更新。
    最後插入業務系統中新建的資料。

  1. create global temporary table tmp
  2. on commit preserve rows
  3. as
  4. select * from target where 1=0;

  5. insert into tmp select * from source@remote;

  6. update target t set
  7.             (
  8.                 t.owner,
  9.                 t.object_name,
  10.                 t.subobject_name,
  11.                 t.object_id,
  12.                 t.data_object_id,
  13.                 t.object_type,
  14.                 t.created,
  15.                 t.last_ddl_time,
  16.                 t.timestamp,
  17.                 t.status,
  18.                 t.temporary,
  19.                 t.generated,
  20.                 t.secondary,
  21.                 t.namespace,
  22.                 t.edition_name
  23.             )
  24.             =
  25.             (select
  26.                 tmp.owner,
  27.                 tmp.object_name,
  28.                 tmp.subobject_name,
  29.                 tmp.object_id,
  30.                 tmp.data_object_id,
  31.                 tmp.object_type,
  32.                 tmp.created,
  33.                 tmp.last_ddl_time,
  34.                 tmp.timestamp,
  35.                 tmp.status,
  36.                 tmp.temporary,
  37.                 tmp.generated,
  38.                 tmp.secondary,
  39.                 tmp.namespace,
  40.                 tmp.edition_name            
  41.             from tmp where t.id=tmp.id)
  42.     where exists(
  43.                     select * from tmp where tmp.id=t.id and not (
  44.                                 tmp.owner=t.owner and
  45.                                 tmp.object_name=t.object_name and
  46.                                 tmp.subobject_name=t.subobject_name and
  47.                                 tmp.object_id=t.object_id and
  48.                                 tmp.data_object_id=t.data_object_id and
  49.                                 tmp.object_type=t.object_type and
  50.                                 tmp.created=t.created and
  51.                                 tmp.last_ddl_time=t.last_ddl_time and
  52.                                 tmp.timestamp=t.timestamp and
  53.                                 tmp.status=t.status and
  54.                                 tmp.temporary=t.temporary and
  55.                                 tmp.generated=t.generated and
  56.                                 tmp.secondary=t.secondary and
  57.                                 tmp.namespace=t.namespace and
  58.                                 tmp.edition_name=t.edition_name
  59.                     )
  60.                 );                                    

  61. insert into target
  62.     select * from tmp where not exists(
  63.         select * from target t where t.id=tmp.id);
3.集合處理
    為了簡單,沒有進行內容變化的判斷
  1. declare
  2.     type tab is table of target%rowtype;
  3.     l_row tab;
  4.     cursor cur is select * from source@remote;
  5. begin
  6.     open cur;
  7.     fetch cur bulk collect into l_row;
  8.     close cur;
  9.     forall i in 1..l_row.count
  10.         update target set row=l_row(i) where id=l_row(i).id;
  11.     insert into target select * from source@remote s
  12.         where not exists (select * from target t where t.id=s.id);
  13.     commit;
  14. end;
  15. /
4.內聯檢視更新
    沒有寫更新後插入的步驟,插入的實現是相同的。
  1. update(
  2.     select
  3.         s.id s1,
  4.         s.owner s2,
  5.         s.object_name s3,
  6.         s.subobject_name s4,
  7.         s.object_id s5,
  8.         s.data_object_id s6,
  9.         s.object_type s7,
  10.         s.created s8,
  11.         s.last_ddl_time s9,
  12.         s.timestamp s10,
  13.         s.status s11,
  14.         s.temporary s12,
  15.         s.generated s13,
  16.         s.secondary s14,
  17.         s.namespace s15,
  18.         s.edition_name s16,
  19.         t.id t1,
  20.         t.owner t2,
  21.         t.object_name t3,
  22.         t.subobject_name t4,
  23.         t.object_id t5,
  24.         t.data_object_id t6,
  25.         t.object_type t7,
  26.         t.created t8,
  27.         t.last_ddl_time t9,
  28.         t.timestamp t10,
  29.         t.status t11,
  30.         t.temporary t12,
  31.         t.generated t13,
  32.         t.secondary t14,
  33.         t.namespace t15,
  34.         t.edition_name t16
  35.     from target t inner join source@remote s on(s.id=t.id)
  36.     where     
  37.         not
  38.         (
  39.             s.owner=t.owner and
  40.             s.object_name=t.object_name and
  41.             s.subobject_name=t.subobject_name and
  42.             s.object_id=t.object_id and
  43.             s.data_object_id=t.data_object_id and
  44.             s.object_type=t.object_type and
  45.             s.created=t.created and
  46.             s.last_ddl_time=t.last_ddl_time and
  47.             s.timestamp=t.timestamp and
  48.             s.status=t.status and
  49.             s.temporary=t.temporary and
  50.             s.generated=t.generated and
  51.             s.secondary=t.secondary and
  52.             s.namespace=t.namespace and
  53.             s.edition_name=t.edition_name
  54.         )
  55. )
  56. set
  57.     t1=s1,
  58.     t2=s2,
  59.     t3=s3,
  60.     t4=s4,
  61.     t5=s5,
  62.     t6=s6,
  63.     t7=s7,
  64.     t8=s8,
  65.     t9=s9,
  66.     t10=s10,
  67.     t11=s11,
  68.     t12=s12,
  69.     t13=s13,
  70.     t14=s14,
  71.     t15=s15,
  72.     t16=s16
  73. ;
5.Minus
    先插入業務表中新增的記錄,然後對比修改。
  1. declare
  2.     type tab is table of target%rowtype;
  3.     l_row tab;
  4.     cursor cur is select * from source@remote minus select * from target;
  5. begin
  6.     insert into target select * from source@remote s
  7.         where not exists (select * from target t where t.id=s.id);
  8.     open cur;
  9.     fetch cur bulk cllect into l_row;
  10.     close cur;
  11.     forall i in 1..l_row.count
  12.         update target set row=low(i) where id=l_row(i).id;
  13.     commit;
  14. end;
  15. /

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-777461/,如需轉載,請註明出處,否則將追究法律責任。

相關文章