ORA-01502 state unusable错误成因和解决方法(二)
2024-07-21 02:09:43
供稿:网友
sql> create table t(a number);
table created.
现在,我们建立一个唯一索引来看看:
sql> create unique index idx_t on t(a);
index created.
sql> select index_name,index_type,tablespace_name,table_type,status from user_indexes where index_name='t';
no rows selected
sql> select index_name,index_type,tablespace_name,table_type,status from user_indexes where index_name='idx_t';
index_name index_type tablespace_name table_type status
------------------------------ --------------------------- ------------------------------ ----------- --------
idx_t normal data_dynamic table valid
sql> insert into t values(1);
1 row created.
sql> commit;
commit complete.
将索引手工修改为unusable状态(模拟发生索引失效的情况):
sql> alter index idx_t unusable;
index altered.
sql> select index_name,index_type,tablespace_name,table_type,status from user_indexes where index_name='idx_t';
index_name index_type tablespace_name table_type status
------------------------------ --------------------------- ------------------------------ ----------- --------
idx_t normal data_dynamic table unusable
我们看到这是,已经不能正常往表中插入数据:
sql> insert into t values(2);
insert into t values(2)
*
error at line 1:
ora-01502: index 'misc.idx_t' or partition of such index is in unusable state
首先,我们通过重建索引(rebuild index)的方法来解决问题:
sql> alter index idx_t rebuild;
index altered.
sql> select index_name,index_type,tablespace_name,table_type,status from user_indexes where index_name='idx_t';
index_name index_type tablespace_name table_type status
------------------------------ --------------------------- ------------------------------ ----------- --------
idx_t normal data_dynamic table valid
sql> insert into t values(2);
1 row created.
sql> commit;
commit complete.
sql>
现在我们再次模拟索引失效(unusable状态):
sql> alter index idx_t unusable;
index altered.
sql> select index_name,index_type,tablespace_name,table_type,status from user_indexes where index_name='idx_t';
index_name index_type tablespace_name table_type status
------------------------------ --------------------------- ------------------------------ ----------- --------
idx_t normal data_dynamic table unusable
sql> insert into t values(3);
insert into t values(3)
*
error at line 1:
ora-01502: index 'misc.idx_t' or partition of such index is in unusable state
然后,看看是否可以通过设置参数skip_unusable_indexes=true来解决问题:
sql> alter session set skip_unusable_indexes=true;
session altered.
sql> insert into t values(3);
insert into t values(3)
*
error at line 1:
ora-01502: index 'misc.idx_t' or partition of such index is in unusable state
sql> select index_name,index_type,tablespace_name,table_type,status from user_indexes where index_name='idx_t';
index_name index_type tablespace_name table_type status
------------------------------ --------------------------- ------------------------------ ----------- --------
idx_t normal data_dynamic table unusable
sql> alter index idx_t rebuild;
index altered.
sql> select index_name,index_type,tablespace_name,table_type,status from user_indexes where index_name='idx_t';
index_name index_type tablespace_name table_type status
------------------------------ --------------------------- ------------------------------ ----------- --------
idx_t normal data_dynamic table valid
sql> insert into t values(3);
1 row created.
sql> commit;
commit complete.
sql>
很显然,对于unique index,通过简单的设置参数是不能解决问题的,要解决unique index 失效的问题,只能通过重建索引来实现。