物化視圖同步數(shù) 1. 用途 現(xiàn)在需要把生產(chǎn)數(shù)據(jù)庫中的部分數(shù)據(jù)同步到數(shù)據(jù)倉庫中,考慮到成本和便捷性,采用定期刷新物化視圖的方
物化視圖同步數(shù) 1.???? 用途
現(xiàn)在需要把生產(chǎn)數(shù)據(jù)庫中的部分數(shù)據(jù)同步到數(shù)據(jù)倉庫中,考慮到成本和便捷性,采用定期刷新物化視圖的方式同步數(shù)據(jù)。
2.???? 整體實施思路
l?? 首先創(chuàng)建一個dblink,可以訪問遠程數(shù)據(jù)庫。
l?? 在本地創(chuàng)建一個物化視圖,存儲遠程數(shù)據(jù)表,當遠程數(shù)據(jù)表有變化時,會定時刷新到物化視圖中。
l?? 創(chuàng)建Oracle job定時刷新表
3.???? 需要同步的表如下
序號
表中文名稱
表名
說明
1
訂單表
OMORDER
訂單主表,保存訂單的基礎信息
2
訂單明細表
ORDERDETAIL
存放訂單里商品的信息
4.1.4.??? 配置步驟
4.1 配置數(shù)據(jù)倉庫中tns連接
testrac =
? (DESCRIPTION =
??? (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.100.122)(PORT = 1521))
??? (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.100.123)(PORT = 1521))
??? (LOAD_BALANCE = yes)
??? (CONNECT_DATA =
????? (SERVER = DEDICATED)
????? (SERVICE_NAME = jscn)
??? )
? )
?
?
測試:
[oracle@testrac1 goldengate]$ sqlplus sys/sys@ testrac as sysdba
SQL> select count(*) from SYS.GV_$INSTANCE
4.2 創(chuàng)建dblink
語法:
create public database link dblink_nameconnect to username identified by passwd using ‘tns_name’
說明:
dblink_name :是dblink名稱
username:是遠端的用戶名
passwd:是遠端的秘密
‘tns_name’ :是tnsnames.ora中的連接字符串名稱
?
示例如下:
?
SQL> create public database link testrac connect to jscn identified by jscn? using ‘testrac’;
SQL> select count(*) from SYS.GV_$INSTANCE@testrac;
?
? COUNT(*)
———-
? 2
?
?
4.3 創(chuàng)建物化視圖同步數(shù)據(jù)
4.3.1 在源用戶上執(zhí)行以下,創(chuàng)建物化視圖日志
說明:
對于已定義主鍵的表,分別執(zhí)行以下 sql:
??? create materialized view log on 表名? withprimary key;??
???????? ???????? 對于未定義主鍵的表,,分別執(zhí)行以下 sql:
???? creatematerialized view log on 表名? with rowid;
???????? 使用sql語句:
?????????????????? select ‘creatematerialized view log on ‘||table_name||’ withprimary key;’ fromuser_tables;
create materialized view log on thh with primary key;
–這兩個沒有主鍵,特殊對待
create materialized view log on USER_ORDER_INTE with rowid;
4.3.2 在目標用戶上執(zhí)行以下 sql,創(chuàng)建物化視圖
創(chuàng)建物化視圖語法如下:
create materialized view [view_name]
refresh [fast|complete|force] with [primaryid | rowid]
[
on [commit|demand] |
start with (start_time) next (next_time)
]
as
{創(chuàng)建物化視圖用的查詢語句}
?
???????? 使用sql語句:
select ‘create materializedview ‘||table_name||’? refresh fast?on demand? as select * fromthh@testrac;’ fromuser_tables;
create materialized view CONSIGNEE_ADDRESS? refresh fast? on demand? as select * from thh@testrac;
create materialized view CUSTOMER? refresh fast? on demand? as select * from thh@testrac;
?
4.3.3 ?? 創(chuàng)建定時任務
定時任務放在每天凌晨3點執(zhí)行任務。
BEGIN
?? DBMS_MVIEW.refresh (
????? LIST=> ‘OMORDER,ORDERDETAIL,ORDERTRACER,THH,THH_SUB,VACCOUNTREASON,CONSIGNEE_ADDRESS,LOGISTICS_COMPANY,PRODUCT,PRODUCT_CATE_REL,PRODUCTCATEGORY,STORE,STORE_INFO,CUSTOMER,CUSTOMER_INFO,USER_ORDER_INTE,GIFT_CARDS,GIFT_CARDS_TYPE,GIFT_CARDS_LOG,PROMOTIONS_OFFERS,JOIN_PROMOTIONS,TEL_EMAIL_RECORD,dealcategory,YDORDER,YDORDERTRACER,PRODUCT_BRAND,THH_LOGS’,
????? METHOD??????? => ‘F’,
????? PARALLELISM?? => 1);
END;
更多Oracle相關信息見Oracle 專題頁面 ?tid=12