shrink space的最佳實踐

wei-xh發表於2011-07-12

shrink space分兩個階段:

1)透過把segment後面的資料delete,然後insert到segment的前面,這個操作的事務單位比較小,在這個過程中即使把程式取消,也不會有問題下次可以繼續上一次的操作。而不是很多人理解的,取消掉後,會回滾之前的操作。由於這個階段的事務單位比較小,因此取消後,只會回滾此次沒有提交掉的事務。
 During segment shrink, data will be moved as part of the compaction phase. During
compaction locks will be held on individual rows and/or blocks containing the data.
This will cause the concurrent DMLs like updates and deletes to serialize on the
locks. The compaction will be done in units of smaller transactions, so the availability
of the object will not be impacted significantly.

2)第二步就是降低高水位,這個過程要在表上加X鎖,會造成所有表上的DML阻塞。一般這個過程比較短,我們可以在做這個操作前,多執行幾次階段一的操作。這樣能保證這個時間足夠的小。
後記:大表即使第一個操作做了N遍,第二個操作依然會比較久,結合10046會發現,這個過程中以單塊讀的形式讀了資料庫絕大多數的塊。而且降低高水位過程中會獲得表上6型別的鎖,因此會阻塞DML,因此SHRINK操作,也是個風險較大的操作
However during certain phases of segment shrink (when the HWM is adjusted), the segment
will have to be locked in exclusive mode.

由於這個過程中,行的rowid會變化,因此需要開啟行移動alter table table_name enable row movement ;

建議對於大表,生產環境下,都採取兩步操作:
1)alter table table_name shrink space compcat;-------------相當於只執行了階段一,中間可以取消掉,下次重新來過,分多次進行,
2)alter table table_name shrink space;---------------------降低高水位,儘量放到空閒的時候做,這個時候不能取消,否則可能遭遇BUG或字典不一致。

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

相關文章