查看dba_tables数据字典时,可以发现有“DEGREE”字段,这个字段表示的就是数据表的并行度。这个参数的设置,关系着数据库的I/O,以及sql的执行效率。
 
 
并行度的优点就是能够最大限度的利用机器的多个cpu资源,是多个cpu同时工作,从而达到提高数据库工作效率的目的。在系统空闲时间,使用并行是个不错的选择,但是好东西总是相对而言,没有绝对的好坏,不当的使用,同样会引起数据库的新的问题产生。
 
1、  此参数的大小设置
 
orcl@ SCOTT> select table_name,degree from user_tables;
 
 
TABLE_NAME                     DEGREE
 
------------------------------         --------------------
 
T1                                      1
 
TAB_REGISTER                            1
 
EMP                                     1
 
EMP_BAK                                 1
 
SALGRADE                                1
 
BONUS                                   1
 
DEPT                                    1
 
LETTER_USER                             1
 
T2                                      1
 
BASE_LOG                                1
 
T                                       1
 
通过上例的例子大家可以观察,此参数的默认值为1,这个数值,我们认为的增加,当设置表的并行度非常高的时候,sql优化器将可能对表进行全表扫描,引起 Direct Path Read 等待 。
 
在使用并行查询前需要慎重考虑, 因为并行查询尽管能提高程序的响应时间, 但是会
 
消耗比较多的资源。 对于低配置的数据库服务器需要慎重。 此外, 需要确认并行度的设置要与 IO 系统的配置相符(建议并行度为 2~4 * CPU 数) 。 
 
2、  并行度的修改
 
alter table t parallel(degree 1);------直接指定表的并行度
 
alter table t parallel;    ----------设置表的并行度为default
 
3、  如何在sql语句中使用表的并行度,并选择合适的并行等级
 
示例:使用并行查询的执行计划
 
并行度为4
 
orcl@ SCOTT> SELECT /*+ PARALLEL(4) */
 
2   MAX(sal),
 
3   AVG(comm)
 
4    FROM emp,dept
 
5   WHERE emp.deptno=dept.deptno
 
6   GROUP BY 1
 
7  ;
 
 
已用时间:  00: 00: 00.09
 
 
执行计划
 
----------------------------------------------------------
 
Plan hash value: 1358624651
 
 
---------------------------------------------------------------------------------------------------------------
 
| Id  | Operation             | Name     | Rows  | Bytes | Cost (%CPU)| Time     |    TQ  |IN-OUT| PQ Distrib |
 
---------------------------------------------------------------------------------------------------------------
 
|   0 | SELECT STATEMENT      |          |    14 |   126 |     2   (0)| 00:00:01 |        |      |         |
 
|   1 |  HASH GROUP BY        |          |    14 |   126 |     2   (0)| 00:00:01 |        |      |         |
 
|   2 |   PX COORDINATOR      |          |       |       |            |          |        |      |         |
 
|   3 |    PX SEND QC (RANDOM)| :TQ10000 |    14 |   126 |     2   (0)| 00:00:01 |  Q1,00 | P->S | QC (RAND)  |
 
|   4 |     PX BLOCK ITERATOR |          |    14 |   126 |     2   (0)| 00:00:01 |  Q1,00 | PCWC |         |
 
|*  5 |      TABLE ACCESS FULL| EMP      |    14 |   126 |     2   (0)| 00:00:01 |  Q1,00 | PCWP |         |
 
---------------------------------------------------------------------------------------------------------------
 
 
Predicate Information (identified by operation id):
 
---------------------------------------------------
 
 
5 - filter("EMP"."DEPTNO" IS NOT NULL)
 
 
Note
 
-----
 
- automatic DOP: computed degree of parallelism is 4
 
 
 
统计信息
 
----------------------------------------------------------
 
20  recursive calls
 
0  db block gets
 
5  consistent gets
 
0  physical reads
 
0  redo size
 
