简体   繁体   English

从SQL Oracle中的其他(巨大)表更新巨大表

[英]Update huge table from other (huge) table in SQL Oracle

I have two big tables: 我有两个大桌子:

Table1 (from_id, to_id, field1, field2, field3) ~ 500K rows Table1(from_id,to_id,field1,field2,field3)〜50万行

Table2 (id_num, field1, field2, field3) ~4M rows Table2(id_num,field1,field2,field3)〜400万行

I need to update Table2 from Table1 data based on Table2.id_num that should be between Table1.from_id and Table1.to_id. 我需要基于Table2.id_num从Table1数据更新Table2,该数据应该在Table1.from_id和Table1.to_id之间。

In both tables - ID value can vary between 16,000,000 and 4,300,000,000. 在两个表中-ID值可以在16,000,000和4,300,000,000之间变化。

I ran the following cursor script, but it runs for hours and not finished yet. 我运行了以下光标脚本,但是它运行了几个小时,尚未完成。

DECLARE
 l_FROM_ID                Table1.FROM_ID%TYPE;
 l_TO_ID                  Table1.TO_ID%TYPE;
 l_Field1                 Table1.Field1%TYPE;
 l_Field2                 Table1.Field1%TYPE;
 l_Field3                 Table1.Field1%TYPE;

CURSOR cur
     IS
      SELECT
      FROM_ID, TO_ID, Field1, Field2, Field3
      FROM
      Table1  ;
BEGIN
 OPEN cur;
 LOOP
       FETCH cur INTO
        l_FROM_ID,
        l_TO_ID,
        l_Field1,
        l_Field2,
        l_Field3 ;

      UPDATE 
        table2 t
      SET
        t.field1 = l_field1, 
        t.field2 = l_field2,
        t.field3 = l_field3      
      WHERE  t.id_num >= l_FROM_ID and t.id_num <= l_TO_ID;
       commit;
    END LOOP;
    CLOSE cur;
end;

Any ideas on how to do it more efficient? 关于如何使其更有效的任何想法?

There are many ways to accelerate this. 有很多方法可以加快这一步。 First of all, I'd try just 首先,我会尝试

update Table2 t2 set
  (field1, field2, field3) = (select field1, field2, field3 
                              from Table1 t1 
                              where t2.id_num between t1.from_id and t1.to_id)

if your database server configured as OLAP(like etl /elt process). 如果您的数据库服务器配置为OLAP(如etl / elt进程)。 Instead of Updating you can create new table with the values you want and drop the existing table. 除了更新外,您还可以使用所需的值创建新表并删除现有表。

this way would be better for large tables. 这样对于大型表会更好。

Create table Newtable nologging pctfree 0 as select t1.field1, t1.field2, t.field3,..t2.columns you want to retain from table1 t1, tab2 t2 where t1.form_id=t2.to_id. 创建表Newtable nologging pctfree 0,从表1 t1,tab2 t2中保留要选择的t1.field1,t1.field2,t.field3,.. t2.columns,其中t1.form_id = t2.to_id。

I hope this helps 我希望这有帮助

Thanks Thangamani Eraniyan 感谢Thangamani Eraniyan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM