首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

表数据对比解决办法

2012-02-19 
表数据对比请帮写个存储过程,需求是这样:有三张表 t_original t_temp t_diff表结构相同,有三个字段 cl1, c

表数据对比
请帮写个存储过程,需求是这样:
有三张表 t_original t_temp t_diff
表结构相同,有三个字段 cl1, cl2, cl3 ,其中在cl1上建立了主键索引。

现在想对比表t_original和表t_temp所有数据中字段cl1相同,但字段cl2却不同,并将t_temp里的这条数据存放到表t_diff中。

请写个存储过程,请考虑效率,因为表t_original数据是千万级的,表t_temp的数据是百万级的。谢谢。

[解决办法]
insert into t_diff
select tte.*
from t_original tor, t_temp tte
where tor.cl1 = tte.cl1
and tor.cl2 <> tte.cl2;
[解决办法]

SQL code
 select * from t_temp  where not exists(select cl1, cl2 from t_original to, t_temp tt where to.cl1=tt.cl1 and to.cl2=tt.cl2 )
[解决办法]
insert into t_diff
select * from t_temp tt
 where not exists(
select tt.cl1, tt.cl2 
from t_original tor
where tor.cl1=tt.cl1 
and tor.cl2=tt.cl2 );
[解决办法]
insert into t_diff
select a.cl1,b.cl2,b.cl3
from t_original a,t_temp b
where a.col1=b.col1 and a.col2<>b.col2
[解决办法]
稍微改一下,参考而已:
SQL code
INSERT /*+append*/INTO t_diff NOLOGGING(cl1,cl2,cl3)SELECT cl1,cl2,cl3 FROM t_temp t1 WHERE not exists(   SELECT /*+hash_aj*/1    FROM t_original t2     WHERE t1.cl1 = t2.cl1     AND t1.cl2 = t2.cl2); 

热点排行