在ORACLE裏如果遇到特別大的表,可以使用分區的表來改變其應用程序的性能。
以system身份登陸數據庫,查看 v$option視圖,如果其中Partition爲TRUE,則支持分區功能;否則不支持。Partition有基於範圍、哈希、綜和三種類型。我們用的比較多的是按範圍分區的表。
我們以一個2001年開始使用的留言版做例子講述分區表的創建和使用:
1 、以system 身份創建獨立的表空間(大小可以根據數據量的多少而定)
create tablespace g_2000q4 datafile '/home/oradata/oradata/test/g_2000q4.dbf' size 50M default storage (initial 100k next 100k minextents 1 maxextents unlimited pctincrease 1);
create tablespace g_2001q1 datafile '/home/oradata/oradata/test/g_2001q1.dbf' size 50M default storage (initial 100k next 100k minextents 1 maxextents unlimited pctincrease 1);
create tablespace g_2001q2 datafile '/home/oradata/oradata/test/g_2001q2.dbf' size 50M default storage (initial 100k next 100k minextents 1 maxextents unlimited pctincrease 1);
2 、用EXPORT工具把舊數據備份在guestbook.dmp中
把原來的guestbook表改名
alter table guestbook rename to guestbookold;
以guestbook 身份創建分區的表
create table guestbook(
id number(16) primary key,
username varchar2(64),
sex varchar2(2),
email varchar2(256),
expression varchar2(128),
content varchar2(4000),
time date,
ip varchar2(64)
)
partition by range (time)
(partition g_2000q4 values less than (to_date('2001-01-01','yyyy-mm-dd'))
tablespace g_2000q4
storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0),
partition g_2001q1 values less than (to_date('2001-04-01','yyyy-mm-dd'))
tablespace g_2001q1
storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0),
partition g_2001q2 values less than (to_date('2001-07-01','yyyy-mm-dd'))
tablespace g_2001q2
storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0)
);
(說明:分區的名稱可以和表空間的名稱不一致。這裏是每個季度做一個分區,當然也可以每個月做一個分區)
3、IMPORT導入數據,參數ignore=y
4、分區表的擴容:
到了2001 年下半年,建立新的表空間:
create tablespace g_2001q3 datafile '/home/oradata/oradata/test/g_2001q3.dbf' size 50m default storage (initial 100k next 100k minextents 1 maxextents unlimited pctincrease 1);
爲表添加新分區和表空間:
alter table guestbook add partition g_2001q3
values less than (to_date('2001-10-01','yyyy-mm-dd')
tablespace g_2001q3
storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0);
5、刪除不必要的分區
將2000年的數據備份(備份方法見 6、EXPORT 分區),將2000年的分區刪除。
alter table guestbook drop partion g_2000q4;
刪除物理文件
%rm /home/oradata/oradata/test/g_2000q4.dbf
6、EXPORT 分區:
% exp guestbook/guestbook_password tables=guestbook:g_2000q4 rows=Y file=g_2000q4.dmp
7、IMPORT分區:
例如在2001 年,用戶要查看2000 年的數據,先創建表空間
create tablespace g_2000q4 datafile '/home/oradata/oradata/test/g_2000q4.dbf' size 50m default storage (initial 100k next 100k minextents 1 maxextents unlimited pctincrease 1);
爲表添加新分區和表空間:
alter table guestbook add partition g_2000q4
values less than (to_date('2001-01-01','yyyy-mm-dd')
tablespace g_2001q3
storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0);
導入數據
%imp guestbook/guestbook_password file=g_2000q4.dmp tables=(guestbook:g_2000q4) ignore=y
(說明:如果不指明導入的分區,imp會自動按分區定義的範圍裝載數據)
引用地址:http://publish.it168.com/2005/0820/20050820019701.shtml