alter index rebuild与alter index rebuild online的扫描方式是不一样的。可以举个简单的例子来说明:
SQL>create table sjh.test as select * from dba_users; --建一个测试表
SQL> explain plan for
2 alter index test_idx rebuild;
2 alter index test_idx rebuild;
Explained
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 2554748275
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
--------------------------------------------------------------------------------
| 0 | ALTER INDEX STATEMENT | | 327 | 4251 | 1 (0)| 00:00:0
| 1 | INDEX BUILD NON UNIQUE| TEST_IDX | | | |
| 2 | SORT CREATE INDEX | | 327 | 4251 | |
| 3 | INDEX FAST FULL SCAN| TEST_IDX | 327 | 4251 | 1 (0)| 00:00:0
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Plan hash value: 2554748275
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
--------------------------------------------------------------------------------
| 0 | ALTER INDEX STATEMENT | | 327 | 4251 | 1 (0)| 00:00:0
| 1 | INDEX BUILD NON UNIQUE| TEST_IDX | | | |
| 2 | SORT CREATE INDEX | | 327 | 4251 | |
| 3 | INDEX FAST FULL SCAN| TEST_IDX | 327 | 4251 | 1 (0)| 00:00:0
--------------------------------------------------------------------------------
10 rows selected
SQL> explain plan for
2 alter index test_idx rebuild online;
2 alter index test_idx rebuild online;
Explained
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1149508270
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
--------------------------------------------------------------------------------
| 0 | ALTER INDEX STATEMENT | | 327 | 4251 | 1 (0)| 00:00:0
| 1 | INDEX BUILD NON UNIQUE| TEST_IDX | | | |
| 2 | SORT CREATE INDEX | | 327 | 4251 | |
| 3 | TABLE ACCESS FULL | TEST | 327 | 4251 | 1 (0)| 00:00:0
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Plan hash value: 1149508270
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
--------------------------------------------------------------------------------
| 0 | ALTER INDEX STATEMENT | | 327 | 4251 | 1 (0)| 00:00:0
| 1 | INDEX BUILD NON UNIQUE| TEST_IDX | | | |
| 2 | SORT CREATE INDEX | | 327 | 4251 | |
| 3 | TABLE ACCESS FULL | TEST | 327 | 4251 | 1 (0)| 00:00:0
--------------------------------------------------------------------------------
10 rows selected
SQL>
--从上面例子红色标签位置可以很清楚的看到 index rebuild online实质上是扫描表而不是扫描现有的索引块来实现索引的重建,index rebuild 只扫描现有的索引块来实现索引的重建。
除了扫描方式不同外,rebuild 会阻塞dml语句而rebuild online则不会(ddl语句除外)。
rebuild online时系统会产生一个SYS_JOURNAL_xxx的IOT类型的系统临时日志表,所有rebuild online时索引的变化都记录在这个表中(但要使用两倍于传统方法的空间),当新的索引创建完成后,把这个表的记录维护到新的索引中去,然后drop掉旧的索引,rebuild online就完成了。
rebuild online时系统会产生一个SYS_JOURNAL_xxx的IOT类型的系统临时日志表,所有rebuild online时索引的变化都记录在这个表中(但要使用两倍于传统方法的空间),当新的索引创建完成后,把这个表的记录维护到新的索引中去,然后drop掉旧的索引,rebuild online就完成了。
注意并行处理,DDL,位图索引不能使用ONLINE。
在rebuild online 时会报错:
ORA-08120: Need to create SYS.IND_ONLINE$ table in order to (re)build index
Cause: Alter index Build/Rebuild online require existing of SYS.IND_ONLINE$ table.
Action: User/DBA needs to create sys.ind_online$ before alter the index /rdbms/admin/catcio.sql contains script to create ind_online$.
ORA-08120: Need to create SYS.IND_ONLINE$ table in order to (re)build index
Cause: Alter index Build/Rebuild online require existing of SYS.IND_ONLINE$ table.
Action: User/DBA needs to create sys.ind_online$ before alter the index /rdbms/admin/catcio.sql contains script to create ind_online$.
解决方法就是运行catcio.sql 创建SYS.IND_ONLINE$表