Oracle GoldenGate 针对表没有主键或唯一索引的解决方案

发布时间:2020-08-13 16:10:46

来源:ITPUB博客

阅读:200

作者:不一样的天空w

http://blog.itpub.net/28258625/viewspace-2060713

This knowledge document describes a best practice method for handling tables without primary keys or unique indexes when using Oracle GoldenGate to replicate transactional data between Oracle databases.

There is a

change log

at the end of this document.

In This Document

This document is divided into the following sections:

Section 1: Overview

Section 2: Configuring Tables without PKs or UIs

Section 3: References

Appendix A: Sample Table Configuration

Section 1:

Overview

1.1 Solution Summary

In order to maintain data integrity when replicating transactional data, Oracle GoldenGate will use primary key columns or unique index columns to uniquely identify a row when issuing update or delete statements against the target database. If no primary keys or unique indexes exist on the table being replicated, Oracle GoldenGate will use all columns to uniquely identify a row.

It is perfectly acceptable to use all columns to uniquely identify a row under the following conditions:

A logical key column cannot be defined for the table using the KEYCOLS parameter.

No duplicate rows exist in the table

Table contains a small number of rows, so full table lookups on the target database are minimal

Table DML activity is very low, so "all column" table supplemental log groups do not negatively impact the source database redo logs

If the table being replicated does not meet all of the conditions listed above, it is recommended to add a column to the table with a SYS_GUID default value to uniquely identify the row. The next section describes the detailed steps on how to configure a table without a primary key or unique index in order to uniquely identify each row.

1.2 Required Software Components

Software Component

Minimum Version

Oracle Database

10.2 or greater

Oracle GoldenGate

10.4 or greater

Section 2:

Configuring Tables without PKs or UIs

2.1 Configure Table(s) in Source Database

Before instantiating the target database from a copy of source, perform. the following steps to the table(s) without primary keys or unique indexes.

Step 1 - Add Column to Table

Issue the following command to add the column to the table.

Replace with the table name being modified.

alter table add OGG_KEY_ID raw(16);

Step 2 - Modify Column Data Default to Use SYS_GUID Values

Issue the following command to modify the OGG_KEY_ID default value to use SYS_GUID values. SYS_GUID values are unique values generated from an internal Oracle algorithm.

Immediately after modifying the OGG_KEY_ID column, newly inserted rows will automatically populate the OGG_KEY_ID column with SYS_GUID values.

Replace with the table name being modified.

alter table

Note

: DO NOT combine steps 1 and 2 into 1 SQL statement. Doing so will cause Oracle to populate the OGG_KEY_ID column with a full table lock. The table could be locked for a significant amount of time based on the number of existing rows in the table.

Step 3 - Backfill Existing Table Rows with SYS_GUID Values

Now that newly inserted rows are being handled, the OGG_KEY_ID column still needs to be populated for existing table rows. The following SQL script. will backfill the existing table rows with unique SYS_GUID values.

Replace

with the table name being updated.

DECLARE cursor C1 is select ROWID from

where OGG_KEY_ID is null;

finished number:=0; commit_cnt number:=0; err_msg varchar2(150);

snapshot_too_old exception; pragma exception_init(snapshot_too_old, -1555);

old_size number:=0; current_size number:=0;

BEGIN

while (finished=0) loop

finished:=1;

BEGIN

for C1REC in C1 LOOP

update

set GG_PK_ID = sys_guid()

where ROWID = C1REC.ROWID;

commit_cnt:= commit_cnt + 1;

IF (commit_cnt = 10000) then

commit;

commit_cnt:=0;

END IF;

END LOOP;

EXCEPTION

when snapshot_too_old then

finished:=0;

when others then

rollback;

err_msg:=substr(sqlerrm,1,150);

raise_application_error(-20555, err_msg);

END;

END LOOP;

IF(commit_cnt > 0) then

commit;

END IF;

END;

/

Note

: This process could take a significant amount of time to complete based on the number of existing rows in the table. The script. will not perform. a full table lock and will only lock each row exclusively as it updates it.

Step 4 - Create Table Supplemental Log Group

in Source Database

Issue the following commands in GGSCI to create the table supplemental log group using the OGG_KEY_ID column. This forces Oracle to always write the OGG_KEY_ID value to the online redo logs.

Replace

with source database username.

Replace

with source database user's password.

Replace

with table schema owner.

Replace

with the table name being modified.

GGSCI> dblogin userid

, password

GGSCI> add trandata

.

, COLS (OGG_KEY_ID), nokey

2.2 Configure Table in Target Database

Step 1 - Create Unique Index on Target Table

After instantiating the target database, issue the following command to add a unique index to the table. The unique index prevents full table scans when applying update and delete statements to the target database.

Replace

with the table name the unique index is on.

Replace

with tablespace name to store unique index.

create unique index OGG_

_UI on

(OGG_KEY_ID) logging online tablespace

Note

: This process could take a significant amount of time to complete based on the number of existing rows in the table.

2.3 Oracle GoldenGate Configuration

Step 1 - Specify OGG_KEY_ID for Table Key in Extract Parameter File

