【发布时间】:2021-07-29 05:39:38
【问题描述】:
我们在 Oracle 19c 中对表进行批量收集插入。逻辑如下
loop
fetch cursor c bulk collect
into v_row limit 1000 --v_row is table of source_table%rowtype
exit when (v_row.count) = 0;
for i in v_row.first .. v_row.last
loop
--do processing here, assign v_row(i) values to v_target_table%rowtype variable
--at the end we are extending a nested table and populating it with this v_target_table row
v_tar_tab.extend;
v_tar_tab(v_tar_tab.count) := v_target_table;
end loop;
end loop;
--insert with forall
forall i in v_tar_tab.first .. v_tar_tab.last
insert into target_table
values v_tar_tab (i);
v_tar_tab := t_tar_table(); -- is table of target_table%rowtype
commit;
问题是在这种特殊情况下,源表有 300 000 行,前 100 000 行的插入工作非常快,但此后每 1000 次提取的时间都在增加,其余 200 000 行的总时间是与前 100 000 行所花费的时间相比太大了。 为了看到这一点,我们添加了一个计数器变量,并在每次获取时将此计数器变量增加 1000,并将此计数器的迭代次数和值记录在我们的日志表中。在第 95 次到第 100 次迭代后,获取并处理了 100 000 行,处理速度变慢。
循环内没有提交,目标表设置为无日志记录,并且在执行此插入过程之前禁用其约束和索引。我想不出任何理由为什么它在前 n 行中运行得很快,而在其余行中运行得很慢。关于应该改变什么的任何想法?
如果需要注意,游标 c 中的 select 语句与提示并行运行。我在 insert into 语句中添加了 APPEND_VALUES 提示,但它并没有改变整体行为或时间。
【问题讨论】:
标签: oracle insert bulkinsert oracle19c