標籤:code res create grant ram 過程 type ora raw
今天是2014-08-19,我今天收到csdn給我發的申請部落格專家的邀請,自己感覺實在羞愧啊。
自從換了工作也一直沒有精力在寫點東西了。今天我一個同事,在群裡貼出了一個資料比對的包(DBMS_COMPARISON),可是這個包相比用的比較少。
所以今天就談談這個工具包的使用吧。
對於常常完資料移轉的朋友來說,在資料挪動之後,最重要也是最關鍵和最關心的一個問題是,目標端和源端的資料是否一致。資料的一致是否關係著大型oracle資料庫資料移轉的成敗與否。眼下非常多公司都開始研發自己的對照工具。如dsg的基於rowid的比對、基於minus的比對等等。可是資料庫本身也是給我們提供了一個資料比對的介面,那就是這個DBMS_COMPARISON軟體包。
DBMS_COMPARISION簡單介紹:
這個軟體包是oracle提供的能夠再兩個資料之間做object是比對。而且呢假設在比對過程中假設源端資料和目標端資料不一致。那麼能夠選擇是從源端在將資料拷貝到目標端。還是從目標端在拷貝到源端。終於達到資料一致性的結果。該包也是通過建立dblink來實現的。這個工具的使用大體分為四步:
第一步:使用create_compare去建立一個比對動作
第二步:使用compare函數去進行資料對象之間的比對
第三步:我們在去查看比對結果,對應的record會記錄到不同視圖中例如以下:
DBA_COMPARISON_SCAN
USER_COMPARISON_SCAN
DBA_COMPARISON_SCAN_VALUES
USER_COMPARISON_SCAN_VALUES
DBA_COMPARISON_ROW_DIF
USER_COMPARISON_ROW_DIF
第四不:假設資料不一致,那麼能夠使用convert去將資料同步
大家可能會說,假設我進行了兩次資料比對,那麼怎樣區分呢,這就是oracle自己會給你設計一個標示了。這個函數是recheck。
興許介紹:
另一個問題。那就是這個包能做哪些資料比對?
答案是:對錶、視圖、物化視圖、同義字等
DBMS_COMPARISION限制:
當然了不論什麼一個工具都有自己的限制,那麼這個包呢?
1、對於源端資料庫版本號碼必須是高於11.1,對於目標端資料庫版本號碼必須高於10.1
2.對於全部比對的資料庫物件,必須是共用對象。也就是說每一個對象的列個數和列的類型必須一致。假設列不一致,那麼須要將比對的列使用column_list做個列表。
Database objects of different types can be compared and converged at different databases. For example, a table at one database and a materialized view at another database can be compared and converged with this package.
以上是說了比較easy理解的限制,以下在說一下索引列的限制:
1、在全庫比對模式下,必需要有一個在 number, timestamp, interval, or DATE 資料類型的單一索引列,或是唯獨一個包含這幾種資料類型的複合索引,可是這個複合索引中設計到的列必須都是not null或是當中一列是一個主鍵列。
2、
For the scan modes CMP_SCAN_MODE_FULL and CMP_SCAN_MODE_CUSTOM to be supported, the database objects must have one of the following types of indexes:
A single-column index on a number, timestamp, interval, DATE, VARCHAR2, or CHAR data type column
A composite index that only includes number, timestamp, interval, DATE,VARCHAR2, or CHAR columns. Each column in the composite index must either have aNOT NULL constraint or be part of the primary key.
假設資料庫沒有滿足這些要求。那麼這個包將無法進行資料比對。
if the database objects have only one index, and it is a composite index that includes aNUMBER column and an NCHAR column, then the DBMS_COMPARISON package does not support them.
If these constraints are not present on a table, then use the index_schema_name andindex_name parameters in the CREATE_COMPARISON procedure to specify an index whose columns satisfy this requirement.
When a single index value identifies both a local row and a remote row, the two rows must be copies of the same row in the replicated tables. In addition, each pair of copies of the same row must always have the same index value.
DBms_comparison不支援的資料類型:
LONG、LANG RAW、ROWID、urowid、clob、nclob、blob、bfile另外還有例如以下兩種:
1、udt(user-defined types,including object types, REFs, varrays, and nested tables)
2、oracle-supplied type (including any types, XML types, spatial types, and media types)
好了。瞭解這些後,我們就開始去親自做一下,光說不練那不行。
第一建立dblink:
SQL> select * from dba_sys_privs rhys where rhys.privilege like upper(‘%link%‘);GRANTEE PRIVILEGE ADM------------------------------ ---------------------------------------- ---SYS DROP PUBLIC DATABASE LINK NOSYS CREATE DATABASE LINK NOOWB$CLIENT CREATE DATABASE LINK NOIMP_FULL_DATABASE CREATE PUBLIC DATABASE LINK NORECOVERY_CATALOG_OWNER CREATE DATABASE LINK NODBA DROP PUBLIC DATABASE LINK YESIMP_FULL_DATABASE DROP PUBLIC DATABASE LINK NOOWBSYS CREATE DATABASE LINK YESIMP_FULL_DATABASE CREATE DATABASE LINK NOSYS CREATE PUBLIC DATABASE LINK NODBA CREATE PUBLIC DATABASE LINK YESGRANTEE PRIVILEGE ADM------------------------------ ---------------------------------------- ---DBA CREATE DATABASE LINK YES12 rows selected.SQL> grant create database link to scott;Grant succeeded.
SQL> create database link comparison_link connect to scott identified by root using ‘orac1‘;Database link created.SQL> show userUSER is "SCOTT"SQL> select * from [email protected]_link; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DEP---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ------------------------------ 7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7839 KING PRESIDENT 17-NOV-81 5000 10 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DEP---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ------------------------------ 7902 AMY ANALYST 7566 03-DEC-81 3000 2012 rows selected.SQL>
第二步建立比對任務:
對了忘記提一下許可權了。對於該包,要有例如以下許可權:
SQL> grant execute on dbms_comparison to scott;Grant succeeded.SQL> grant execute_catalog_role to scott;Grant succeeded.SQL>
SQL> begin 2 dbms_comparison.create_comparison( 3 comparison_name=>‘test1‘, 4 schema_name=>‘SCOTT‘, 5 object_name=>‘DEPT‘, 6 dblink_name=>‘comparison_link‘ 7 ); 8 end; 9 /PL/SQL procedure successfully completed.SQL>
好這樣就做完第一步了。
當源端和目標端資料對象的列不一致的情況會出現例如以下錯誤:
SQL> begin 2 dbms_comparison.create_comparison( 3 comparison_name=>‘test1‘, 4 schema_name=>‘SCOTT‘, 5 object_name=>‘EMP‘, 6 dblink_name=>‘comparison_link‘ 7 ); 8 end; 9 / begin*ERROR at line 1:ORA-23625: Table shapes of SCOTT.EMP and [email protected]_LINK did not match.ORA-06512: at "SYS.DBMS_COMPARISON", line 5008ORA-06512: at "SYS.DBMS_COMPARISON", line 448ORA-06512: at line 2
那麼怎麼辦呢?和我說的是做一個column_list;
第二步開始進行資料比對:
SQL> declare 2 compare_info dbms_comparison.comparison_type; 3 compare_return boolean; 4 begin 5 compare_return := dbms_comparison.compare (comparison_name=>‘test1‘, 6 scan_info=>compare_info, 7 perform_row_dif=>TRUE); 8 9 if compare_return=TRUE 10 then 11 dbms_output.put_line(‘the tables are equivalent.‘); 12 else 13 dbms_output.put_line(‘Bad news... there is data divergence.‘); 14 dbms_output.put_line(‘Check the dba_comparison and dba_comparison_scan_summary views for locate the differences for scan_id:‘||compare_info.scan_id); 15 end if; 16 end; 17 /the tables are equivalent.PL/SQL procedure successfully completed.SQL>
第三步查看比對結果:
SQL> select * from user_comparison_scan 2 ;COMPARISON_NAME SCAN_ID PARENT_SCAN_ID ROOT_SCAN_ID STATUS CURRENT_DIF_COUNT INITIAL_DIF_COUNT COUNT_ROWS S LAST_UPDATE_TIME-------------------- ---------- -------------- ------------ ---------------- ----------------- ----------------- ---------- - ----------------------------------------TEST1 1 1 SUC 0 0 4 N 19-AUG-14 11.05.42.780593 PMTEST1 2 2 SUC 0 0 4 N 19-AUG-14 11.11.37.613343 PMSQL> select * from user_comparison_row_dif;no rows selectedSQL>
好了,簡簡單單就到這了。
當然假設敢興趣能夠自己在測試其它的。
我要把資料清掉了:
SQL> begin 2 dbms_comparison.purge_comparison( 3 comparison_name=>‘test1‘); 4 end; 5 /PL/SQL procedure successfully completed.SQL> select * from user_comparison_scan;no rows selectedSQL>
that‘s all!
談oracle資料比對(DBMS_COMPARISON)