我在两个表中有数据:
**Supplier:** ERPSupplier, RMSSupplier
**ItemLoc:** Item, Location, Supplier
ItemLoc 中的供应商是供应商表中的 ERP 供应商。在与ERP供应商进行比较后,我需要替换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;
@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)
根据您使用的 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 有一些考虑。