oracle 表碎片太多的處理辦法

liuhaimiao發表於2014-08-20
某張結果表在etl過程中頻繁的被dml語句操作,導致整個表的碎片激增。原來7g多的空間經過一段時間的執行增長至80g。。。。。汗 
透過move的方式來解決該辦法,在網上搜羅了一些操作說明,供參考 
一、碎片程度檢視
在沒有toad的情況下可以透過如下語句:
 select tablespace_name,count(tablespace_name) from dba_free_space group by tablespace_name
having count(tablespace_name)>10;
alter tablespace name coalesce;
alter table name deallocate unused;
create or replace view ts_blocks_v as
select tablespace_name,block_id,bytes,blocks,'free space' segment_name from dba_free_space
union all
select tablespace_name,block_id,bytes,blocks,segment_name from dba_extents;
select * from ts_blocks_v;
select tablespace_name,sum(bytes),max(bytes),count(block_id) from dba_free_space
group by tablespace_name;
二、檢視碎片程度高的表
SELECT segment_name table_name , COUNT(*) extents
FROM dba_segments WHERE owner NOT IN ('SYS', 'SYSTEM') GROUP BY segment_name
HAVING COUNT(*) = (SELECT MAX( COUNT(*) ) FROM dba_segments GROUP BY segment_name);
三、解決方法
If you have to rebuild a table,always consider the 'move' command first.Availability can be reduced,but the benefit to administrative safty and convenience is large.There are no windows of opportunity for data to go missing.----Jonathan Lewis.
如果必須要重建表,alter table ...move tablespace...絕對是第一選擇,理由如下:
1 對於大表move時,對此表的查詢不受影響,只有在move操作完成的瞬間受影響。DML操作受影響。
2 index結構不受影響,只需move完成後rebuild。
3 與其它物件依賴關係不受影響,操作前不必為物件間的依賴關係操心。
4 move操作可以parallel。
5 NOLOGGING選項對move操作有作用,可大大加快重建速度。如果要move的表是nologging的,則不需指定。
基於以上理由,move是rebuild table的最佳選擇,應該優先考慮,大家可以對照以上優點考慮一下用exp/imp的優缺點。採取的策略根據實際需求的不同而不同

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

相關文章