Oracle SQL 优化更新
Oracle SQL optimal update
我有两个 table 的数据:
**Supplier:** ERPSupplier, RMSSupplier
**ItemLoc:** Item, Location, Supplier
ItemLoc 中的 Supplier 是来自 Supplier table 的 ERPSupplier。与ERPSupplier比较后,我需要替换RMSSupplier。
进行更新的最佳方式是什么? ItemLoc 中有 1000 万条记录 Table.
目前我正在使用 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;
根据您使用的 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在这方面有几点考虑。
@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)
我有两个 table 的数据:
**Supplier:** ERPSupplier, RMSSupplier
**ItemLoc:** Item, Location, Supplier
ItemLoc 中的 Supplier 是来自 Supplier table 的 ERPSupplier。与ERPSupplier比较后,我需要替换RMSSupplier。
进行更新的最佳方式是什么? ItemLoc 中有 1000 万条记录 Table.
目前我正在使用 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;
根据您使用的 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在这方面有几点考虑。
@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)