最近有一個需求,隨著業(yè)務(wù)的不斷發(fā)展侣颂,每天備份的dmp越來越大档桃,占用了很多的磁盤空間。
看了下備份的log憔晒,發(fā)現(xiàn)排在第一個的居然是一個日志log表藻肄,記錄系統(tǒng)中的每一步操作。這個表只是單純做查詢使用拒担,而且使用頻率也極低嘹屯,除非為了查詢某些特殊操作是何人所為的情況。數(shù)據(jù)記錄一旦形成就不會在改變从撼。既然如此州弟,重復(fù)的備份該表的歷史部分也就沒有多大意義。
于是考慮將該表改為分區(qū)表谋逻,設(shè)置2個分區(qū),一個分區(qū)存儲最近三個月的數(shù)據(jù)桐经,另一個分區(qū)存儲三個月以前的數(shù)據(jù)毁兆。并將歷史數(shù)據(jù)存入另一個表空間,將表空間設(shè)置為只讀阴挣。
開干:
- 新建一個表空間olddata气堕,用于存儲歷史的不再改變的數(shù)據(jù)。
- 在業(yè)務(wù)停止的時候畔咧,創(chuàng)建一個分區(qū)表茎芭。
create table C_MONITORLOG_TMP
(
logid ,
qname ,
lx ,
category ,
type ,
lasttime ,
datasize ,
param ,
name ,
created default sysdate ,
createby ,
updated default sysdate ,
updateby ,
orgid ,
note
)
partition by range (CREATED)
(
partition P1 values less than (TO_DATE('2017-07-31 00:00:00', 'YYYY-MM-DD HH24:MI:SS' ))
tablespace OLDDATA,
partition PMAX values less than (MAXVALUE)
tablespace USERS
)
as
select * from C_MONITORLOG;
- 確認數(shù)據(jù)無誤后,將C_MONITORLOG drop掉誓沸。
- 修改 C_MONITORLOG_TMP 表名為 C_MONITORLOG梅桩。
- 備份oldata表空間數(shù)據(jù),將C_MONITORLOG:P1 數(shù)據(jù)進行一次單獨備份拜隧。
expdp system/xxxxxx directory=expdpdir dumpfile=c_monitorlog_p1.dmp tables=longyyy.C_MONITORLOG:P1
這個備份需要保存好宿百,以后不會再每天對這個分區(qū)進行備份了趁仙。
如果對分區(qū)數(shù)據(jù)進行了修改和合并,則必須重新備份該分區(qū)垦页。
編輯dmp導(dǎo)出腳本雀费,增加過濾選項將p1的數(shù)據(jù)分區(qū)數(shù)據(jù)過濾掉。
expdp system/xxxxxx directory=expdpdir dumpfile=test.dmp exclude=table:\"in\(\'C_MONITORLOG:P1\'\)\"
然而exclude 并不能對表的分區(qū)進行排除痊焊。如果這么寫盏袄,還是會將C_MONITORLOG進行全表導(dǎo)出。
怎么辦呢薄啥,應(yīng)該使用exclude=table_data 來對表分區(qū)進行排除辕羽。
expdp system/xxxxxx directory=expdpdir dumpfile=test.dmp tablespaces=users exclude=table_data:\"in (select partition_name from dba_tab_partitions where table_name=\'C_MONITORLOG\' and partition_name<>\'PMAX\')\"
通過上面的語句,就可以將C_MONITORLOG表中除PMAX的分區(qū)全部排除掉罪佳。
最后逛漫,每三個月對PMAX分區(qū)的數(shù)據(jù)進行一次切割,然后合并到P1分區(qū)里面赘艳。這樣酌毡,我們每次備份到處的dmp就不需要保存那么多重復(fù)不變的歷史日志了。
--查看表的分區(qū)
select * from dba_tab_partitions
where table_name='C_MONITORLOG';
--分區(qū)拆分
alter table C_MONITORLOG split partition PMAX at (to_date('2017/10/01','yyyy/mm/dd'))
into (partition P2 tablespace olddata, partition PMAX);
--分區(qū)合并
--底部分區(qū)不能作為新的合并分區(qū)蕾管。
alter table C_MONITORLOG merge partitions p1,p2 into partition p2;
--分區(qū)改名
alter table C_MONITORLOG rename partition p2 to p1;
--分區(qū)移動枷踏,可以在不同表空間之間移動。
alter table C_MONITORLOG move partition p1 tablespace olddata;
最后掰曾,將olddata表空間設(shè)置為read only旭蠕,直到下次需要操作時再打開read write
alter tablespace olddata read only;
alter tablespace olddata read write;