今天一个脚本里面的一条build index老是出现ORA-00054: resource busy and acquire with NOWAIT specified 错误,错误本身比较容易理解,肯定是有排他锁限制了对Partition Index的DDL操作。 那么如何避免,以及使用分区索引需要注意的事情,都是需要去总结的,所以下面我结合实验,简单总结一下重建Local Index:
1. DML操作对index partition rebuild的影响。 如果在rebuild某一个分区的时候,有其他的进程在对这个index partition所对应的数据进行DML操作,rebuild会受影响:
kl@k02> create table t_part_1 (a number, b varchar2(10))
2 partition by range(a)
3 (partition t1 values less than (10),
4 partition t2 values less than (20),
5 partition t3 values less than (30),
6 partition tmax values less than (maxvalue));
Table created.
--- 不管是btree index还是bitmap index:
kl@k02> create index t_part_ind1 on t_part_1 (a) local;
Index created.
kl@k02> insert into t_part_1 values (21,'AAA');
1 row created.
< no commit>
--- 然后重新开一个session:
kl@k02> alter index t_part_ind1 rebuild partition t3;
alter index t_part_ind1 rebuild partition t3
*
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified
2. 是否考虑加online这个参数,这个参数加上加上以后,除了rebuild过程中index 保持online状态,Oracle还会在rebuild index之前等待所有DML操作结束,然后得到DDL锁,开始rebuild.
kl@k02> alter index t_part_ind1 rebuild partition t3 online;
Index altered.
---- 如果不commit,上面的操作就会一直hold。
3. 如果是bitmap index, online 选项失效
kl@k02> alter index t_part_ind1 rebuild partition t3 online;
alter index t_part_ind1 rebuild partition t3 online
*
ERROR at line 1:
ORA-08108: may not build or rebuild this type of index online
4. 在split以后partition index的状态是usable, 所以split以后不需要重建索引。
5. 在drop 分区 local index的时候,只能drop 整个index; 而rebuild index时只能一个一个分区来drop.
6. 如果Local index失效,在查询时会出现如下问题:
kl@k02> select /*+ full */ * from t_part_1 where a=31;
select /*+ full */ * from t_part_1 where a=31
*
ERROR at line 1:
ORA-01502: index 'KL.T_PART_IND1' or partition of such index is in unusable state
---- 如果走全表扫描,则不会出现:
kl@k02> select /*+ full(t) */ * from t_part_1 t where a=31;
no rows selected
总结: Local Index对查询性能的提高是显而易见的,但他在维护过程中的成本也是比较高的,所以大家在使用Local Index的时候,需要仔细评估一下,如果不经常被使用(比如用MV,代替start-transformation),最好避免使用。