rebuild index 和 recreate index (重新建立index) 的區別

tolywang發表於2007-07-16

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具有下列特徵:
新索引在建立的時候使用已存的索引作為資料來源。
使用已存的索引建立新索引不需要排序操作,從而提高效能。
新索引建立後就會刪除舊的索引. 在索引重建期間,在兩個不同的表空間上需要足夠的空間來同時放置新舊兩個索引,
新的索引不會包含有任何已經刪除了的行的條目,從而索引空間具有更好的利用率。
在新索引建立期間,查詢語句可以繼續使用以前的索引 。

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

重新建立索引是drop index ,釋放空間, 然後重新create . 當然也需要排序。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/35489/viewspace-84792/,如需轉載,請註明出處,否則將追究法律責任。

相關文章