ORACLE的索引并行和表并行

1. ORACLE的表并行和索引并行

1.1. 表并行

查看dba_tables/dba_indexes数据字典时, 可以发现有“DEGREE”字段,这个字段表示的就是数据表的并行度。这个参数的设置,关系着数据库的I/O,以及sql的执行效率。
并行度的优点就是能够最大限度的利用机器的多个cpu资源,是多个cpu同时工作,从而达到提高数据库工作效率的目的。
在系统空闲时间,使用并行是个不错的选择,但是好东西总是相对而言,没有绝对的好坏,不当的使用,同样会引起数据库的新的问题产生。

1. 查看各个表默认的并行度

select table_name,degree from user_tables;

查询并行度大于1的表或者索引

select owner,table_name,degree from dba_tables where degree>1;
select owner,index_name,degree from dba_indexes where degree>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) */
MAX(sal),
AVG(comm)
FROM emp,dept
WHERE emp.deptno=dept.deptno
GROUP BY 1;

或者
SELECT  /*+ parallel(t, 10) */ * from table t;
t为表的别名

非并行度

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并行配置
启用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;

1.2. 并行索引

当我在创建索引时,如果我们想让索引创建更快。可以对索引加并行。
这可能会导致一些问题,作为server候用并行会引起更加严重的争用。所以建议当使用并行后,须要把并行度改回来。

创建索引时加并行

create index ind_t_object_id on test(object_id) parallel 4 ;

查看所有的并行度

select s.degree
      from dba_indexes s
     where s.index_name = upper('ind_t_object_id');

修改索引并行度

alter index ind_t_object_id noparallel;

1.3. 设置sql执行时强制走索引

同样,我们还可以让查询语句强制走索引,加入我们表中已经创建好了一个索引,我们可以在执行sql语句时指定让其走我们的索引即可。

# 设置查询强制走表的索引,可以在sql/plus工具里选中sql语句,按F5,即可查看sql走索引的情况。
SELECT    /*+INDEX(your_table_name MER_NO_INDEX)*/  
*
FROM
    your_table_name
posted @ 2023-06-21 15:43  数据库小白(专注)  阅读(1168)  评论(0编辑  收藏  举报