今天在选择一条记录进行做删除操作时,碰见index失败的问题,如下:
处理失败!错误信息:[SQLException ORA-01502 index VHL_V6.PK_WEB_APP_TGT_OBJ or partition of such index is in unusable state ]
在网上查询了下,知道原因,就上数据库查了一下,结果如下:
1 PK_WEB_APP_TGT_OBJ NORMAL VHL_V6 TABLE UNUSABLE。
显然是UNUSABLE状态,那也就知道原因了,呵呵,解决!
以下为参考文件:
SQL> create table t(a number);
Table created.
1、现在,我们建立一个唯一索引来看看:
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.
2、将索引手工修改为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
3、首先,我们通过重建索引(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>
4、现在我们再次模拟索引失效(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
5、然后,看看是否可以通过设置参数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>
先使索引失效 DISABLE alter table TEST_TABLE add constraint TEST_TABLE primary key (TEST_ID) 删除主键索引 drop index test_id; 曾经无意中跑了一个建表脚本,建了两百多张表,为赶时间仓促间只是简单的DROP表,结果导致user_object中INDEX的记录出现很多乱码,一定要删除索引先,是以为戒 |
总结:对于unique index,通过简单的设置参数是不能解决问题的,要解决unique index 失效的问题,只能通过重建索引来实现。