Use the KEYCOLS parameter in the Extract parameter file to define OGG_KEY_ID as the unique column for the table.

Replace

with the table name the unique index is on.

TABLE

, KEYCOLS (OGG_KEY_ID);

Section 3: References

For further information about using Oracle GoldeGate, refer to the following documents available on edelivery:

Oracle GoldenGate Installation and Setup Guides

Oracle GoldenGate Administration Guide

Introduces Oracle GoldenGate components and explains how to plan for, configure, and implement Oracle GoldenGate.

Oracle GoldenGate Reference Guide

Provides detailed information about Oracle GoldenGate parameters, commands, and functions.

Oracle GoldenGate Troubleshooting and Performance Tuning Guide

Provides suggestions for improving the performance of Oracle GoldenGate in different situations, and provides solutions to common problems.

Appendix A:

Sample Table Configuration

Add Column to Table

SQL> SELECT * FROM NL_WEST;

TEAM_NAME         LOCATION

----------------  --------------------

PADRES            SAN DIEGO

ROCKIES           COLORADO

DODGERS           LOS ANGELES

DIAMONDBACKS      ARIZONA

SQL> alter table NL_WEST add OGG_KEY_ID raw(16);

Table altered.

SQL> select * from NL_WEST;

TEAM_NAME         LOCATION            OGG_KEY_ID

----------------  ------------------  ----------------

PADRES            SAN DIEGO

ROCKIES           COLORADO

DODGERS           LOS ANGELES

DIAMONDBACKS      ARIZONA

Modify Column Data Default to Use SYS_GUID() Values

SQL> alter table NL_WEST modify OGG_KEY_ID default sys_guid();

Table altered.

SQL> select * from NL_WEST;

TEAM_NAME         LOCATION            OGG_KEY_ID

---------------   ------------------  ----------------

PADRES            SAN DIEGO

ROCKIES           COLORADO

DODGERS           LOS ANGELES

DIAMONDBACKS      ARIZONA

SQL> INSERT INTO NL_WEST (TEAM_NAME,LOCATION) VALUES ('GIANTS','SAN FRANCISCO');

1 row created.

SQL> COMMIT;

Commit complete.

SQL> select * from NL_WEST;

TEAM_NAME         LOCATION          OGG_KEY_ID

----------------  ----------------  ----------------

PADRES            SAN DIEGO

ROCKIES           COLORADO

DODGERS           LOS ANGELES

DIAMONDBACKS      ARIZONA

GIANTS            SAN FRANCISCO     95AB2831E724991FE040C8C86E0162D0

Backfill Existing Table Rows with SYS_GUID() Values

SQL> DECLARE cursor C1 is select ROWID from

NL_WEST

where OGG_KEY_ID is null;

2 3 4 finished number:=0; commit_cnt number:=0; err_msg varchar2(150);

5 snapshot_too_old exception; pragma exception_init(snapshot_too_old, -1555);

6 old_size number:=0; current_size number:=0;

7 BEGIN

8 while (finished=0) loop

9 finished:=1;

10 BEGIN

11 for C1REC in C1 LOOP

12 update NL_WEST

13 set OGG_KEY_ID = sys_guid()

14 where ROWID = C1REC.ROWID;

15 commit_cnt:= commit_cnt + 1;

16 IF (commit_cnt = 10000) then

17 commit;

18 commit_cnt:=0;

19 END IF;

20 END LOOP;

21 EXCEPTION

22 when snapshot_too_old then

23 finished:=0;

24 when others then

25 rollback;

26 err_msg:=substr(sqlerrm,1,150);

27 raise_application_error(-20555, err_msg);

28 END;

END LOOP;

29 30 IF(commit_cnt > 0) then

31 commit;

32 END IF;

33 END;

34 /

PL/SQL procedure successfully completed.

SQL> select * from NL_WEST;

TEAM_NAME         LOCATION          OGG_KEY_ID

----------------  ----------------  ----------------

PADRES            SAN DIEGO         95AB2831E725991FE040C8C86E0162D0

ROCKIES           COLORADO          95AB2831E726991FE040C8C86E0162D0

DODGERS           LOS ANGELES       95AB2831E727991FE040C8C86E0162D0

DIAMONDBACKS      ARIZONA           95AB2831E728991FE040C8C86E0162D0

GIANTS            SAN FRANCISCO     95AB2831E724991FE040C8C86E0162D0

Create Table Supplemental Log Group in Source Database

GGSCI> dblogin userid OGG, password OGG

GGSCI> add trandata MLB.NL_WEST, COLS (OGG_KEY_ID), nokey

Create Unique Index on Target Table

SQL> create unique index GG_NL_WEST_UI on NL_WEST (OGG_KEY_ID) logging online;

Index created.

