暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 只为1个分区重建索引

ASKTOM 2020-03-30
599

问题描述

嗨,
我们的表上有分区。我们只想为一个分区构建索引。如果我们这样做,那么它会影响应用程序吗?有一次,我们在无法使用状态下出现了类似索引的错误。

首先,我们在不可用的状态下创建索引,如:
Create index idx_name on table(a) local unusable;


然后只为1个分区重建:
Alter index idx_name rebuild partition one online;


假设有两个分区。那么,当它尝试将idx_name用于二分区时,它会影响appln吗?
就像我提到的,我们确实得到了一个错误,但在那之后没有发生这样的错误。所以,我们不明白它发生的原因。

我还有一个疑问:
当我们离线重建索引时,会发生独占表锁定。
如果我们在线重建,表锁会发生吗?

专家解答

如果您将skip_unusable_indexes设置为true (默认值),DML应该继续正常。如果是false,那么您将无法在不可用的分区上使用ins/upd/del:

create table t (
  c1 int, c2 int
) partition by list ( c1 ) (
  partition p1 values ( 1 ),
  partition p2 values ( 2 )
);

create index i 
  on t ( c2 )
  local unusable;
  
alter index i
  rebuild partition p1 online;
  
alter session set skip_unusable_indexes = false;
  
insert into t values ( 1, 1 );
insert into t values ( 2, 2 );

ORA-01502: index 'CHRIS.I' or partition of such index is in unusable state

alter session set skip_unusable_indexes = true;

insert into t values ( 1, 1 );
insert into t values ( 2, 2 );

select * from t;

C1    C2   
    1     1 
    1     1 
    2     2 


优化器也将无法使用不可用的分区。这可能会导致一些不带分区键的查询的时髦计划:

set serveroutput off

select * from t
where  c2 = 2;

select * 
from   table(dbms_xplan.display_cursor(null, null, 'BASIC LAST'));

--------------------------------------------------------    
| Id  | Operation                            | Name    |    
--------------------------------------------------------    
|   0 | SELECT STATEMENT                     |         |    
|   1 |  VIEW                                | VW_TE_2 |    
|   2 |   UNION-ALL                          |         |    
|   3 |    PARTITION LIST SINGLE             |         |    
|   4 |     TABLE ACCESS BY LOCAL INDEX ROWID| T       |    
|   5 |      INDEX RANGE SCAN                | I       |    
|   6 |    PARTITION LIST SINGLE             |         |    
|   7 |     TABLE ACCESS FULL                | T       |    
--------------------------------------------------------


we did get an error once but after that no such error occurred

到底是什么错误?

If we rebuild online, does the table lock happen?

数据库在重建的开始和结束时仍然会取出锁,但不会取出独占锁。理查德·富特 (Richard Foote) 进一步讨论了这一点:

https://richardfoote.wordpress.com/2008/02/11/index-create-and-rebuild-locking-improvements-in-11g-ch-ch-ch-changes/
文章转载自ASKTOM,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论