---
title: "oracle streams "
author: "chenyubo"
date: "2008-10-14T17:03:05Z"
tags: ["C", "C#", "C++", "Oracle", "SQL"]
slug: "oracle-streams"
draft: false
lastmod: "2008-10-14T17:03:05Z"
id: 252765
url: "https://chenyubo.iteye.com/blog/252765"
---

本文讲解streams的基础部件，同时深入探讨streams对redo的特殊要求。

streams是基于log miner的一种技术，可以用于数据同步，数据复制等功能。本系列主要涉及用streams升级数据库（9i -->10g）的知识。streams主要有3个功能部件: capture, propagation以及apply，同时还可以定义rule。rule可以绑定到capture,propagation以及apply, 用于筛选capture,propagation,apply的输入，从而可以根据需要只对满足rule的数据进行操作。

capture启动时会同时启动一个log miner的会话，log miner不停地从archive log/online redo log中抽取redo entry，capture 根据rule将这些redo entry变为LCR(Logic Change Record). Progagation 根据rule将满足条件的LCR传到目标数据库，然后apply 将满足rule的LCR 应用到目标数据库中。

redo的目的是数据库恢复，它包含了数据库的哪一物理数据块发生什么样的变化的信息。rowid可以准确地定位数据变化的具体位置，因此如果redo中包含rowid，就可以用于数据恢复了。但是rowid只对本数据库有效，如果使用streams，仅仅在redo中记载rowid不够的，因为原数据库和目标数据库的物理结构不一定是一样的。streams需要一种能唯一定位一个row变化的机制，那么什么可以用于唯一定位一个row呢？rowid, primary key都可以（unique index不行，因为对于unique index,可以允许多个row的相关列为null，因此在index的列为null时不能唯一定位一条记录）。其中只有primary key可以跨数据库，跨版本，跨平台。所以在使用streams是需要在redo中包含primary key的信息。

运行以下命令后，数据库就会在redo中添加相应的primary key。
alterdatabaseadd supplemental logdata(primary key) columns;
如果表中没有主键，redo中就包含所有的列（除了long等特殊字段外）。当一个变化（change）传递到目标数据库时，streams会查找相应的需要apply的行。比如表citizen的主键是ID, 当ID=43210的纪录在源数据库发生update变化后，streams会在目标数据库中利用ID=43210这个条件找到相应的行，然后就apply这个变化。假如数据表没有主键，并且有重复的行，那么streams在查找相应的行时，会发现有多行满足条件，这时streams就会报错。所以强烈建议所有的表中都要有主键，不然streams在运行时可能出现问题。

做个实验就可以发现执行
alterdatabaseadd supplemental logdata(primary key) columns;
后，redo的变化。

createtablet1 (a int primary key, b int, c int, d int, e int);
create table t2 (a int, b int, c int, d int, e int);

insert into t1 values (1,1,1,1,1);
insert into t2 values (1,1,1,1,1);

insert into t1 values (2,2,1,1,1);
insert into t2 values (2,2,1,1,1);
commit;

Test 1:
执行语句之前:
select supplemental_log_data_pk from v$database;
NO    

update t1 set c=10;
update t2 set c=10;
commit;

（以下使用log miner查找redo的信息，本文暂不提供log miner 的具体内容）
select sql_redo from v$Logmnr_contents where seg_name like 'T%' and sql_redo like 'update%
update "TEST"."T1" set "C" = '10' where "C" = '1' and ROWID = 'AAAHZjAABAAAMYyAAA';
update "TEST"."T1" set "C" = '10' where "C" = '1' and ROWID = 'AAAHZjAABAAAMYyAAB';
update "TEST"."T2" set "C" = '10' where "C" = '1' and ROWID = 'AAAHZlAABAAAMZCAAA';
update "TEST"."T2" set "C" = '10' where "C" = '1' and ROWID = 'AAAHZlAABAAAMZCAAB';

可以看到，redo中只包含rowid以及相关列变化后的值

Test 2:
执行语句之后:
alter database add supplemental log data (primary key) columns;

select supplemental_log_data_pk from v$database;
YES    

update t1 set c=-10;
update t2 set c=-10;
commit;

select sql_redo from v$Logmnr_contents where seg_name like 'T%' and sql_redo like 'update%
update "TEST"."T1" set "C" = '-10' where "A" = '1' and "C" = '10' and ROWID = 'AAAHZjAABAAAMYyAAA';
update "TEST"."T1" set "C" = '-10' where "A" = '2' and "C" = '10' and ROWID = 'AAAHZjAABAAAMYyAAB';
update "TEST"."T2" set "C" = '-10' where "A" = '1' and "B" = '1' and "C" = '10' and "D" = '1' and "E" = '1' and ROWID = 'AAAHZlAABAAAMZCAAA';
update "TEST"."T2" set "C" = '-10' where "A" = '2' and "B" = '2' and "C" = '10' and "D" = '1' and "E" = '1' and ROWID = 'AAAHZlAABAAAMZCAAB';


<span style="color:red">可以看到，redo中多了一些信息，对于主键是A的表T1, redo包含了主键的信息。 对于没有主键的表T2, redo中包含了所有列的信息</span>