從10g開始,Oracle開始提供Shrink的命令,假如我們的資料表空間中支援自動段空間管理 (ASSM),就可以使用這個特性縮小段,即降低HWM。這裡需要強調一點,10g的這個新特性,僅對ASSM資料表空間有效,否則會報 ORA-10635: Invalid segment or tablespace type。
如果經常在表上執行DML操作,會造成資料庫塊中資料分布稀疏,浪費大量空間。同時也會影響全表掃描的效能,因為全表掃描需要訪問更多的資料區塊。從oracle10g開始,表可以通過shrink來重組資料使資料分布更緊密,同時降低HWM釋放空閑資料區塊。
segment shrink分為兩個階段:
1、資料重組(compact):通過一系列insert、delete操作,將資料盡量排列在段的前面。www.bkjia.com在這個過程中需要在表上加RX鎖,即只在需要移動的行上加鎖。由於涉及到rowid的改變,需要enable row movement.同時要disable基於rowid的trigger.這一過程對業務影響比較小。
2、HWM調整:第二階段是調整HWM位置,釋放空閑資料區塊。此過程需要在表上加X鎖,會造成表上的所有DML語句阻塞。在業務特別繁忙的系統上可能造成比較大的影響。
shrink space語句兩個階段都執行。
shrink space compact只執行第一個階段。
如果系統業務比較繁忙,可以先執行shrink space compact重組資料,然後在業務不忙的時候再執行shrink space降低HWM釋放空閑資料區塊。
shrink必須開啟行遷移功能。
alter table table_name enable row movement ;
注意:alter table XXX enable row movement語句會造成參考資料表XXX的對象(如預存程序、包、視圖等)變為無效。www.bkjia.com執行完成後,最好執行一下utlrp.sql來編譯無效的對象。
============================================================================================
utlrp.sql and utlprp.sql
The utlrp.sql and utlprp.sql scripts are provided by Oracle to recompile all invalid objects in the database. They are typically run after major database changes such as upgrades or patches. They are located in the $ORACLE_HOME/rdbms/admin directory and provide a wrapper on the UTL_RECOMP package. The utlrp.sql script simply calls the utlprp.sql script with a command line parameter of "0". The utlprp.sql accepts a single integer parameter that indicates the level of parallelism as follows:
0 - The level of parallelism is derived based on the CPU_COUNT parameter.
1 - The recompilation is run serially, one object at a time.
N - The recompilation is run in parallel with "N" number of threads.
Both scripts must be run as the SYS user, or another user with SYSDBA, to work correctly.
============================================================================================