以下转自:http://blog.chinaunix.net/uid-23284114-id-3421922.html 作者:十字螺丝钉
操作分区表对global和local索引的影响 (三)请参考 http://bfc99.blog.51cto.com/265386/1436445
八、COALESCE PARTITION (收缩表分区)
仅能应用在hash 分区表,执行一次就少一个分区,如一个hash分区表含有3个分区,COALESCE PARTITION 一次就成了2个分区。
create table part_hash(id number,name nvarchar2(20))
partition by hash(id)
(
partition p1,
partition p2,
partition p3,
partition p4
);
create index ind_hash_id on part_hash(id);
create index ind_hash_name on part_hash(name) local;
insert into part_hash values(1,'tom');
insert into part_hash values(2,'lucy');
insert into part_hash values(3,'john');
insert into part_hash values(4,'kate');
索引信息
SQL> select INDEX_NAME,TABLE_OWNER,STATUS from dba_indexes where index_name='IND_HASH_ID';
INDEX_NAME TABLE_OWNER STATUS
------------------------------ ------------------------------ --------
IND_HASH_ID DOWNLOAD VALID
SQL> select INDEX_NAME,PARTITION_NAME,HIGH_VALUE,STATUS from dba_ind_partitions where INDEX_NAME='IND_HASH_NAME';
INDEX_NAME PARTITION_NAME HIGH_VALUE STATUS
------------------------------ ------------------------------ -------------------------------------------------------------------------------- --------
IND_HASH_NAME P1 USABLE
IND_HASH_NAME P2 USABLE
IND_HASH_NAME P3 USABLE
IND_HASH_NAME P4 USABLE
表信息
SQL> select table_name,partition_name,HIGH_VALUE from dba_tab_partitions where table_name='PART_HASH' order by table_name,partition_name;
TABLE_NAME PARTITION_NAME HIGH_VALUE
------------------------------ ------------------------------ --------------------------------------------------------------------------------
PART_HASH P1
PART_HASH P2
PART_HASH P3
PART_HASH P4
1.下面进行coalesce partiton操作,不加update index
SQL> alter table PART_HASH coalesce partition;
Table altered.
操作引起global和local索引失效。
SQL> select INDEX_NAME,TABLE_OWNER,STATUS from dba_indexes where index_name='IND_HASH_ID';
INDEX_NAME TABLE_OWNER STATUS
------------------------------ ------------------------------ --------
IND_HASH_ID DOWNLOAD UNUSABLE
SQL> select INDEX_NAME,PARTITION_NAME,HIGH_VALUE,STATUS from dba_ind_partitions where INDEX_NAME='IND_HASH_NAME';
INDEX_NAME PARTITION_NAME HIGH_VALUE STATUS
------------------------------ ------------------------------ -------------------------------------------------------------------------------- --------
IND_HASH_NAME P1 USABLE
IND_HASH_NAME P2 UNUSABLE
IND_HASH_NAME P3 USABLE
修复索引
alter index IND_HASH_ID rebuild;
alter index IND_HASH_NAME rebuild partition p2;
2.加update indexes
SQL> alter table PART_HASH coalesce partition update indexes;
Table altered.
操作后,索引没有失效。
SQL> select INDEX_NAME,TABLE_OWNER,STATUS from dba_indexes where index_name='IND_HASH_ID';
INDEX_NAME TABLE_OWNER STATUS
------------------------------ ------------------------------ --------
IND_HASH_ID DOWNLOAD VALID
SQL> select INDEX_NAME,PARTITION_NAME,HIGH_VALUE,STATUS from dba_ind_partitions where INDEX_NAME='IND_HASH_NAME';
INDEX_NAME PARTITION_NAME HIGH_VALUE STATUS
------------------------------ ------------------------------ -------------------------------------------------------------------------------- --------
IND_HASH_NAME P1 USABLE
IND_HASH_NAME P2 USABLE
九、exchange partition
exchange partition是表与表,表与分区,分区与分区数据交换的命令,不是将表转换成分区,或者分区转换成表
依然用上面的part_test分区表
SQL> select * from PART_TEST partition(p6);
ID NAME
---------- ----------
18 ok
21 john
21 jodan
新建一个表exchange_test
create table exchange_test(id number,name varchar2(10));
下面把PART_TEST partition(p6)中的数据exchange到exchange_test中
SQL> alter table PART_TEST exchange partition p6 with table exchange_test;
Table altered.
p6分区没有数据了
SQL> select * from PART_TEST partition(p6);
no rows selected
数据跑到了exchange_test中。
SQL> select * from exchange_test;
ID NAME
---------- ----------
18 ok
21 john
21 jodan
看分区表索引情况,global索引失效,p6上的local索引失效。
SQL> select INDEX_NAME,TABLE_OWNER,STATUS from dba_indexes where index_name='IND_ID';
INDEX_NAME TABLE_OWNER STATUS
------------------------------ ------------------------------ --------
IND_ID DOWNLOAD UNUSABLE
SQL> select INDEX_NAME,PARTITION_NAME,HIGH_VALUE,STATUS from dba_ind_partitions where INDEX_NAME='IND_NAME';
INDEX_NAME PARTITION_NAME HIGH_VALUE STATUS
------------------------------ ------------------------------ -------------------------------------------------------------------------------- --------
IND_NAME P1 5 USABLE
IND_NAME P3 15 USABLE
IND_NAME P6 25 UNUSABLE
IND_NAME MAX_PART MAXVALUE USABLE
恢复索引
alter index IND_ID rebuild;
alter index IND_NAME rebuild partition p6;
2.加update indexes
SQL> alter table PART_TEST exchange partition p6 with table exchange_test update indexes;
数据又回去了
SQL> select * from exchange_test;
no rows selected
SQL> select * from PART_TEST partition(p6);
ID NAME
---------- ----------
18 ok
21 john
21 jodan
看索引情况
global索引正常
SQL> select INDEX_NAME,TABLE_OWNER,STATUS from dba_indexes where index_name='IND_ID';
INDEX_NAME TABLE_OWNER STATUS
------------------------------ ------------------------------ --------
IND_ID DOWNLOAD VALID
local索引还是失效了。
SQL> select INDEX_NAME,PARTITION_NAME,HIGH_VALUE,STATUS from dba_ind_partitions where INDEX_NAME='IND_NAME';
INDEX_NAME PARTITION_NAME HIGH_VALUE STATUS
------------------------------ ------------------------------ -------------------------------------------------------------------------------- --------
IND_NAME P1 5 USABLE
IND_NAME P3 15 USABLE
IND_NAME P6 25 UNUSABLE
IND_NAME MAX_PART MAXVALUE USABLE
小结:加上update indexes,global索引不失效,但是local 索引依然失效。
十、重命名分区和local索引名字都不会影响索引状态
SQL> alter table part_test rename partition P6 to p5;
SQL> alter index IND_NAME rename partition P6 to p5;
注意:
local索引不能整个rebuild,要rebuild索引分区。
SQL> alter index IND_NAME rebuild;
alter index IND_NAME rebuild
*
ERROR at line 1:
ORA-14086: a partitioned index may not be rebuilt as a whole
SQL> alter index IND_NAME rebuild partition MAX_PART;
Index altered.
如果local索引大面积不可用,那么用动态sql生成重语句吧
SQL> select 'alter index INDEX_NAME rebuild partition '||PARTITION_NAME||';' from user_ind_partitions where INDEX_NAME='INDEX_NAME' and STATUS='UNUSABLE';