兩個oracle 10g 數據庫,通過物化視圖同步數據的具體實施流程
源庫是 192.168.1.81
目標庫是192.168.1.96
統計庫用的服務器是 81
在統計庫81上創建只讀用戶snap_query,用於查詢
conn /as sysdba
create user snap_query identified by snap_query
default tablespace users
temporary tablespace temp;
grant connect to snap_query;
grant select on userb.test1 to snap_query;
為表建立實體化視圖日志,要求這些表帶有PRIMARY KEY
CREATE MATERIALIZED VIEW LOG
ON userb.test1
TABLESPACE ts12
WITH PRIMARY KEY
EXCLUDING NEW VALUES;
----------------------------
在測試庫96上
以sys用戶登陸,查看並修改job_queue_processes的值
SQL> show parameter spfile --先檢查是否是spfile(服務器端初始化文件)
SQL> show parameter job_queue_process --如果為零 用以下命令修改
SQL> alter system set job_queue_processes=10 scope=spfile;
創建表空間,用於存放物化視圖內容(即test1)
create tablespace ts_event
datafile 'i:ts_event_01.dbf' size 5000m,
'i:ts_event_02.dbf' size 5000m;
創建用戶
create user ts12_st identified by ts12_st_2010
default tablespace ts_event
temporary tablespace temp;
grant connect,resource,create snapshot to ts12_st;
grant create database link to ts12_st;
在tnsnames.ora中配置網絡別名ts12tj連接到81
創建數據庫鏈
conn ts12_st/ts12_st_2010
create database link DBLINK_SNAP_QUERY
connect to snap_query identified by snap_query
using 'ts12tj';
測試:
select event_id,test1YPE
from userb.test1@DBLINK_SNAP_QUERY where rownum<10
創建物化視圖
CREATE MATERIALIZED VIEW test1_st2
BUILD IMMEDIATE
USING INDEX
REFRESH FORCE
NEXT sysdate + 12/24
AS
SELECT
Event_ID,
EVENT_CODE,
CALL_TIME,
TELE_X,
TELE_Y,
test1YPE,
EVENT_CONTENT,
ROAD_CODE,
PLACE,
DIRECTION,
PLACE_X,
PLACE_Y,
UPDATE_TIME,
MAIN_ASSISTANT_ROAD
FROM userb.test1@DBLINK_SNAP_QUERY;
----------------------------
創建完畢後在統計庫81上執行
grant select on mlog$_test1 to snap_query;
在81上檢查需要更新情況:
select '數據量 '||count(0) from test1 union
select '需更新量 '||count(0) from mlog$_test1;
----------------------------
在96上手工刷新物化視圖方法:
exec dbms_mview.refresh('test1_st');
公司環境測試300萬條耗時1 小時
經過長時間(1年)運行後,日志表mlog$_test1可能需要move以便回收空間。