`
desert3
  • 浏览: 2139590 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

oracle rebuild index and table move and rename

阅读更多
Oracle里大量删除记录后,表和索引里占用的数据块空间并没有释放。
table move可以释放已删除记录表占用的数据块空间,整理碎片。如果将表格用move方式整理碎片后,索引将失效,这时需要将索引重建。
重建索引可以释放已删除记录索引占用的数据块空间。重建索引不仅能增加索引表空间空闲空间大小,还能够提高查询性能。
--table move
alter table tbl move; 

--rebuild索引
alter index idx_tbl_col rebuild;
alter index idx_tbl_col rebuild online;

--rename
ALTER INDEX employee_idx RENAME TO employee_index_Newname;

普通情况下建立索引或者rebuild索引时,oracle会对基表加share锁,由于share锁和 row-X是不兼容的,也就是说,在建立索引期间,无法对基表进行DML操作。
是否加online,要看你的系统需求。因为不加online时rebuild会阻塞一切DML操作。
当我们对索引进行rebuild时,如果不加online选项,oracle则直接读取原索引的数据;当我们添加online选项时,oracle是直接扫描表中的数据
索引在重建时,查询仍然可以使用旧索引。实际上,oracle在rebuild时,在创建新索引过程中,并不会删除旧索引,直到新索引rebuild成功。
从这点可以知道rebuild比删除索引然后重建索引的一个好处是不会影响原有的SQL查询,但也正由于此,用rebuild方式建立索引需要相应表空间的空闲空间是删除重建方式的2倍。

可以通过如下的sql看到rebuild前后的索引空间大小
Select Segment_Name, Sum(bytes) / 1024 / 1024/1024 as Gsize
From User_Extents
Group By Segment_Name
order by Gsize desc


不能直接rebuild整个分区索引
对于非组合索引,需要rebuild每个分区(partition),不能直接rebuild整个索引
对于组合索引,需要rebuild每个子分区(subpartition),不能直接rebuild整个索引,也不能直接rebuild分区
使用下面的sql可以生成相应的rebuild语句,注意是否加上online
非组合索引:
select 'alter index ' || index_owner || '.' ||index_name ||' rebuild partition ' || PARTITION_NAME || ' nologging parallel ;'
from dba_ind_partitions
where index_owner = 'USER_NAME'
AND INDEX_NAME = 'idx_tbl_col'


组合索引:
select 'alter index ' || index_owner || '.' ||index_name ||' rebuild subpartition ' || subpartition_name || '  parallel ;'
from dba_ind_subpartitions where index_owner='&index_owner' and index_name='&index_name';


参考rebuild 分区索引
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics