很多數(shù)據(jù)庫(kù)在設(shè)計(jì)時(shí)未能很好的規(guī)劃數(shù)據(jù)庫(kù)的容量, 導(dǎo)致在某些情況下, 導(dǎo)致一個(gè)表中存放了數(shù)百萬(wàn), 數(shù)千萬(wàn)甚至上億條記錄, 維護(hù)起來(lái)非常的不便, 下面介紹一種將非分區(qū)表轉(zhuǎn)換為分區(qū)表的方法.
testtb_nopart是一個(gè)非分區(qū)表, 基本信息如下:
SQL> desc testtb_nopart
Name Null? Type
----------------------------------------- -------- ----------------------------
TIMESTAMP DATE
OWNER VARCHAR2(30)
ACTION VARCHAR2(6)
OBJECT_NAME VARCHAR2(128)
MESSENG VARCHAR2(169)
SQL> select to_char(timestamp, 'mm-yyyy'), count(*) from testtb_nopart group by to_char(timestamp, 'mm-yyyy');
TO_CHAR COUNT(*)
------- ----------
12-2005 2058125
11-2005 1363431
08-2005 150951
10-2005 628149
07-2005 1006340
09-2005 730410
6 rows selected.
創(chuàng)建分區(qū)表testtb_part, 結(jié)構(gòu)和testtb_nopart一致.
create table testtb_part (timestamp date, owner varchar2(30), action varchar2(6), object_name varchar2(128) , messeng varchar2(169))
tablespace testts
partition by range(timestamp)(
partition rest values less than (maxvalue))
交換testtb_nopart和testtb_part中part分區(qū)的數(shù)據(jù)
SQL> alter table testtb_part exchange partition rest with table testtb_nopart;
Table altered.
SQL> select to_char(timestamp, 'mm-yyyy'), count(*) from testtb_nopart group by to_char(timestamp, 'mm-yyyy');
no rows selected
SQL> c/nopart/part/
1* select to_char(timestamp, 'mm-yyyy'), count(*) from testtb_part group by to_char(timestamp, 'mm-yyyy')
SQL> /
TO_CHAR COUNT(*)
------- ----------
12-2005 2058125
11-2005 1363431
08-2005 150951
10-2005 628149
07-2005 1006340
09-2005 730410
6 rows selected.
testtb_nopart中大約有50萬(wàn)條記錄, 占用600m左右空間, 交換大約用了不到30秒鐘, testtb_part和testtb_nopart所在的表空間也沒有明顯增長(zhǎng).
對(duì)rest分區(qū)進(jìn)行拆分
SQL> alter table testtb_part split partition rest at (to_date('200508','yyyymm'))
2 into (partition part08, partition rest);
Table altered.
SQL> alter table testtb_part split partition rest at (to_date('200509','yyyymm'))
2 into (partition part08, partition rest);
Table altered.
SQL> alter table testtb_part split partition rest at (to_date('200510','yyyymm'))
2 into (partition part09, partition rest);
Table altered.
SQL> alter table testtb_part split partition rest at (to_date('200511','yyyymm'))
2 into (partition part10, partition rest);
Table altered.
SQL> alter table testtb_part split partition rest at (to_date('200512','yyyymm'))
2 into (partition part11, partition rest);
Table altered.
SQL> alter table testtb_part split partition rest at (to_date('200601','yyyymm'))
2 into (partition part12, partition rest);
Table altered.
***后可以重命名表, 使得轉(zhuǎn)換過程不影響用戶程序.
alter table testtb_part rename to testtb_nopart