OLAP的第一步就是从业务系统中抽取数据到数据仓库系统。
除了ETL工具Kettle,也可以使用PL/SQL
创建Source表,模拟业务系统的数据表。
create table source as select sys_guid() id ,o.* from dba_objects o where rownum<1000;
alter table source add constraint PK_source primary key (id);
创建Target表,模拟数据仓库中的表。
create table target as select * from source where 1=0;
alter table target add constraint PK_target primary key (id);
创建DB LINK,remote模拟业务系统的数据库
create database link remote connect to username identified by xxxxxx using 'remote';
因为业务系统的数据是变化的,相较于数据仓库的表,他可能更新了一些数据,也可能修改了一些数据。
比对业务表和数据仓库表的数据,
如果ID相同,并且数据有变化
则根据ID更新数据仓库的表(target)
如果业务系统的数据ID(source),还没有出现在数据仓库的表中(target)
则在数据仓库的表中新增这个记录。
本质都是Oracle Merge的功能,只不过尝试另外几种方法。
1.merge
-
mergeintotarget tusing(select*fromsource@remote)s
-
on(t.id=s.id)
-
when matched
-
then
-
updateset t.owner=s.owner,
-
t.object_name=s.object_name,
-
t.subobject_name=s.subobject_name,
-
t.object_id=s.object_id,
-
t.data_object_id=s.data_object_id,
-
t.object_type=s.object_type,
-
t.created=s.created,
-
t.last_ddl_time=s.last_ddl_time,
-
t.timestamp=s.timestamp,
-
t.status=s.status,
-
t.temporary=s.temporary,
-
t.generated=s.generated,
-
t.secondary=s.secondary,
-
t.namespace=s.namespace,
-
t.edition_name=s.edition_name
-
whennotmatched
-
then
-
insertvalues
-
(
-
s.id,
-
s.owner,
-
s.object_name,
-
s.subobject_name,
-
s.object_id,
-
s.data_object_id,
-
s.object_type,
-
s.created,
-
s.last_ddl_time,
-
s.timestamp,
-
s.status,
-
s.temporary,
-
s.generated,
-
s.secondary,
-
s.namespace,
-
s.edition_name
-
);
2.全局临时表。
首先将远程业务系统的数据放入临时表,
然后根据ID更新数据,如果数据的内容没有变化,则不更新。
最后插入业务系统中新建的数据。
-
createglobal temporarytabletmp
-
oncommit preserverows
-
as
-
select*fromtargetwhere1=0;
-
-
insertintotmpselect*fromsource@remote;
-
-
updatetarget tset
-
(
-
t.owner,
-
t.object_name,
-
t.subobject_name,
-
t.object_id,
-
t.data_object_id,
-
t.object_type,
-
t.created,
-
t.last_ddl_time,
-
t.timestamp,
-
t.status,
-
t.temporary,
-
t.generated,
-
t.secondary,
-
t.namespace,
-
t.edition_name
-
)
-
=
-
(select
-
tmp.owner,
-
tmp.object_name,
-
tmp.subobject_name,
-
tmp.object_id,
-
tmp.data_object_id,
-
tmp.object_type,
-
tmp.created,
-
tmp.last_ddl_time,
-
tmp.timestamp,
-
tmp.status,
-
tmp.temporary,
-
tmp.generated,
-
tmp.secondary,
-
tmp.namespace,
-
tmp.edition_name
-
fromtmpwheret.id=tmp.id)
-
whereexists(
-
select*fromtmpwheretmp.id=t.idandnot(
-
tmp.owner=t.ownerand
-
tmp.object_name=t.object_nameand
-
tmp.subobject_name=t.subobject_nameand
-
tmp.object_id=t.object_idand
-
tmp.data_object_id=t.data_object_idand
-
tmp.object_type=t.object_typeand
-
tmp.created=t.createdand
-
tmp.last_ddl_time=t.last_ddl_timeand
-
tmp.timestamp=t.timestampand
-
tmp.status=t.statusand
-
tmp.temporary=t.temporaryand
-
tmp.generated=t.generatedand
-
tmp.secondary=t.secondaryand
-
tmp.namespace=t.namespaceand
-
tmp.edition_name=t.edition_name
-
)
-
);
-
-
insertintotarget
-
select*fromtmpwherenotexists(
-
select*fromtarget twheret.id=tmp.id);
3.集合处理
为了简单,没有进行内容变化的判断
-
declare
-
typetabistableoftarget%rowtype;
-
l_rowtab;
-
cursor curisselect*fromsource@remote;
-
begin
-
open cur;
-
fetch cur bulk collectintol_row;
-
close cur;
-
forall iin1..l_row.count
-
updatetargetsetrow=l_row(i)whereid=l_row(i).id;
-
insertintotargetselect*fromsource@remote
s
-
wherenotexists(select*fromtarget
twheret.id=s.id);
-
commit;
-
end;
-
/
4.内联视图更新
没有写更新后插入的步骤,插入的实现是相同的。
-
update(
-
select
-
s.id s1,
-
s.owner s2,
-
s.object_name s3,
-
s.subobject_name s4,
-
s.object_id s5,
-
s.data_object_id s6,
-
s.object_type s7,
-
s.created s8,
-
s.last_ddl_time s9,
-
s.timestamp s10,
-
s.status s11,
-
s.temporary s12,
-
s.generated s13,
-
s.secondary s14,
-
s.namespace s15,
-
s.edition_name s16,
-
t.id t1,
-
t.owner t2,
-
t.object_name t3,
-
t.subobject_name t4,
-
t.object_id t5,
-
t.data_object_id t6,
-
t.object_type t7,
-
t.created t8,
-
t.last_ddl_time t9,
-
t.timestamp t10,
-
t.status t11,
-
t.temporary t12,
-
t.generated t13,
-
t.secondary t14,
-
t.namespace t15,
-
t.edition_name t16
-
fromtarget t inner join source@remote son(s.id=t.id)
-
where
-
not
-
(
-
s.owner=t.ownerand
-
s.object_name=t.object_nameand
-
s.subobject_name=t.subobject_nameand
-
s.object_id=t.object_idand
-
s.data_object_id=t.data_object_idand
-
s.object_type=t.object_typeand
-
s.created=t.createdand
-
s.last_ddl_time=t.last_ddl_timeand
-
s.timestamp=t.timestampand
-
s.status=t.statusand
-
s.temporary=t.temporaryand
-
s.generated=t.generatedand
-
s.secondary=t.secondaryand
-
s.namespace=t.namespaceand
-
s.edition_name=t.edition_name
-
)
-
)
-
set
-
t1=s1,
-
t2=s2,
-
t3=s3,
-
t4=s4,
-
t5=s5,
-
t6=s6,
-
t7=s7,
-
t8=s8,
-
t9=s9,
-
t10=s10,
-
t11=s11,
-
t12=s12,
-
t13=s13,
-
t14=s14,
-
t15=s15,
-
t16=s16
-
;
5.Minus
先插入业务表中新增的记录,然后对比修改。
-
declare
-
typetabistableoftarget%rowtype;
-
l_rowtab;
-
cursor curisselect*fromsource@remoteminusselect*fromtarget;
-
begin
-
insertintotargetselect*fromsource@remote
s
-
wherenotexists(select*fromtarget
twheret.id=s.id);
-
open cur;
-
fetch cur bulk cllectintol_row;
-
close cur;
-
forall iin1..l_row.count
-
updatetargetsetrow=low(i)whereid=l_row(i).id;
-
commit;
-
end;
-
/
相关推荐
在两个SQLSERVER之间数据同步,本文介绍了两个数据库之间的数据交换。
内部java实现多个数据库,保持数据同步案例。
两个数据库之间数据的同步软件设计,阮宁君,沈林,本文介绍一种两个数据库之间表中数据自动同步或者手动同步的软件设计与实现。该软件具有可以自动定时把一个数据库中的数据同步到
两个MySQL数据库之间同步表结构及索引模式
该rar包里面代码是我自己写的一个同步。它的目的是:同步两个数据库staff表的数据,把A表多的数据插入到B表,同时也删除B中多余的数据(A表没有的 )
比较两个数据库之间的差异,并进行同步。功能非常强大。
做开发的时候要做MySQL的数据库同步,两台安装一样的系统,都是FreeBSD5.4,安装了Apache 2.0.55和PHP 4.4.0,MySQL的版本是4.1.15,都是目前最新的版本。本文将为大家介绍如何在两台MySQL数据库间实现同步。
mysql,两个数据库,实现实时数据同步
在VS2008下,利用c#在SQLSERVER2005实现两个数据库同步,要求:两个数据库中表名可以不一样,但是列名要一样。(利用存储过程实现)
Mysql两个数据库表之间双向数据同步方案.docx
为了满足客户的要求,客户要求两台数据库服务器在不能直接通信的情况下,要保证两台数据库数据保持同步,并且要保证效率,数据量一般每分钟最少可以到达2万条(只能通过端口访问),所以只能使用程序先生成满足特定...
两个mysql数据库之间实现同步,通过定时任务实现从主库到从库的同步,java代码实现,所有java项目包括配置都有,数据结构可以跟据自己的特定情况去改
同步当前数据库和服务器数据库的数据,使得两个数据库的数据一致,这个属于sql的高级应用,相信对你们都很有用处,也可以对你们的数据库能用有所提示,是一篇比较好的资料,由于该资料是本人开发的,所以希望尊重...
springboot双数据库定时同步,可以是mysql,SqlServer
同步不同数据库之间的数据,比如postgresql到oracle,oracle到oracle,不限于此,支持自己配置。 实现思路: 在主线程中创建若干子线程,每个子线程分别用来同步一张数据表,提供web页面,监控各个子线程运行状况,...
3.同步开始时间(就是根据最后更新时间栏位 例如:update_date 大于等于这个时间的数据进行传输) 4.调度配置(配置自动多久执行一次) 5.最后保存设置. 6.可手动执行传输数据,也可自动根据调度执行. 7.同步表结构,此...
通过Oracle的流复制实现数据库之间的同步
linux中的shell脚本用于同步两台互通数据库服务器上的表数据
在同一机上,两个数据库资料同步的储存过程
DBLINK实现两个数据库间表数据同步---张金总结版