分区原则
1.表分区的指南
a、表的大小
对于大表进行分区,将有益于大表操作的性能和大表的数据维护。通常当表的大小超过1.5GB-2GB,或对于OLTP系统,表的记录超过1000万,都应考虑对表进行分区。
b、数据访问特性
基于表的大部分查询应用,只访问表中少量的数据。对于这样表进行分区,可充分利用分区排除无关数据查询的特性。
c、数据维护
某些表的数据维护,经常按时间段删除成批的数据,例如按月删除历史数据。对于这样的表需要考虑进行分区,以满足维护的需要。因为删除(Delete)大量的数据,对系统开销很大,有时甚至是不可接受的。
d、只读数据
如果一个表中大部分数据都是只读数据,通过对表进行分区,可将只读数据存储在只读表空间中,对于数据库的备份是非常有益的。
e、并行数据操作(Parallel DML)
对于经常执行并行操作(如Parallel Insert,Parallel Update等)的表应考虑进行分区。
f、表的可用性
当对表的部分数据可用性要求很高时,应考虑进行表分区。
2.选择分区字段(Partition Key)
当确定分区字段时,有两个主要因素特别需要考虑:
a、增强表的管理和维护性
通过Partition Key,可以使数据维护基于某个分区进行,如Drop或Truncate一个或多个分区。通过Paratition Key可控制只读的数据存储在相应的分区中,且这些分区存储在只读的表空间里,这将提高数据备份的性能。这类Partition Key通常与时间相关。
b、提高访问表的性能
通过Partition Key,可使查询的数据定位在一个或少量的分区中;这需要考虑最常用的查询条件。注意在考虑提高查询效率这个因素的同时,还应兼顾数据维护管理的因素,尽可能地避免相互间地冲突。
CREATE TABLESPACE TEST1 DATAFILE 'C:\TEMP\TEST1.DBF' SIZE 10M;
CREATE TABLESPACE TEST2 DATAFILE 'C:\TEMP\TEST2.DBF' SIZE 10M;
CREATE TABLESPACE TEST3 DATAFILE 'C:\TEMP\TEST3.DBF' SIZE 10M;
Create table login
(login_id number primary key,
login_name varchar2(50) not null,
login_pass varchar2(20) not null,
login_date date not null)
Partition by range(login_date)
(partition part_01 values less than(to_date('2006-01-01','yyyy-mm-dd')) tablespace test1,
partition part_02 values less than(to_date('2010-01-01','yyyy-mm-dd')) tablespace test2,
partition part_03 values less than(maxvalue) tablespace test3)
insert into login values(1,'zhangsan','111',sysdate);
insert into login values(2,'lisi', '111',sysdate+30);
insert into login values(3,'wangwu', '111',to_date('2006-05-30','yyyy-mm-dd'));
insert into login values(4,'zhaoliu', '111',to_date('2007-06-23','yyyy-mm-dd'));
insert into login values(5,'lily', '111',to_date('2011-02-26','yyyy-mm-dd'));
insert into login values(6,'lucy', '111',to_date('201
1-04-30','yyyy-mm-dd'));
commit;
select * from login partition(part_01);
select * from login partition(part_02);
select * from login partition(part_03);
select * from login;
-- Create table(创建分区表)
create table BILL_MONTHFEE_ZERO
(
SERV_ID NUMBER(20) not null,
BILLING_CYCLE_MONTH NUMBER(6) not null,
DATE_TYPE NUMBER(1),
ACC_NBR VARCHAR2(80)
)
partition by range (BILLING_CYCLE_MONTH)
(partition p_200407 values less than (200407)
tablespace TS_ZIKEN
storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0),
partition p_200408 values less than (200408)
tablespace TS_ZIKEN
storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0))
;
create index idx_bill_monthfee_zero_idx01 on bill_monthfee_zero(billing_cycle_month)
tablespace TS_ZIKEN_idx
storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0) nologging;
--增加分区表
alter table BILL_MONTHFEE_ZERO add Partition p_200409
values less than (200409) tablespace ts_ziken;
--删除一分区
alter table part_tbl drop Partition part_tbl_08;
--将一个分区分为两个分区
alter table bill_monthfee_zero split Partition p_200409 at (200409)
into (Partition p_200409_1 tablespace ts_ziken,
Partition p_200409_2 tablespace ts_ziken_idx);
--合并分区
ALTER TABLE bill_monthfee_zero
MERGE PARTITIONS p_200408, p_200409 INTO PARTITION p_all
--将分区改名
alter table bill_monthfee_zero rename Partition p_200408 to p_fee_200408
--将分区改表空间
alter table bill_monthfee_zero move Partition p_200409
tablespace ts_ziken_01 nologging
--查询特定分区
select count(*) from BILL_MONTHFEE_ZERO partition (p_200407);
--添加数据
insert into bill_monthfee_zero select * from bill_monthfee_zero partition (p_200407)
--分区表的导出
userid=dxsq/teledoone@jndxsq154
buffer=102400
tables=bill_monthfee:P_200401,
file=E:\exp_para\exp_dxsq_tables.dmp
log=E:\exp_para\exp_dxsq_tables.log
技巧:
删除表中一个字段:
alter table bill_monthfee_zero set unused column date_type;
添加一个字段:alter table bill_monthfee_zero add date_type number(1);