使用列列表插入并行DML

时间:2014-11-15 05:19:53

标签: sql oracle performance oracle11g dml

首先,我在11gR2

SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
PL/SQL Release 11.2.0.3.0 - Production
CORE    11.2.0.3.0      Production

我正在尝试使用PARALLEL DML并行化一个非常大的INSERT语句,该语句批量加载数百万行。为实现此目的,我通过ALTER SESSION ENABLE PARALLEL DML启用并行DML。我的插页看起来像这样 -

INSERT INTO TAB_NEW (COL1, COL2, COL3, COL4, ...)  --32 columns                
 SELECT COL1,
        COL2,
        COL3,
        ....
        ....  --32 columns selected
   FROM TAB_A a, TAB_B b,...
   WHERE ....; --A bunch of joins here

我正在尝试Statement Level并行DML,就像这样 -

尝试1 -

INSERT /*+ PARALLEL(16) */ TAB_NEW (COL1, COL2, COL3, COL4, ...)  --32 columns
 SELECT COL1,
        COL2,
        COL3,
        ....
        ....  --32 columns selected
   FROM TAB_A a, TAB_B b,...
   WHERE ....; --A bunch of joins here

但是,上述不会并行运行。以下是 -

尝试2 -

INSERT /*+ PARALLEL(16) */ TAB_NEW (COL1, COL2, COL3, COL4, ...)  --32 columns
 SELECT /*+ PARALLEL(16) */
        COL1,
        COL2,
        COL3,
        ....
        ....  --32 columns selected
   FROM TAB_A a, TAB_B b,...
   WHERE ....; --A bunch of joins here

OR

尝试3 -

INSERT /*+ PARALLEL(16) */  INTO TAB_NEW
 SELECT COL1,
        COL2,
        COL3,
        ....
        ....  --32 columns selected
   FROM TAB_A a, TAB_B b,...
   WHERE ....; --A bunch of joins here

是否由于尝试1中INSERT语句中的列列表而发生这种情况?

2 个答案:

答案 0 :(得分:0)

这适用于下面的简单示例,适用于11.2.0.3(EE,64位,Solaris)和12.1.0.1(EE,64位,Windows)。

这意味着您遇到了一个非常具体的错误,可能与您的对象,语句或环境的某些细节有关。你的下一步是  修改您的查询直到它与我的查询匹配的痛苦过程,寻找使它停止工作的微不足道的不同。

示例表

--drop table tab_new;
--drop table tab_a;
--drop table tab_b;
--drop table tab_c;

create table tab_new(col01 number,col02 number,col03 number,col04 number,col05 number,col06 number,col07 number,col08 number,col09 number,col10 number,col11 number,col12 number,col13 number,col14 number,col15 number,col16 number,col17 number,col18 number,col19 number,col20 number,col21 number,col22 number,col23 number,col24 number,col25 number,col26 number,col27 number,col28 number,col29 number,col30 number,col31 number,col32 number) nologging;
create table tab_a  (col01 number,col02 number,col03 number,col04 number,col05 number,col06 number,col07 number,col08 number,col09 number,col10 number,col11 number,col12 number,col13 number,col14 number,col15 number,col16 number,col17 number,col18 number,col19 number,col20 number,col21 number,col22 number,col23 number,col24 number,col25 number,col26 number,col27 number,col28 number,col29 number,col30 number,col31 number,col32 number) nologging;
create table tab_b  (col01 number,col02 number,col03 number,col04 number,col05 number,col06 number,col07 number,col08 number,col09 number,col10 number,col11 number,col12 number,col13 number,col14 number,col15 number,col16 number,col17 number,col18 number,col19 number,col20 number,col21 number,col22 number,col23 number,col24 number,col25 number,col26 number,col27 number,col28 number,col29 number,col30 number,col31 number,col32 number) nologging;
create table tab_c  (col01 number,col02 number,col03 number,col04 number,col05 number,col06 number,col07 number,col08 number,col09 number,col10 number,col11 number,col12 number,col13 number,col14 number,col15 number,col16 number,col17 number,col18 number,col19 number,col20 number,col21 number,col22 number,col23 number,col24 number,col25 number,col26 number,col27 number,col28 number,col29 number,col30 number,col31 number,col32 number) nologging;

<强>语句

rollback;
alter session enable parallel dml;
explain plan for
INSERT /*+ PARALLEL(16) */ INTO TAB_NEW (col01,col02,col03,col04,col05,col06,col07,col08,col09,col10,col11,col12,col13,col14,col15,col16,col17,col18,col19,col20,col21,col22,col23,col24,col25,col26,col27,col28,col29,col30,col31,col32)
SELECT a.col01,a.col02,a.col03,a.col04,a.col05,a.col06,a.col07,a.col08,a.col09,a.col10,a.col11,a.col12,a.col13,a.col14,a.col15,a.col16,a.col17,a.col18,a.col19,a.col20,a.col21,a.col22,a.col23,a.col24,a.col25,a.col26,a.col27,a.col28,a.col29,a.col30,a.col31,a.col32
  FROM TAB_A a, TAB_B b, TAB_C c
 WHERE a.col01 = b.col01
   AND b.col01 = c.col01;

select * from table(dbms_xplan.display(format => 'basic'));

解释计划

LOAD AS SELECT操作及其在PX操作下的位置意味着数据会并行插入。

Plan hash value: 1632580283

-------------------------------------------------------
| Id  | Operation                          | Name     |
-------------------------------------------------------
|   0 | INSERT STATEMENT                   |          |
|   1 |  PX COORDINATOR                    |          |
|   2 |   PX SEND QC (RANDOM)              | :TQ10003 |
|   3 |    LOAD AS SELECT                  | TAB_NEW  |
|   4 |     OPTIMIZER STATISTICS GATHERING |          |
|   5 |      HASH JOIN                     |          |
|   6 |       HASH JOIN                    |          |
|   7 |        PX RECEIVE                  |          |
|   8 |         PX SEND HASH               | :TQ10000 |
|   9 |          PX BLOCK ITERATOR         |          |
|  10 |           TABLE ACCESS FULL        | TAB_A    |
|  11 |        PX RECEIVE                  |          |
|  12 |         PX SEND HASH               | :TQ10001 |
|  13 |          PX BLOCK ITERATOR         |          |
|  14 |           TABLE ACCESS FULL        | TAB_B    |
|  15 |       PX RECEIVE                   |          |
|  16 |        PX SEND HASH                | :TQ10002 |
|  17 |         PX BLOCK ITERATOR          |          |
|  18 |          TABLE ACCESS FULL         | TAB_C    |
-------------------------------------------------------

答案 1 :(得分:0)

因此,并行DML不会在PARALLEL中执行查询的原因是因为表TAB_NEW上有一个启用了它的触发器。对PARALLEL DML有限制,并且该表上的任何并行DML尝试将仅由于触发而运行串行。触发器被禁用后,INSERT并行运行。

可以找到完整的限制列表here