2015-10-25 63 views
0

我有两个表中的数据:的Oracle SQL优化更新

**Supplier:** ERPSupplier, RMSSupplier 

**ItemLoc:** Item, Location, Supplier 

在ItemLoc供应商是从供应商表ERPSupplier。与ERPS供应商比较后,我需要替换RMSSupplier。

做更新的最佳方式是什么? ItemLoc表中有1000万条记录。

目前我用PLSQL块做的,但其花费过多时间:

DECLARE 
    cursor c1 is 
    select * from Supplier; 

BEGIN 

    FOR r in c1 LOOP 

    update mig_item_loc 
     set Supplier = r.RMSSupplier 
     where Supplier = r.ERPSupplier; 

    END LOOP; 

END; 

回答

2

取决于你使用的是Oracle数据库的版本,你可能会得到一些好处了使用BULK的COLLECT (https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:1203923200346667188)。

我也认为你应该可以在没有PL/SQL的情况下完成这个任务。 https://dba.stackexchange.com/questions/3033/how-to-update-a-table-from-a-another-table在这方面有一些考虑因素。

+2

肯定使用一个SQL语句此,并只更新新值与旧值不同的位置。 –

3

@ziesemer是正确的。如果你想让它更快,那么你想考虑使用批量收集。这个概念似乎很难在第一把握,但这里的散装的示例应用程序代码中的收集:

DECLARE 
    cursor c1 is 
     select * from Supplier; 
    type RMSSupplier_type is table of Supplier.RMSSupplier%type index by pls_integer; 
    type ERPSupplier_type is table of Supplier.ERPSupplier%type index by pls_integer; 
    tableOfRMSSupplier RMSSupplier_type 
    tableOfERPSupplier ERPSupplier_type; 
    BEGIN 
    select RMSSupplier, ERPSupplier BULK COLLECT INTO tableOfRMSSupplier, tableOfERPSupplier FROM Supplier; 
    FORALL a in 1..tableOfRMSSupplier.COUNT 
     update mig_item_loc 
      set Supplier = tableOfRMSSupplier(a) 
      where Supplier = tableOfERPSupplier(a);    
    END; 

你也可以试试这个单行更新:

update mig_item_loc a 
set a.Supplier = (select b.RMSSupplier from Supplier b where a.Supplier=b.ERPSupplier) 
+0

单行查询在添加索引后完成了这项工作。 –