oracle为什么主键不唯一,Oracle GoldenGate 针对表没有主键或唯一索引的解决方案相关推荐

  1. oracle联合主键怎么找,Oracle数据库联合主键

    1.定义: 主键:在Oracle中,主键指能唯一标识一条记录的单个数据表列或联合的数据表列(联合主键|复合主键).主键用到的数据                   表列数据不能包含空值.而且,一张表 ...

  2. oracle主键约束删除,oracle删除主键查看主键约束及创建联合主键

    oracle删除主键查看主键约束及创建联合主键 1,主键的删除 ALTER TABLE TABLENAME DROP PRIMARY_KEY 执行上面的SQL可以删除主键:如果不成功可以用 ALTER ...

  3. oracle如何禁用主键约束,【oracle】约束之主键约束

    1.主键约束作用: 确保表中每一行数据是唯一的,要求非空且唯一 2.一张表中只能设置一个主键约束: 主键约束可以由多个字段构成(联合主键或复合主键). 1.在创建表时设置主键约束 CREATE TAB ...

  4. mybatis使用statement.getGenreatedKeys(); useGeneratedKeys=”true”;使用自增主键获取主键值策略和Oracle不支持自增,Oracle使用序列

    parameterType: 参数类型, 可以省略 mysql支持自增主键,自增主键的获取, mybatis也是利用statement.getGenreatedKeys();  useGenerate ...

  5. oracle中主键的建立,oracle 建立主键与索引

    一般的情况下,表的主键是必要的,没有主键的表可以说是不符合设计规范的. SELECT table_name FROM User_tables t WHERE NOT EXISTS (SELECT ta ...

  6. Oracle 创建数据表以及对数据表、字段、主外键、约束的操作

    选择主键的原则: 最少性 尽量选择使用单个键作为主键 稳定性 尽量选择数值更新少的列作为主键 1.创建数据表(CREATE TABLE) --创建数据表Student create table Stu ...

  7. oracle恢复主键丢失,案例:Oracle重建控制文件丢失undo异常恢复 ORA-01173模拟与恢复...

    天萃荷净 重建控制文件丢失undo异常恢复 ORA-01173模拟与恢复 数据库异常关闭,使用resetlogs方式重建控制文件,不包含undo表空间相关数据库,然后尝试resetlogs打开数据库, ...

  8. oracle 外键约束 权限,ORACLE外键约束(FORIGEN KEY)

    外键约束的定义是,让另一张表的记录来约束自己.这里的另一张表就是主表. 当主表的记录删除时,我们可以跟随主表删除记录(ON DELETE CASCADE).或者相应字段设置为空(ON DELETE S ...

  9. 在linux oracle 10g/11g x64bit环境中,goldengate随os启动而自己主动启动的脚本

    在linux oracle 10g/11g x64bit环境中,goldengate随os启动而自己主动启动的脚本 在linux.oracle 10g/11g x64bit环境中,goldengate ...

  10. Oracle Restart能够用来给Oracle GoldenGate 做 High Availability 使用么?

    Oracle Restart能够用来给Oracle GoldenGate  做 High Availability 使用么? 来源于: Can Oracle Restart be used with ...

最新文章

  1. Unix_Linux系统定时器的应用(案例)
  2. 左线性文法和右线性文法_线性代数期末考试复习资料
  3. Unrecognised tag: #39;encoding#39; (position: START_TAG seen ...lt;/versiongt;\r\n\t\t\t\tlt;en...
  4. html怎么去掉背景颜色,word背景颜色怎么去掉
  5. 有关JNLP中传SESSIONID为参数的问题
  6. c++组合 聚合 关联
  7. [js] ReferenceError和TypeError有什么区别?
  8. 《那些年啊,那些事——一个程序员的奋斗史》——79
  9. [原创]FineUI秘密花园(二十一) — 表格之动态创建列
  10. 互联网晚报 | 3月11日 星期五 |​ ​​商汤科技在深圳成立新公司,;微信支付电子小票上线...
  11. Red Gate系列之一 SQL Compare 10.4.8.87 Edition 数据库比较工具 完全破解+使用教程
  12. 马斯克:如果我不担任CEO 特斯拉就会完蛋
  13. 主机硬件系统主板状态 vmware_电脑主机启动不了怎么办?
  14. Delphi开发人员指南 第一部份快速开发的基础 第2章 Object Pascal 语言(二)
  15. 代理工具及使用技巧Proxy Hunter
  16. 佳博标签打印机如何打印条码流水号
  17. 阿里HSF(服务框架)
  18. K均值(K-Means)聚类算法简介
  19. AgentWeb简易使用
  20. C++语言里的pow函数(初学)

热门文章

  1. 等保2.0 安全计算环境 ——Windows服务器(三级系统)
  2. 物体重心的特点是什么_确定物体重心位置的常用方法是什么呢?
  3. 18 在springboot整合thymeleaf模板引擎中@Controller和@RestController不同注解的跳转页面方法
  4. 流媒体与实时计算,Netflix公司Druid应用实践
  5. 如何使用 NoxPlayer 加速 Android 应用程序开发?
  6. Linux命令之cat和tac篇
  7. [NOI Online 2021 入门组] 切蛋糕
  8. redis 客户端 -- lettuce 介绍
  9. 提示GuestAdditions versions on your host (6.1.32) and guest (6.1.22) do not match错误的解决方法
  10. 干货分享!java进阶视频百度云