日期:2014-05-17  浏览次数:21082 次

表数据对比
请帮写个存储过程,需求是这样:
有三张表 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
);