现有三个表,temp,temp01,temp02
其中temp表是总表,其中包含no字段,并且包含temp01表中no字段的内容
现在我想作的是从temp表中找出不在temp01中出出现的字段,并且把不出现的纪录
插入到temp02中,其中temp01和temp02都只是包含一个no字段
如:
temp     temp01   最后插入到temp02中的是 
  no       no              no
  1        1               4
  2        2               5
  3        3               6 
  4                        7
  5
  6
  7
这是我写的sql语句,因为数据量比较大,temp中有四百多万条纪录,而temp01中大约有五十多万条,
下面的语句效率太低,运行好几个小时都没反映,不知道是不是哪有问题,还是我写的这个效率比较低,我觉得
一般没什么问题啊,可是这个就不行,是不是数据库的配置有问题
有哪位大侠请指点指点
  insert into temp02
  select no 
  from temp
  where no not in(
  select no 
  from temp01);

解决方案 »

  1.   

    试一下:insert into temp02
    (select temp.no
    from temp, temp01
    where temp.no = temp01.no(+)
          and temp01.no is null)
      

  2.   

    或者insert into temp02
    (select temp.no
    from temp left outer join temp01
         on temp.no = temp01.no
    where temp01.no is null)
      

  3.   

    try:insert into temp02
    (select no from temp
    minus
    select no from temp01)
      

  4.   

    minus---数据集相减,速度还可以,你试试
      

  5.   


    select * from 
    (select '1' from dual
    union
    select '2' from dual
    union
    select '3' from dual
    union
    select '4' from dual
    union
    select '5' from dual
    union
    select '6' from dual
    union
    select '7' from dual
    union
    select '8' from dual)minusselect * from 
    (select '1' from dual
    union
    select '2' from dual
    union
    select '3' from dual)---------------------
    1 4
    2 5
    3 6
    4 7
    5 8
      

  6.   

    赞成1楼2楼所写,我用以下:
    declare
      v_count number;
    begin
      v_count:=0;
      for rec in (select temp.no
       from temp, temp01
       where temp.no = temp01.no(+)
            and temp01.no is null) loop
       insert into temp02(no) values(rec.no);
       v_count++;
       if v_count=10000 then 
         commit;
         v_count:=0;
       end if;
     end loop;  
    end;
    以上10000条提交一次,根据楼主所说,大概数据在350万,这么多的数据一次提交,如果回滚段不够大的话,也会很慢或者不成功,如果1万条(自己调整)提交一次,虽然用游标慢多花时间,但是肯定可以提交
      

  7.   

    not in 效率是最低的, temp01.no is null 即使有索引也用不上!!
    带有UNION,MINUS,INTERSECT,ORDER BY的SQL语句会启动SQL引擎执行耗费资源的排序功能.
    用not exists可能比较好!!insert   into   temp02 
        select   no   
        from   temp t
        where   not   exists( 
        select   'x'   
        from   temp01 t1 where t1.no=t.no);