查看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;