您的位置:首页 > 数据库 > Oracle

Oracle rebuild index 使用 parallel 时 与 并行度 的注意事项

2017-01-08 11:57 549 查看


一.Rebuild 索引 与 并行度 说明

在之前的Blog里整理了一些列有关索引相关的Blog,如下:

 

Oracle 索引 详解

http://blog.csdn.net/tianlesoftware/article/details/5347098

 

如何加快建index 索引 的时间

http://blog.csdn.net/tianlesoftware/article/details/5664019

 

Oracle 索引扫描的五种类型

http://blog.csdn.net/tianlesoftware/article/details/5852106

 

Oracle 索引的维护

http://blog.csdn.net/tianlesoftware/article/details/5680706

 

Oracle alterindex rebuild 与ORA-08104 说明

http://blog.csdn.net/tianlesoftware/article/details/6538928

 

在索引create 和rebuild的时候,在CPU 允许的情况下,我们可以使用parallel来加快操作的速度。但是这里有一个注意的问题,有关索引的并行度,这个对表同样要注意。

 

对于OLTP类型的数据库,除非只用于做统计、报表类的表或索引,建议不对相关表或索引调置并行度。在数据库有开启并行查询的情况下,在表或索引上存在默认并行度,将导致数据库优先采用全表或全索引扫描的执行计划,另外将生成多个并行子进程,对于OLTP类应用将反而降低相关SQL的执行效率。

 

       有关parallel,官网的说明如下:

http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_7002.htm#i2159323

 

The parallel_clause letsyou parallelize creation of the table and set the default degree of parallelismfor queries and the DML INSERT, UPDATE, DELETE,andMERGE after table creation.

 

(1)    NOPARALLEL: Specify NOPARALLEL forserial
execution. This is the default,即并行度为1.

 

(2)PARALLEL:  Specify PARALLEL ifyou
want Oracle to select a degree of parallelism equal to the number of CPUsavailable on all participating instances times the value ofthe PARALLEL_THREADS_PER_CPU initialization parameter.

 

(3)PARALLEL integer: Specificationof integer indicates
the degree of parallelism, which is thenumber of parallel threads used in the parallel operation. Each parallel threadmay use one or two parallel execution servers. Normally Oracle calculates the optimumdegree of parallelism, so it is not necessary for you
to specify integer.

 

       Oracle在并行处理时,会启动多少个并行进程来同时执行任务,并行度越高, 并行进程越多,执行速度 会越快,默认是noparallel,如果我们设置并行度为default值,那么此时的并行度是:

服务器CPU数*每个CPU启用的线程数(PARALLEL_THREADS_PER_CPU)

 

       所以一般我们建议使用Noparallel,或者将并行度设置为1,而不是default。

 

       可以通过dba_tables 和 dba_indexes 视图的degree 字段来查看相关对象的并行度。

 

       要注意的就是在我们用并行来rebuild索引的时候,rebuild结束后,我们索引的并行度也会改成我们rebuild的并行度,所以在我们rebuild 结束之后还需要对索引的并行度进行一个修改操作。

 

 


二.示例

测试环境: win7 +oracle 11.2.0.1

 

SQL> select * from v$version where rownum=1;

 

BANNER

-----------------------------------------------------------------------------

Oracle Database 11g Enterprise EditionRelease 11.2.0.1.0 - 64bit Production

 

 

--查看并行参数:

SQL> show parameter parallel_max_servers

 

NAME                                 TYPE        VALUE

----------------------------------------------- ------

parallel_max_servers                 integer     20

 

SQL> show parameter PARALLEL_THREADS_PER_CPU

 

NAME                                 TYPE        VALUE

----------------------------------------------- ------

parallel_threads_per_cpu             integer     2

 

 

--测试表Dave信息:

SQL> select count(*) from dave;

 

 COUNT(*)

----------

333798

 

SQL> col segment_name for a15

SQL> l

  1*select segment_name,bytes/1024/1024||'M' as "size" from dba_segmentswhere segment_name='DAVE' and owner='SYS'

SQL> /

 

SEGMENT_NAME    size

--------------------------------------------------------

DAVE            38M

 

 

--使用默认值创建索引:

SQL> create index idx_dave_id on dave(object_id) ;

Index created.

SQL>

 

--查看默认值:

SQL> select degree from dba_indexes where index_name='IDX_DAVE_ID';

 

DEGREE

----------------------------------------

1

--这里默认为noparallel,即为1.

 

 

--使用并行度为4,对索引进行rebuild:

SQL> ater index idx_dave_id rebuildparallel 4;

Index altered.

 

--在次查看索引的并行度:

SQL> select degree from dba_indexes where index_name='IDX_DAVE_ID';

 

DEGREE

----------------------------------------

4

 

注意:


这里变成了4,也就是说启用了索引自己的并行,这样在我们使用索引时会影响执行计划,也会消耗很多的资源。所以,我们需要对这个并行度进行修改,改成noparallel。

 

--修改并行度为noparallel:

SQL> alter index idx_dave_id noparallel;

Index altered.

 

--查看并行度:

SQL> select degree from dba_indexes where index_name='IDX_DAVE_ID';

 

DEGREE

----------------------------------------

1

 

这里又变成了1.

 

 

使用并行来处理时,我们可以通过v$px_session 来查看相关的等待事件:

SQL> select a.sql_id,a.event,count(*)from v$session a,v$px_session b where a.sid=b.sid group by a.sql_id,a.event;

 

 

 

关于并行的更多测试参考:

OracleParallel Execution(并行执行)

http://blog.csdn.net/tianlesoftware/article/details/5854583

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: