表分区是一种思想,分区表示一种技术实现。当表的大小过G的时候可以考虑进行表分区,提高查询效率,均衡IO。oracle分区表是oracle数据库提供的一种表分区的实现形式。表进行分区后,逻辑上仍然是一张表,原来的查询SQL同样生效,同时可以采用使用分区查询来优化SQL查询效率,不至于每次都扫描整个表。
根据年: INTERVAL(NUMTOYMINTERVAL(1,'YEAR'))
根据月: INTERVAL(NUMTOYMINTERVAL(1,'MONTH'))
根据天: INTERVAL(NUMTODSINTERVAL(1,'DAY'))
根据时分秒: NUMTODSINTERVAL( n, { 'DAY'|'HOUR'|'MINUTE'|'SECOND'})
此时已经有普通表了,我创建了含相同字段的分区表,把数据导入到分区表中,再把原表删掉。
--Create table(WMS_OPERATION_RECORD)
create tableDPHOMEWMS.WMS_OPERATION_RECORD_TMP
(
IDNUMBER(19) not null,
WAREHOUSE_IDNUMBER(19),
ASN_IDNUMBER(19),
PICK_TICKET_IDNUMBER(19),
RELATION_CODEVARCHAR2(50),
OPERATION_TYPEVARCHAR2(50),
OPERATION_IDNUMBER(19),
OPERATION_NAMEVARCHAR2(50 CHAR),
OPERATION_TIMETIMESTAMP(6) DEFAULT systimestamp not null,
STATUSVARCHAR2(10),
TYPEVARCHAR2(10),
COUNTSNUMBER(10)
)tablespace DPHOMEWMS_DATA
PARTITIONBY RANGE (OPERATION_TIME) interval (numtoyminterval(1, 'month'))
STOREIN(DPHOMEWMS_DATA)
(
partition OPERATION_RECORD_P01values less than (TIMESTAMP'2017-05-08 00:00:00')
tablespace DPHOMEWMS_DATA
);--Add comments to the columns
comment on columnDPHOMEWMS.WMS_OPERATION_RECORD_TMP.WAREHOUSE_IDis '仓库id';
commenton columnDPHOMEWMS.WMS_OPERATION_RECORD_TMP.ASN_IDis '收货单id';
commenton columnDPHOMEWMS.WMS_OPERATION_RECORD_TMP.PICK_TICKET_IDis '发货单ID';
commenton columnDPHOMEWMS.WMS_OPERATION_RECORD_TMP.RELATION_CODEis '关联单据号(收货单或发货单的code)';
commenton columnDPHOMEWMS.WMS_OPERATION_RECORD_TMP.OPERATION_TYPEis '操作类型';
commenton columnDPHOMEWMS.WMS_OPERATION_RECORD_TMP.OPERATION_IDis '操作人ID';
commenton columnDPHOMEWMS.WMS_OPERATION_RECORD_TMP.OPERATION_NAMEis '操作人名称';
commenton columnDPHOMEWMS.WMS_OPERATION_RECORD_TMP.OPERATION_TIMEis '操作时间';
commenton columnDPHOMEWMS.WMS_OPERATION_RECORD_TMP.STATUSis '状态';
commenton columnDPHOMEWMS.WMS_OPERATION_RECORD_TMP.TYPEis '明细或者统计次数类型';
commenton columnDPHOMEWMS.WMS_OPERATION_RECORD_TMP.COUNTSis '统计次数';--创建本地索引
create index IDX_WMS_OPERATION_RECORD onDPHOMEWMS.WMS_OPERATION_RECORD_TMP(RELATION_CODE)
tablespace DPHOMEWMS_INDEX local;update DPHOMEWMS.WMS_OPERATION_RECORD set OPERATION_TIME = sysdate where OPERATION_TIME is null;insert into DPHOMEWMS.WMS_OPERATION_RECORD_TMP select * fromWMS_OPERATION_RECORD;drop tableDPHOMEWMS.WMS_OPERATION_RECORD;alter table DPHOMEWMS.WMS_OPERATION_RECORD_TMP rename toWMS_OPERATION_RECORD;
alter table DPHOMEWMS.WMS_OPERATION_RECORD enable row movement;
(1)OPERATION_TIME是分区键,每一个月会自动创建一个分区,分区键不允许为null。
(2)alter table table_name enable row movement; 是指允许分区表的分区键是可更新,当某一行更新时,如果更新的是分区列,并且更新后的列值不属于原来的这个分区,如果开启了这个选项,就会把这行从这个分区中delete掉,并加到更新后所属的分区,此时就会发生rowid的改变。相当于一个隐式的delete+insert,但是不会触发insert/delete触发器。如果没有开启这个选项,就会在更新时报错。