并行DDL语句
并行DDL语句支持表和索引。下面是有代表性的可以被并行化的操作:
CREATE TABLE AS SELECT (CTAS) 语句;
创建和重建索引;
创建和校验约束。
另外,对分区表和分区索引来讲,诸如SLIP和MOVE这类分区管理操作也可以被并行化。通常,那些可以利用并行处理的DDL语句可以通过提供PARALLEL子句来指定是否使用并行处理,以及如果被使用,其并行度为多大。无法利用提示来指定并行度。
并行DDL语句默认是启用的。可以使用下面的SQL语句在绘画级别启用或禁用它们:
ALTER SESSION ENABLE PARALLEL DDL
ALTER SESSION DISABLE PARALLEL DDL
也可以使用下面的SQL语句来强制按照指定的并行度(对于支持它的DDL语句)并行地执行:
ALTER SESSION FORCE PARALLEL DDL PARALLEL 4
会话级别查询是否启用并行DDL语句:
SELECT pddl_status FROM v$session WHERE sid=sys_context('userenv','sid')
操作示例如下:
LIBIN@orac>CREATE TABLE t1 AS
2 SELECT rownum AS id, rpad('*',100,'*') AS pad
3 FROM dual
4 CONNECT BY level <= 10000;
表已创建。
已用时间: 00: 00: 00.60
LIBIN@orac>
LIBIN@orac>execute dbms_stats.gather_table_stats(ownname => user, tabname => 't1')
PL/SQL 过程已成功完成。
已用时间: 00: 00: 03.04
LIBIN@orac>
LIBIN@orac>PAUSE
在SESSION级别显示并行DML状态
LIBIN@orac>ALTER SESSION DISABLE PARALLEL DDL;
会话已更改。
已用时间: 00: 00: 00.18
LIBIN@orac>
LIBIN@orac>SELECT pddl_status
2 FROM v$session
3 WHERE /* sid = sys_context('userenv','sid') */
4 audsid = sys_context('userenv','sessionid');
PDDL_STATUS
----------------
DISABLED
已用时间: 00: 00: 00.17
LIBIN@orac>ALTER SESSION ENABLE PARALLEL DDL;
会话已更改。
已用时间: 00: 00: 00.01
LIBIN@orac>
LIBIN@orac>SELECT pddl_status
2 FROM v$session
3 WHERE /* sid = sys_context('userenv','sid') */
4 audsid = sys_context('userenv','sessionid');
PDDL_STATUS
----------------
ENABLED
已用时间: 00: 00: 00.01
LIBIN@orac>ALTER SESSION FORCE PARALLEL DDL PARALLEL 4;
会话已更改。
已用时间: 00: 00: 00.04
LIBIN@orac>
LIBIN@orac>SELECT pddl_status
2 FROM v$session
3 WHERE /* sid = sys_context('userenv','sid') */
4 audsid = sys_context('userenv','sessionid');
PDDL_STATUS
----------------
FORCED
已用时间: 00: 00: 00.03
LIBIN@orac>ALTER SESSION ENABLE PARALLEL DDL;
会话已更改。
已用时间: 00: 00: 00.03
插入操作的并行化测试
插入时并行处理,查询时非并行处理;由(S-P)再到(P-S)
LIBIN@orac>EXPLAIN PLAN FOR CREATE TABLE t2 PARALLEL 2 AS SELECT /*+ no_parallel(t1) */ * FROM t1;
已解释。
已用时间: 00: 00: 00.14
LIBIN@orac>SELECT * FROM table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1933456602
------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | TQ |IN-OUT| PQ Distrib |
------------------------------------------------------------------------------------------------------------------
| 0 | CREATE TABLE STATEMENT | | 10000 | 1015K| 72 (0)| 00:00:01 | | | |
| 1 | PX COORDINATOR | | | | | | | | |
| 2 | PX SEND QC (RANDOM) | :TQ10001 | 10000 | 1015K| 38 (0)| 00:00:01 | Q1,01 | P->S | QC (RAND) |
| 3 | LOAD AS SELECT | T2 | | | | | Q1,01 | PCWP | |
| 4 | BUFFER SORT | | | | | | Q1,01 | PCWC | |
| 5 | PX RECEIVE | | 10000 | 1015K| 38 (0)| 00:00:01 | Q1,01 | PCWP | |
| 6 | PX SEND ROUND-ROBIN| :TQ10000 | 10000 | 1015K| 38 (0)| 00:00:01 | | S->P | RND-ROBIN |
| 7 | TABLE ACCESS FULL | T1 | 10000 | 1015K| 38 (0)| 00:00:01 | | | |
------------------------------------------------------------------------------------------------------------------
已选择14行。
已用时间: 00: 00: 01.18
插入时非并行处理,查询时并行处理;由(P-S)
LIBIN@orac>EXPLAIN PLAN FOR CREATE TABLE t2 NOPARALLEL AS SELECT /*+ parallel(t1 2) */ * FROM t1;
已解释。
已用时间: 00: 00: 00.12
LIBIN@orac>SELECT * FROM table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2883714028
----------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | TQ |IN-OUT| PQ Distrib |
----------------------------------------------------------------------------------------------------------------
| 0 | CREATE TABLE STATEMENT | | 10000 | 1015K| 38 (0)| 00:00:01 | | | |
| 1 | LOAD AS SELECT | T2 | | | | | | | |
| 2 | PX COORDINATOR | | | | | | | | |
| 3 | PX SEND QC (RANDOM) | :TQ10000 | 10000 | 1015K| 21 (0)| 00:00:01 | Q1,00 | P->S | QC (RAND) |
| 4 | PX BLOCK ITERATOR | | 10000 | 1015K| 21 (0)| 00:00:01 | Q1,00 | PCWC | |
| 5 | TABLE ACCESS FULL | T1 | 10000 | 1015K| 21 (0)| 00:00:01 | Q1,00 | PCWP | |
----------------------------------------------------------------------------------------------------------------
已选择12行。
已用时间: 00: 00: 00.07
插入时并行处理,查询时并行处理;由(P-S)
LIBIN@orac>EXPLAIN PLAN FOR CREATE TABLE t2 PARALLEL 2 AS SELECT /*+ parallel(t1 2) */ * FROM t1;
已解释。
已用时间: 00: 00: 00.17
LIBIN@orac>SELECT * FROM table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 494765410
----------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | TQ |IN-OUT| PQ Distrib |
----------------------------------------------------------------------------------------------------------------
| 0 | CREATE TABLE STATEMENT | | 10000 | 1015K| 38 (0)| 00:00:01 | | | |
| 1 | PX COORDINATOR | | | | | | | | |
| 2 | PX SEND QC (RANDOM) | :TQ10000 | 10000 | 1015K| 21 (0)| 00:00:01 | Q1,00 | P->S | QC (RAND) |
| 3 | LOAD AS SELECT | T2 | | | | | Q1,00 | PCWP | |
| 4 | PX BLOCK ITERATOR | | 10000 | 1015K| 21 (0)| 00:00:01 | Q1,00 | PCWC | |
| 5 | TABLE ACCESS FULL | T1 | 10000 | 1015K| 21 (0)| 00:00:01 | Q1,00 | PCWP | |
----------------------------------------------------------------------------------------------------------------
已选择12行。
已用时间: 00: 00: 00.07
并行创建索引测试
两组从属进程同时工作,第一组从属进程读取将编入索引的数据,第二组从属进程将对第一组接收到的数据进行排序,并构造索引.
LIBIN@orac>EXPLAIN PLAN FOR CREATE INDEX i1 ON t1 (id) PARALLEL 4;
已解释。
已用时间: 00: 00: 00.15
LIBIN@orac>SELECT * FROM table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 826494785
------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | TQ |IN-OUT| PQ Distrib |
------------------------------------------------------------------------------------------------------------------
| 0 | CREATE INDEX STATEMENT | | 10000 | 40000 | 12 (0)| 00:00:01 | | | |
| 1 | PX COORDINATOR | | | | | | | | |
| 2 | PX SEND QC (ORDER) | :TQ10001 | 10000 | 40000 | | | Q1,01 | P->S | QC (ORDER) |
| 3 | INDEX BUILD NON UNIQUE| I1 | | | | | Q1,01 | PCWP | |
| 4 | SORT CREATE INDEX | | 10000 | 40000 | | | Q1,01 | PCWP | |
| 5 | PX RECEIVE | | 10000 | 40000 | 11 (0)| 00:00:01 | Q1,01 | PCWP | |
| 6 | PX SEND RANGE | :TQ10000 | 10000 | 40000 | 11 (0)| 00:00:01 | Q1,00 | P->P | RANGE |
| 7 | PX BLOCK ITERATOR | | 10000 | 40000 | 11 (0)| 00:00:01 | Q1,00 | PCWC | |
| 8 | TABLE ACCESS FULL| T1 | 10000 | 40000 | 11 (0)| 00:00:01 | Q1,00 | PCWP | |
------------------------------------------------------------------------------------------------------------------
Note
-----
- estimated index size: 196K bytes
已选择19行。
已用时间: 00: 00: 00.09
并行创建索引
LIBIN@orac>CREATE INDEX i1 ON t1 (id) PARALLEL 4;
索引已创建。
已用时间: 00: 00: 07.60
LIBIN@orac>
非并行重建索引
LIBIN@orac>EXPLAIN PLAN FOR ALTER INDEX i1 REBUILD;
已解释。
已用时间: 00: 00: 00.07
LIBIN@orac>SELECT * FROM table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2094272848
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | ALTER INDEX STATEMENT | | 10000 | 40000 | 38 (0)| 00:00:01 |
| 1 | INDEX BUILD NON UNIQUE| I1 | | | | |
| 2 | SORT CREATE INDEX | | 10000 | 40000 | | |
| 3 | INDEX FAST FULL SCAN| I1 | | | | |
-------------------------------------------------------------------------------
已选择10行。
已用时间: 00: 00: 00.06
并行重建索引
两组从属进程同时工作,第一组从属进程读取将编入索引的数据,第二组从属进程将对第一组接收到的数据进行排序,并构造索引.
LIBIN@orac>EXPLAIN PLAN FOR ALTER INDEX i1 REBUILD PARALLEL 4;
已解释。
已用时间: 00: 00: 00.04
LIBIN@orac>SELECT * FROM table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1882365185
---------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | TQ |IN-OUT| PQ Distrib |
---------------------------------------------------------------------------------------------------------------------
| 0 | ALTER INDEX STATEMENT | | 10000 | 40000 | 11 (0)| 00:00:01 | | | |
| 1 | PX COORDINATOR | | | | | | | | |
| 2 | PX SEND QC (ORDER) | :TQ10001 | 10000 | 40000 | | | Q1,01 | P->S | QC (ORDER) |
| 3 | INDEX BUILD NON UNIQUE | I1 | | | | | Q1,01 | PCWP | |
| 4 | SORT CREATE INDEX | | 10000 | 40000 | | | Q1,01 | PCWP | |
| 5 | PX RECEIVE | | | | | | Q1,01 | PCWP | |
| 6 | PX SEND RANGE | :TQ10000 | | | | | Q1,00 | P->P | RANGE |
| 7 | PX BLOCK ITERATOR | | | | | | Q1,00 | PCWC | |
| 8 | INDEX FAST FULL SCAN| I1 | | | | | Q1,00 | PCWP | |
---------------------------------------------------------------------------------------------------------------------
已选择15行。
已用时间: 00: 00: 00.09