創建和使用分區的表

    在ORACLE里特別大的表,可以使用分區表提高性能。
    Partition有基於範圍、哈希、綜和三種類型。用的比較多的是按範圍分區的表。

1.查看 v$option視圖,如果其中Partitionting為TRUE,則支援分區功能;否則不支持。
  
   
2.創建獨立的表空間(大小可以根據資料量的多少而定)
create tablespace emp_2003q3 datafile 'D:\tools\oracle\oradata\test\emp_2003q4.dbf' size 50M default storage (initial 100k next 100k minextents 1 maxextents unlimited pctincrease 1);
create tablespace emp_2004q1 datafile 'D:\tools\oracle\oradata\test\emp_2004q1.dbf' size 50M default storage (initial 100k next 100k minextents 1 maxextents unlimited pctincrease 1);
create tablespace emp_2004q2 datafile 'D:\tools\oracle\oradata\test\emp_2004q2.dbf' size 50M default storage (initial 100k next 100k minextents 1 maxextents unlimited pctincrease 1);

3.用EXPORT把舊資料備份在emp.dmp中

4.把原來的emp表改名
alter table emp rename to empold;

5.以emp表用戶test創建分區的表(分區的名稱可以和表空間的名稱不一致。這裏是每個季度做一個分區)
create table emp(
 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 emp_2003q4 values less than (to_date('2004-01-01','yyyy-mm-dd'))
    tablespace emp_2003q4
      storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0),
   partition emp_2004q1 values less than (to_date('2004-04-01','yyyy-mm-dd'))
    tablespace emp_2004q1
      storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0),
   partition emp_2004q2 values less than (to_date('2004-07-01','yyyy-mm-dd'))
    tablespace emp_2004q2
      storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0)
   );

6.IMPORT導入資料,參數ignore=y

7、分區表的擴容:

到了2004 年下半年,建立新的表空間:
create tablespace emp_2004q3 datafile 'D:\tools\oracle\oradata\test\emp_2004q2.dbf' size 50m default storage (initial 100k next 100k minextents 1 maxextents unlimited pctincrease 1);

為表添加新分區和表空間:
alter table emp add partition emp_2004q3
values less than (to_date('2004-10-01','yyyy-mm-dd')
tablespace emp_2004q3
storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0);

8.刪除不必要的分區

將2003年第四季度的資料備份,EXPORT 分區:
exp test/test tables=emp:emp_2003q4 rows=Y file=emp_2003q4.dmp

將2003年第四季度的分區刪除。
alter table emp drop partion emp_2003q4;

刪除物理檔
%rm D:toolsoracleoradatatestemp_2003q4.dbf


9.恢復刪除的分區:
例如在2004 年,用戶要查看2003年第四季度的資料.

先創建表空間
create tablespace emp_2003q4 datafile 'D:\tools\oracle\oradata\test\emp_2003q4.dbf' size 50m default storage (initial 100k next 100k minextents 1 maxextents unlimited pctincrease 1);

為表添加新分區和表空間:
alter table emp add partition emp_2003q4
values less than (to_date('2004-01-01','yyyy-mm-dd')
tablespace emp_2003q4
storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0);

導入數據
%imp test/test file=emp_2003q4.dmp tables=(emp:emp_2003q4) ignore=y
(說明:如果不指明導入的分區,imp會自動按分區定義的範圍裝載資料)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/110321/viewspace-612478/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/110321/viewspace-612478/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值