Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2119731
  • 博文数量: 1647
  • 博客积分: 80000
  • 博客等级: 元帅
  • 技术积分: 9980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 15:15
文章分类

全部博文(1647)

文章存档

2011年(1)

2008年(1646)

我的朋友

分类:

2008-10-28 18:17:12

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

--------------------next---------------------

阅读(277) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~