为一个包含上千万数据量的大表创建索引是一个不小的消耗。为了尽快完成任务可以采用nologging和并行相结合的方法。
虽然使用nologging的方式创建索引会有小小的隐患,也就是该索引不受redo保护,一旦介质故障该索引无法恢复。
此时访问索引会出现ORA-26040: Data block was loaded using the NOLOGGING opation
此时访问索引会出现ORA-26040: Data block was loaded using the NOLOGGING opation
但索引和表不一样,不能用了可以rebuild。并行度需要注意的是它的开销,如果cpu已经非常繁忙的时候就不适合这么做。下面看一下语句示例:
1. 创建bigtable表空间和其索引表空间
[oracle@snow ~]$
sqlplus u1/u1@pdb1
SQL*Plus: Release 12.1.0.1.0 Production on Tue May 5 01:26:18 2015
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Last Successful login time: Mon May 04 2015 20:20:28 -04:00
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
U1@pdb1 >
create tablespace bigtable_tbs datafile '/home/oracle/dbfile/cdb/pdb1/bigtable_tbs01.dbf' size 10m;
U1@pdb1 >
create tablespace bigtable_idx_tbs datafile '/home/oracle/dbfile/cdb/pdb1/bigtable_idx_tbs01.dbf' size 10m;
2. 创建bigtable并插入数据
U1@pdb1 >
create table bigtable (id number,name varchar2(50));
U1@pdb1 >
insert into bigtable(name) select object_name from dba_objects;
U1@pdb1 > commit;
3. 创建索引使用nologging并开启并行度4
U1@pdb1 >
create index bigtable_idx on bigtable(name) tablespace bigtable_idx_tbs nologging parallel 4;
U1@pdb1 > col index_name for a20
U1@pdb1 > col degree for a20
U1@pdb1 > select index_name,degree from user_indexes;
INDEX_NAME DEGREE
-------------------- --------------------
BIGTABLE_IDX 4
注意:并行度是可选的操作,根据需要可以自行调节,也可以不设置。Oracle默认会根据CPU数目乘以PARALLEL_THREDS_PER_CPU的值来选择并行度。通过实验证明不应定每次都会生效。我的实验环境PARALLEL_THREDS_PER_CPU值为2,但是创建的索引degree都是1。
U1@pdb1 > conn / as sysdba
SYS@cdb > show parameter parallel_threads_per_cpu
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
parallel_threads_per_cpu integer 2
SYS@cdb >
SYS@cdb > conn u1/u1@pdb1
U1@pdb1 > drop index bigtable_idx;
U1@pdb1 > create index bigtable_idx on bigtable(name) nologging;
U1@pdb1 > select index_name,degree from user_indexes;
INDEX_NAME DEGREE
-------------------- --------------------
BIGTABLE_IDX 1
完
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29047826/viewspace-1777653/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29047826/viewspace-1777653/