談oracle資料比對(DBMS_COMPARISON),dbmscomparison

來源:互聯網
上載者:User

談oracle資料比對(DBMS_COMPARISON),dbmscomparison

    今天是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.

以上是說了比較容易理解的限制,下面在說一下索引列的限制:

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 scott.emp@comparison_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 SCOTT.EMP@COMPARISON_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 資料庫資料的比較對

自己寫SQL語句很簡單,A表是你原來的表,B表是新資料表,F是你的資料欄位
select count(*) from a inner join b where a.f= b.f
select count(*) from a
比較一下第一個結果是不是大於等於第二個就可以了,如果小於第二個,那肯定沒導全
如果F是唯一值的欄位,那隻能相等才導全,如果不是唯一欄位,相信到現在你肯定知道應該怎麼做了。
 
ORACLE下資料比對,找出A已指派B未分配的資料行

能問一句,你要的是B已指派列的資料在A已指派列中存在,而且A保函B的資料的話就把被保函的B的資料給想A一樣的拼起來是嗎?

SELECT
t1.id,
'所需列' AS typ
FROM t1
INNER JOIN
(
select
t.ID,
LISTAGG(t.B,',') WITHIN GROUP (order by t.B) As A
from
(
SELECT
distinct
t.ID,
t.B
from
t
)GROUP BY t.id
) t2
ON t1.ID = t2.ID
AND t1.A <> t2.A
GROUP BY t1.id
樓主你試一下這一段。
不好意思上面那段SQL文我有修改,我把外面接改成了內串連,就是只把不等於的資料給顯示出來了。
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.