首页 > 解决方案 > 简单更新 (SQL) 与批量更新 (PL/SQL)

问题描述

最近我遇到一个面试问题:在 oracle DB 中更新超过 100 万条记录的表,哪个更好更快,为什么?简单更新:

update employee
set salary = salary + (salary*5%)
where dept_id = l_dept_id

或者

像这样使用批量收集和 forall 语句:`

declare 
type t_ntt is table of employee.employee_id%TYPE; 
l_ntt t_ntt; 
l_dept_id INTEGER := 10; 
c_limit INTEGER := 10000; 
cursor c is select employee_id from employee where dept_id = l_dept_id; 
begin 
open c; 
loop 
fetch c into l_ntt limit c_limit; 
exit when l_ntt.count = 0; 

forall I in indices of l_ntt 
update employee 
set salary = salary + (salary*5%) 
where employee_id = l_ntt(I); 
end loop; 
close c; 
commit; 
end;

`

标签: sqloracleplsqlbulkupdate

解决方案


推荐阅读