当Oracle运行PL/SQL时会使用两套引擎,所有procedural code由PL/SQL engine 完成,所有SQL由SQL engine处理。所以如果Oracle从一个collection中循环执行相同的DML操作,那么为了避免两套engine切换所消耗的系统资源,可以使用bulk binds来把所有的DML操作binding到一次操作中完成。这将极大提高PL/SQL的执行效率。

  以下是简单的测试,用两种方式插入100000条数据,可以看到效率提高了7倍左右。

Bulk Binds对性能的提高SQL> CREATE TABLE test1( Bulk Binds对性能的提高2 id NUMBER(10), Bulk Binds对性能的提高3 description VARCHAR2(50)); Bulk Binds对性能的提高Bulk Binds对性能的提高Table created Bulk Binds对性能的提高Bulk Binds对性能的提高SQL> ALTER TABLE test1 ADD ( Bulk Binds对性能的提高2 CONSTRAINT test1_pk PRIMARY KEY (id)); Bulk Binds对性能的提高Bulk Binds对性能的提高Table altered Bulk Binds对性能的提高Bulk Binds对性能的提高SQL> SET TIMING ON; Bulk Binds对性能的提高Bulk Binds对性能的提高SQL> DECLARE Bulk Binds对性能的提高2 TYPE id_type IS TABLE OF test1.id%TYPE; Bulk Binds对性能的提高3 TYPE description_type IS TABLE OF test1.description%TYPE; Bulk Binds对性能的提高4 Bulk Binds对性能的提高5 t_id id_type := id_type(); Bulk Binds对性能的提高6 t_description description_type := description_type(); Bulk Binds对性能的提高7 BEGIN Bulk Binds对性能的提高8 FOR i IN 1 .. 100000 LOOP Bulk Binds对性能的提高9 t_id.extend; Bulk Binds对性能的提高10 t_description.extend; Bulk Binds对性能的提高11 Bulk Binds对性能的提高12 t_id(t_id.last) := i; Bulk Binds对性能的提高13 t_description(t_description.last) := 'Description: ' || To_Char(i); Bulk Binds对性能的提高14 END LOOP; Bulk Binds对性能的提高15 Bulk Binds对性能的提高16 FOR i IN t_id.first .. t_id.last LOOP Bulk Binds对性能的提高17 INSERT INTO test1 (id, description) Bulk Binds对性能的提高18 VALUES (t_id(i), t_description(i)); Bulk Binds对性能的提高19 END LOOP; Bulk Binds对性能的提高20 Bulk Binds对性能的提高21 COMMIT; Bulk Binds对性能的提高22 END; Bulk Binds对性能的提高23 / Bulk Binds对性能的提高Bulk Binds对性能的提高PL/SQL procedure successfully completed Bulk Binds对性能的提高Bulk Binds对性能的提高Executed in 141.233 seconds Bulk Binds对性能的提高Bulk Binds对性能的提高SQL> truncate table test1; Bulk Binds对性能的提高Bulk Binds对性能的提高Table truncated Bulk Binds对性能的提高Bulk Binds对性能的提高Executed in 0.631 seconds Bulk Binds对性能的提高Bulk Binds对性能的提高SQL> Bulk Binds对性能的提高SQL> DECLARE Bulk Binds对性能的提高2 TYPE id_type IS TABLE OF test1.id%TYPE; Bulk Binds对性能的提高3 TYPE description_type IS TABLE OF test1.description%TYPE; Bulk Binds对性能的提高4 Bulk Binds对性能的提高5 t_id id_type := id_type(); Bulk Binds对性能的提高6 t_description description_type := description_type(); Bulk Binds对性能的提高7 BEGIN Bulk Binds对性能的提高8 FOR i IN 1 .. 100000 LOOP Bulk Binds对性能的提高9 t_id.extend; Bulk Binds对性能的提高10 t_description.extend; Bulk Binds对性能的提高11 Bulk Binds对性能的提高12 t_id(t_id.last) := i; Bulk Binds对性能的提高13 t_description(t_description.last) := 'Description: ' || To_Char(i); Bulk Binds对性能的提高14 END LOOP; Bulk Binds对性能的提高15 Bulk Binds对性能的提高16 FORALL i IN t_id.first .. t_id.last Bulk Binds对性能的提高17 INSERT INTO test1 (id, description) Bulk Binds对性能的提高18 VALUES (t_id(i), t_description(i)); Bulk Binds对性能的提高19 Bulk Binds对性能的提高20 COMMIT; Bulk Binds对性能的提高21 END; Bulk Binds对性能的提高22 / Bulk Binds对性能的提高Bulk Binds对性能的提高PL/SQL procedure successfully completed Bulk Binds对性能的提高Bulk Binds对性能的提高Executed in 27.52 seconds Bulk Binds对性能的提高Bulk Binds对性能的提高SQL> select count(*) from test1; Bulk Binds对性能的提高Bulk Binds对性能的提高COUNT(*) Bulk Binds对性能的提高---------- Bulk Binds对性能的提高 100000 Bulk Binds对性能的提高Bulk Binds对性能的提高Executed in 0.04 seconds

相关文章:

  • 2021-12-07
  • 2021-08-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-07
  • 2022-12-23
猜你喜欢
  • 2021-09-23
  • 2021-09-06
  • 2021-11-11
  • 2022-03-04
  • 2022-02-27
  • 2021-08-15
  • 2021-08-03
相关资源
相似解决方案