482  bytes sent via SQL*Net to client
 
416  bytes received via SQL*Net from client
 
2  SQL*Net roundtrips to/from client
 
1  sorts (memory)
 
0  sorts (disk)
 
1  rows processed
 
 
非并行度
 
SELECT /*+ no_parallel */ ename, dname FROM emp e, dept d
 
WHERE e.deptno=d.deptno;
 
自动并行度
 
SELECT /*+ parallel(auto) */ ename, dname FROM emp e, dept d
 
WHERE e.deptno=d.deptno;
 
4、  并行查询的使用范围
 
适用于:
 
大表查询,join,分区索引的查询,
 
创建大量的index,
 
创建大量的表(包括固化视图),
 
批量的insert,update,delete;
 
查行执行适合场景:
 
对称多处理器,集群,并行系统,
 
cpu利用不足,
 
足够的内存用于其他操作,排序,hash,缓存,
 
查行执行适合与dss与数据仓库,也适合于批量操作的OLTP系统,不适合OLTP简介的dml或select操作;
 
并行执行不适合场景:
 
非常短的查询或事务
 
基本硬件要求:
 
并行执行设计需要多个cpu与io来实现快速的查询,每个硬件都应该维持在同一个吞吐量
 
哪些操作可以用并行
 
全表查询,分区查询,索引快速查询
 
join操作
 
nested loop, sort merge, hash, and star transformation
 
DDL语句
 
CREATE TABLE AS SELECT,  CREATEINDEX, REBUILDINDEX,
 
REBUILD INDEX PARTITION, 
 
And MOVE/SPLIT/COALESCEPARTITION
 
DML语句
 
INSERT AS SELECT,UPDATE,DELETE,
 
And MERGE operations
 
5、  在并行操作中默认情况并行查询和并行DDL操作可以无障碍使用并行,但是如果想使用并行DML,需要先修改dml并行配置
 
alter session enable parallel dml;
 
11g new feature:
For a statement-level PARALLEL hint:
■ PARALLEL: The statement always is run parallel, and the database computes the
degree of parallelism, which can be 2 or greater.
■ PARALLEL (DEFAULT): The same as PARALLEL. The DEFAULT keyword is
included for completeness.
■ PARALLEL (AUTO): The database computes the degree of parallelism, which can be
1 or greater. If the computed degree of parallelism is 1, then the statement runs
serially.
■ PARALLEL (MANUAL): The optimizer is forced to use the parallel settings of the
objects in the statement.
■ PARALLEL (integer): The optimizer uses the degree of parallelism specified by
integer.
 
 
In the following example, the optimizer calculates the degree of parallelism. The
statement always runs in parallel.
SELECT /*+ PARALLEL */ last_name
FROM employees;
In the following example, the optimizer calculates the degree of parallelism, but that
degree may be 1, in which case the statement will run serially.
SELECT /*+ PARALLEL (AUTO) */ last_name
FROM employees;
 
 
11g new feature:
For a statement-level PARALLEL hint:
■ PARALLEL: The statement always is run parallel, and the database computes the
degree of parallelism, which can be 2 or greater.
■ PARALLEL (DEFAULT): The same as PARALLEL. The DEFAULT keyword is
included for completeness.
■ PARALLEL (AUTO): The database computes the degree of parallelism, which can be
1 or greater. If the computed degree of parallelism is 1, then the statement runs
serially.
■ PARALLEL (MANUAL): The optimizer is forced to use the parallel settings of the
objects in the statement.
■ PARALLEL (integer): The optimizer uses the degree of parallelism specified by
integer.
In the following example, the optimizer calculates the degree of parallelism. The
statement always runs in parallel.
SELECT /*+ PARALLEL */ last_name
FROM employees;
In the following example, the optimizer calculates the degree of parallelism, but that
degree may be 1, in which case the statement will run serially.
SELECT /*+ PARALLEL (AUTO) */ last_name
FROM employees;