有个表A有字段ID, NAME
有个表B有字段ID,NEWNAME  都是百万级的数据
现在我要将A里面的ID在B里面存在的数据取出来
然后将取出来的数据的 NEWNAME设置成A的NAME 如何高效呢?我是这样写的
cursor c_id is select * from B where id in (select id from A)
然后用fetch 游标循环 
在循环里面 update a set name = v_name where id = v_id 
(v_name , v_id 是游标的值)这样是对的 但是效率不高 请问有高效率的吗

解决方案 »

  1.   

    update a 
       set name = (select newname from b where b.id = a.id)
    where exists (select null from b where b.id = a.id)
      
      

  2.   

    用exists 效率比in要高..
    最高效的方法是使用oracle bucket,可以批量更新...
      

  3.   

    如果b.id是主键,不重复,就用:
    update a 
       set name = (select newname from b where b.id = a.id)
    where exists (select 1 from b where b.id = a.id)
    重复的时候,得稍加改进
      

  4.   

    百万级的表 我一般这样处理 呵呵 create table temp as 
    select a.id,nvl(b.NEWNAME,a.NAME) from a,b
    where a.id = b.id(+);
    truncate table a;
    insert into a
    select * from temp;
    drop table temp;
      

  5.   

    我一般是用shell脚本处理。
    先导出两张表的数据,然后通过shell脚本进行处理后,在导入就ok啦。
      

  6.   

    遇到这样的情况我一般三步解决:一、在A表和B表中id列建立索引二、确定b表中是否有重复数据,如果有重复数据需要上除b表中的重复数据三、执行update语句:
    update a  
      set name = (select newname from b where b.id = a.id)
    where exists (select 1 from b where b.id = a.id)
      

  7.   

    现在的问题是我不是单存的UPDATE 
    还要判断一些值才UPDATE。比如 A里面有两个字段 VALUE1 VALUE2
    我取得B的两个字段 TIAOJIAN VALUE
    我要判断 TIAOJIAN == 1 就updae A 的VALUE1字段
    TIAOJIAN == 2 就updae A 的VALUE2字段
    这样还可以批量更新吗
      

  8.   


    用2个update搞掂,最好不要用游标
      

  9.   

    同意,能有sql语句搞定的最好.
      

  10.   

    数据量大建议使用merge into或者
    update /*+bypass_ujvc*/  
      (select a.name,b.newname from a,b where b.id = a.id)
    set name=newname;
      

  11.   

    用游标吧,速度还是游标最快,参考我在这个帖子的回复:http://topic.csdn.net/u/20100601/18/e612292a-f23f-4c29-a581-6d62947de062.html如果你看不懂,我可以写好了给你,不过你的两表真的是A和B吗?
      

  12.   

    从B表查出来  复制  直接往A表上贴 就有了(用PL/SQL) 
      

  13.   

    哈哈哈哈,一看就晓得你是个外行,IN是用不到索引的update b 
    set b.newname=a.name
    where exist(select 1 from a where a.id=b.id)
      

  14.   

    呵呵,丢人了,写掉了点
    update b  
    set b.newname=(select a.name from a where a.id=b.id)
    where exist(select 1 from a where a.id=b.id)
      

  15.   

    IN,SUBSTR很多很多等等都是用不到索引的
    另外,用游标也是很慢的
      

  16.   

    批量操作你用forall in,别用fetch,这样快些,可以试试
      

  17.   


    -- 支持这种写法
    update a
     set name = (select NEWNAME from b where a.id = b.id)
     where exist (select 1 from b where a.id = b.id);