我在 PL/SQL 中使用批量绑定时遇到问题。基本上我想要的是一个表(组件)根据 Component_id 和字段名更新其字段值。所有这些都作为参数传入(varchar2_nested_table 类型实际上是字符串数组,每个需要发生的更新语句的一个元素)。因此,例如,如果 Component_id = 'Compid1' 和 fieldname = 'name' 那么 fieldvalue 应该更新为“新组件名称”。
我输入了以下与此http://www.oracle.com/technetwork/issue-archive/o14tech-plsql-l2-091157.html相关的代码。该代码有效,但并不比为 IN 参数中的每个元素执行更新的简单循环快。因此,如果参数有 1000 个元素,则将执行 1000 个更新语句。我也意识到我没有使用 BULK COLLECT INTO 但我认为我不需要它,因为我不需要从数据库中选择任何东西,只需更新。
目前,1000 次更新都需要 4-5 秒。我假设我错误地使用了批量绑定或对主题有误解,如示例中我可以发现人们在 2 秒内执行 50,000 行等。据我了解,FORALL 应该通过减少上下文切换的数量来提高性能。我尝试了另一种使用游标和批量绑定在网上找到的方法,但结果相同。可能是我对性能的期望太高了?看到别人的结果,我不这么认为。任何帮助将不胜感激。
create or replace procedure BulkUpdate(sendSubject_in IN varchar2_nested_table_type,
fieldname_in IN varchar2_nested_table_type,fieldvalue_in IN varchar2_nested_table_type) is
TYPE component_aat IS TABLE OF component.component_id%TYPE
INDEX BY PLS_INTEGER;
TYPE fieldname_aat IS TABLE OF component.fieldname%TYPE
INDEX BY PLS_INTEGER;
TYPE fieldvalue_aat IS TABLE OF component.fieldvalue%TYPE
INDEX BY PLS_INTEGER;
fieldnames fieldname_aat;
fieldvalues fieldvalue_aat;
approved_components component_aat;
PROCEDURE partition_eligibility
IS
BEGIN
FOR indx IN sendSubject_in.FIRST .. sendSubject_in.LAST
LOOP
approved_components(indx) := sendSubject_in(indx);
fieldnames(indx):= fieldname_in(indx);
fieldvalues(indx) := fieldvalue_in(indx);
END LOOP;
END;
PROCEDURE update_components
IS
BEGIN
FORALL indx IN approved_components.FIRST .. approved_components.LAST
UPDATE Component
SET Fieldvalue = fieldvalues(indx)
WHERE Component_id = approved_components(indx)
AND Fieldname = fieldnames(indx);
END;
BEGIN
partition_eligibility;
update_components;
END BulkUpdate;