两个表 tb1,tb2
结构如下
tb1:
  id  number(6) not null, 
  pcs number(9) null,
  xzqh number(6) null,
  。。tb2: 
  id  number(6) not null,
  xzqh number(6) null,
  tx   Blob   null,
  。。两个表的id,xzqh相互对应,即tb1中每条记录在tb2中有一条记录存在或没有对应记录,但它们没有父子关系.表记录数6000万,现从tb2中删除20万数据,满足的条件如下:
  1, xzqh= 100000
  2, tb1中的pcs最后三位 在100-199之间.我写了以下语句:
    for i in 1..40 
    loop
       delete  from tb2 where xzqh=100000 and 
    id in (select id from tb1 where xzqh=100000 and 
    substr(to_char(pcs),7,3)>=100 and 
    substr(to_char(pcs),7,3)<=199) and rownum<=5000;
       commit;
    end loop;假设已在substr(to_char(pcs),7,3)上建基于函数的索引(Function-Based Index)
现在的问题是 “in”操作符查询效率太慢!!!
请问各位,能否不用“in”. 或者另有更好的实现方式??

解决方案 »

  1.   

    for i in 1..40 
        loop
           delete  from tb2 where xzqh=100000 and 
        id = (select id from tb1 where xzqh=100000 and 
        substr(to_char(pcs),7,3)>=100 and 
        substr(to_char(pcs),7,3)<=199) and rownum<=5000;
           commit;
        end loop;
      

  2.   

    delete from tb2 
    where szqh=100000
    and exists (select 'x'
        from tb1
        where tb1.id=tb2.id
        and tb1.szqh=tb2.szqh
        and substr(to_char(tb1.pcs),7,3)>=100 
        and substr(to_char(tb1.pcs),7,3)<=199) 
    and rownum<=5000;
      

  3.   

    1﹑step1:
    create or replace view 
         view1 as
       select id,
         from tb1
        where szqh=100000 and
              substr(to_char(pcs),7,3) between '100' and '199' and
              rownum<=5000;
    2﹑step2:
    delete from tb2
        where szqh=100000 and
              id in (select id from view1);
    3﹑step3:
    如果step2還是很慢的話請在sqlplus中采取這一種方式試一下﹕
    var i number;
    declare cursor a is
     select id from view1;
    begin
       :i:=0;
       for c in a loop
          delete from tb2 
           where id=c.id and szqh=100000 ;
          :i:=:i+1;
          if :i>=100 then
            commit;
            :i:=0;
          end if;
       end loop;
       commit;
    end ;
    /