请帮写个存储过程,需求是这样:
有三张表 t_original t_temp t_diff
表结构相同,有三个字段 cl1, cl2, cl3 ,其中在cl1上建立了主键索引。现在想对比表t_original和表t_temp所有数据中字段cl1相同,但字段cl2却不同,并将t_temp里的这条数据存放到表t_diff中。请写个存储过程,请考虑效率,因为表t_original数据是千万级的,表t_temp的数据是百万级的。谢谢。

解决方案 »

  1.   

    insert into t_diff
    select tte.*
    from t_original tor, t_temp tte
    where tor.cl1 = tte.cl1
    and tor.cl2 <> tte.cl2;
      

  2.   

     
    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 )
      

  3.   

    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 );
      

  4.   

    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
      

  5.   

    稍微改一下,参考而已: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
    );
    有分区表,在这个基础上动点手脚也好。。
      

  6.   

    把t_diff的索引先drop掉,
    然后再用楼上某位的sql去insert,
    再建索引