rebuild index & rebuild index online [final]

Rebuild index :

rebuild时也会使用临时表空间
ask tom上关于rebuild index 有这么一段话:
If you need to rebuild your indexes, you need 2x the space -- you'll have the old and the new index for a period of time. If you do it online, you'll need additional space to hold the changes that are made during the rebuild as well

2×的空间是当前index所在的tablespace中使用空间


以前有人问过类似的问题

rebuild index为何要排序

我们都知道索引是有序的存储
然而在block内部,实际上索引键值的存储是无序的

比如说,你先存入了1,3
即使以后增加了一个2
那么在同一个数据块内部,数据库也不会去动1,3的存储
在读取的时候,oracle可以作简单的块内排序,进行有序的读取输出

在重建索引的时候
Oracle显然不会按照1,2,3..........的索引顺序来读出索引内容
因其代价高昂

Oracle实际执行的是Fast Full Scan
按顺序读取block

这样读取出来的数据需要重新sort
排序,然后重构索引

这个重构的索引在物理存储上比原来更为有序

这个可以通过block dump观察到

建立索引实际上是排序,大型的排序(只要sort area容纳不下)就要使用临时表空间

我作了两个试验
我的index大小为7M左右。
首先,我将index所在的tablespace只开到10M,临时表空间较大,
rebuild 时报无法在index所在的tablespace上扩展temp 段。
将表空间增大,rebuild成功。(至少要开到20M)
在将临时表空间只开到5M,表空间开到20M,rebuild就报无法在temp表空间上扩展(这个是因为需要排序,而sort area容不下,所以要利用临时表空间),将临时表空间增大到10M,rebuild成功。


1.索引的存储是,块内无序,块间有序
2.rebuild index时对于索引块的读取也是Fast full scan
3.不是说读索引的代价高,是说顺序读取块内无序的索引的代价高
所以会采用Fast Full Scan方式读取

ALTER INDEX REBUILD is usually faster than dropping and re-creating an index because it uses the fast full scan feature. It thus reads all the index blocks, using multi-block I/O, then discards the branch blocks. The Oracle database allows the creation of an index or the re-creation of an existing index while allowing concurrent operations on the base table. This helps achieve demanding availability goals by allowing maintenance operations to be performed online with no down time.

索引的rebuild具有下列特征: 
新索引在建立的时候使用已存的索引作为数据源(不一定,也有可能源头是表,见下面分析)。
使用已存的索引建立新索引不需要排序操作(其实都需要排序啦,见下面分析),从而提高性能。
新索引建立后就会删除旧的索引. 在索引重建期间,在两个不同的表空间(也可以是相同表空间啦)上需要足够的空间来同时放置新旧两个索引,
新的索引不会包含有任何已经删除了的行的条目,从而索引空间具有更好的利用率。
在新索引建立期间,查询语句可以继续使用以前的索引,直到新索引建立完成才删除旧的索引 。

alter index xx rebuild online ;   会走 Full Table Scan .

alter index xx rebuild ;   可能走Index Fast Full Scan,取原index数据作为数据源,也有可能取全表扫描即表的数据作为数据源,取决于统计数据,如果索引大小比表大,那么会选择使用全表扫描表中的数据。

走Index Fast Full Scan , 多块读,按照物理block存储的顺序读取而不是index tree创建的逻辑顺序读取, 之所以这样做,是因为Sequential multiblock reads是最快的IO访问方式.    I/O的快速带来的负面影响就是要重新排序

rebuild index  源数据来源于原来的index还是table, 取决于统计信息,假如统计信息显示索引比表还大,那么无疑走全表比走索引快,因此走全表扫描。所以统计信息的完整与否影响到数据来源。

还有 ,区别一下,alter index  XXX  coalesce 是合并索引中相邻的碎片,DMT表空间中会有SMON定期执行合并, 如果是LMT本地表空间管理管理,一般不用做。

========================

重新建立索引是drop index ,释放空间, 然后重新create . 当然也需要排序。

========================  

从阻塞表上的操作来讲 rebuild index 和 drop and create 是一样的,都会阻塞DML操作,rebuild index online不会 ,不过rebuild index 速度要快一些因为它扫描的是旧索引而不是表(如果统计数据显示表比索引大,index fast full scan 会快一些; 但是如果统计数据显示索引比表还大,那么rebuild index会全表扫描,速度和drop and create 差不多 ), 当新索引建立后删除旧索引。 
 
 
========================   
 
还有一个概念容易搞混淆,  index fast full scan  和 index full scan ,rebuild index 要么是full table sacn  , 要么是 index fast full scan 。    
 
当进行index full scan的时候 oracle定位到索引的root block,然后到branch block(如果有的话),再定位到第一个leaf block, 然后根据leaf block的双向链表顺序读取。它所读取的块都是有顺序的,也是经过排序的。
 
而index fast full scan则不同,它是从段头开始,读取包含位图块,root block,所有的branch block, leaf block,读取的顺序完全有物理存储位置决定,并采取多块读,没次读取db_file_multiblock_read_count个块。  index fast full scan  会比index full scan多一步sort order by . 
 
 
 
 
 
tolywang 发表于:2007.07.16 11:56 ::分类: ( Oracle数据库管理 ) ::阅读:(373次) :: 评论 (3) :: 引用 (0)
re: rebuild index 和 recreate index (重新建立index) 的区别 [回复]

Bug4364202   -    Failed ONLINE index rebuild

当我们rebuild index online失败后,smon进程会清除sys_journal_xxx(xxx是object_id)以及其它的不一致的东西,在smon清除它们之前,是不能再进行rebuild index online的,如果要清除它们,需要执行dbms_repare包的online_index_clean过程来处理...

在9206,9207,10.2.0.1中smon不会很快地清除index rebuild online失败所带来的对象,在9208,10.2.0.2,11g中这个bug才被fixed.所以在9206,9207,10.2.0.1这些版本中,当我们对大的索引进行index rebuild online时,不要随意地中断rebuild index online操作,否则smon可能需要很久才能清除临时对象。。。

tolywang 评论于:2007.10.12 08:31
re: rebuild index 和 recreate index (重新建立index) 的区别 [回复]

rebuild index    可以通过查看 dba_objects 中status 是否index有效。

tolywang 评论于:2007.10.12 08:34
re: rebuild index 和 recreate index (重新建立index) 的区别 [回复]

rebuild index 之后一般需要进行索引的分析

tolywang 评论于:2009.05.06 10:30  
 
 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/35489/viewspace-594278/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/35489/viewspace-594278/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值