第一部分:
接到开发人员和业务人员的通知,说一个登陆页面不能用了,报错:
2017-10-24 15:59:02,721 [com.aspire.common.dao.OamUserDAO]- -214:select错误
第二部分:
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 失效的问题,只能通过重建索引来实现。
------------------------------------------------------------------------------------
【总结】
1、移动table的表空间后,基于该table的索引会自动失效UNUSABLE;此时访问或操作该table时,会报ORA-01502异常;
2、对于普通索引,可以通过跳过索引或重建索引来解决ORA-01502异常的问题;
3、对于唯一索引,则只能通过重建索引来解决ORA-01502异常的问题。
【参考】
-- 创建普通索引
SQL> alter index idxt unusable;
-- 设置 skip_unusable_indexes=true;
SQL> alter session set skip_unusable_indexes=true;
--重建索引
SQL> alter index idxt rebuild;
--创建唯一索引
SQL> create unique index idx_t on t(a);
查询索引状态:
SQL> select index_name,index_type,tablespace_name,table_type,status from user_indexes where index_name='IDXT';
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31392094/viewspace-2146311/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/31392094/viewspace-2146311/