我现在要做数据移行,从文本中读取20W条数据到零时表A中,需要将这20W条数据更新到B表,B中数据量超过100W条,原有思路是对A表进行循环将数据更新到B表中,但此方法效率太低。能不能按A表中的某个字段对数据进行并发更新?例如:A表中有地区号字段,根据地区号不同对B表进行同时update处理!往高手指点啊!说得越详细越好,最好能有详细的例子!

解决方案 »

  1.   

    20W,用merge into 语句,一下子就完了。
    merge into  a
    using (select id,name from b ) c
    on(a.id=c.id )
    when matched then update set a.name=c.name
    when not matched then insert (a.id,a.name) values (c.id,c.name);
    作用:利用表 b 跟新表a ,条件是a.id=b.id,如果a表中没有该条件的数据就插入。如果你的数据量很大,此sql效率非常高。 
      

  2.   

    如果on的地方不变,update的字段多,效率上应该相差不大。
      

  3.   

    再问个问题。在存储过程中怎么建零时表呢?数据从.txt文件中导入到临时表中!
      

  4.   

    .txt导入到表有个命令可以的,哪个命令我不晓得。导入到临时表就不知道怎么弄了。
    存储过程使用临时表的例子:create or replace procedure upd_wbb_quotenum 
    as
    begin
    --创建会话级临时表
    execute immediate 'CREATE GLOBAL TEMPORARY TABLE tmp_wbb_quotenum (project_id NUMBER,quotenum NUMBER) ON COMMIT PRESERVE ROWS'; --找出第一个SQL需要的count及projectid,插入临时表数据
    insert into tmp_wbb_quotenum
    select project_id,count(distinct seller_id)
    from bid_quote_tables 
    where status >= 5
    group by project_id; --关联临时表进行修改
    update web_business_bulletin a
    set a.quotenum = tmp_wbb_quotenum.quotenum
    where a.itemState = 1
    and (a.bulletinType = 1 or  a.bulletinType = 2)
    and a.projectid = tmp_wbb_quotenum.project_id;

    --提交
    commit; --清空临时表
    execute immediate 'truncate table tmp_wbb_quotenum'; --找出第二个SQL需要的count及projectid,插入临时表数据
    insert into tmp_wbb_quotenum
    select projectid,count(distinct selleruserid)
    from expr_bid_quotetable
    group by projectid; --关联临时表进行修改
    update web_business_bulletin a
    set a.quotenum = tmp_wbb_quotenum.quotenum 
    where a.itemState = 1
    and (a.bulletinType = 30 or  a.bulletinType = 31)
    and a.projectid = tmp_wbb_quotenum.project_id;

    --删除临时表
    execute immediate 'drop table tmp_wbb_quotenum';

    commit;
    exception when others then
    raise;
    end upd_wbb_quotenum;
    /
      

  5.   

    select count(*) into tempisexist from all_tables where table_name='TMP';
    if tempisexist=0 then--不存在临时表就创建一个
    execute   immediate 'grant create table to upd_wbb_quotenum';--还得给存储过程中加上增加表的权限。
    刚在别处看了个例子,最好在建表前加个判断