前几天想起使用move命令,消除表碎片的事情。索引会不会失效。想了想应该会失效。
通过例子测试,使用move后,所以会失效,需要rebuild
测试如下:
创建一个测试表,并插入数据
SYS@test>create table bb_t1 tablespace bb as select * from dba_objects;
Table created.
SYS@test>select count(*) from bb_t1;
COUNT(*)
----------
87211
SYS@test>insert into bb_t1 select * from bb_t1;
174422 rows created.
SYS@test>/
348844 rows created.
SYS@test>/
697688 rows created.
SYS@test>/
1395376 rows created.
SYS@test>/
2790752 rows created.
SYS@test>commit;
创建索引
create index bb.idx_bb_t1 on bb_t1(object_id) tablespace bb;
--
SYS@test>select index_name, tablespace_name ,status ,visibility from dba_indexes where table_name='BB_T1';
INDEX_NAME TABLESPACE_NAME STATUS VISIBILIT
------------------------------ ------------------------------ -------- ---------
IDX_BB_T1 BB VALID VISIBLE
SYS@test>
进行movement
SYS@test>alter table bb_t1 enable row movement;
Table altered.
SYS@test>
SYS@test>alter table bb_t1 move;
Table altered.
SYS@test>
查看索引状态
SYS@test>select index_name, tablespace_name ,status ,visibility from dba_indexes where table_name='BB_T1';
INDEX_NAME TABLESPACE_NAME STATUS VISIBILIT
------------------------------ ------------------------------ -------- ---------
IDX_BB_T1 BB UNUSABLE VISIBLE
rebuild索引
SYS@test>alter index bb.idx_bb_t1 rebuild ;
Index altered.
再次查看索引状态
SYS@test>select index_name, tablespace_name ,status ,visibility from dba_indexes where table_name='BB_T1';
INDEX_NAME TABLESPACE_NAME STATUS VISIBILIT
------------------------------ ------------------------------ -------- ---------
IDX_BB_T1 BB VALID VISIBLE
SYS@test>
测试shrink。shrink完毕后,索引不会失效。
SYS@test>alter table bb_t1 shrink space cascade;
Table altered.
SYS@test>select index_name, tablespace_name ,status ,visibility from dba_indexes where table_name='BB_T1';
INDEX_NAME TABLESPACE_NAME STATUS VISIBILIT
------------------------------ ------------------------------ -------- ---------
IDX_BB_T1 BB VALID VISIBLE
SYS@test>
关闭movement
SYS@test>alter table bb_t1 disable row movement;
Table altered.
SYS@test>select row_movement from dba_tables where table_name='BB_T1'
2 ;
ROW_MOVE
--------
DISABLED
SYS@test>
结论:使用move命令整理碎片,需要rebuild 索引,
使用shrink命令整理碎片,不需要rebuild索引
END