ORA-01502 state unusable错误成因和解决方法(二)

发表于:2007-07-02来源:作者:点击数: 标签:
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

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 失效的问题,只能通过重建索引来实现。

 

原文转自:http://www.ltesting.net