搜索分区下的数据:
SELECT * FROM table PARTITION(tablespace);
创建表分区
create table lico_user
(
id integer primary key,
username varchar2(20),
password varchar2(20),
create_date date
)
partition by range(create_date)
(
partiton user_01 values less than(to_date(‘2016-09-01’,’yyyy-mm-dd’)) tablespace tbs_lico_1,
partiton user_02 values less than(to_date(‘2016-10-01’,’yyyy-mm-dd’)) tablespace tbs_lico_1,
partiton 表分区名 values less than(to_date(‘2016-10-01’,’yyyy-mm-dd’)) tablespace 表空间名,
);
注意事项:
partition by 分区类型(分区键)
- 范围分区:range
可以确定键值指定范围进行分布,当数据在范围内均匀分布时,性能最好,如:日期
需要考虑数据值的取值范围。 - 散列分区:hash
用户在列难以确定的情况下采用的分区方法,如:按照身份证号分区 - 列表分区:list
如果表的某个值可以枚举,则可以考虑对表进行列表分区,如:省份 - 组合分区
结合两个数据分区的方法可以成为一个组合分区方法。首先用第一个数据分区方法对表格进行分区,然后在用第二个 数据分区方法对每个分区进行分区。 - interval分区
范围分区的一种增强功能,可以实现 equi_sized范围自动化。创建的分区作为元数据,只有最开始的分区是永久分区随着数据的增加会自动创建分区和本地索引。
例:
partition by range(create_date)
interval(numtoyminterval (1,'month’))
VALUES LESS THAN 数值小于等于,日期是小于
添加表分区
对于已经存在表分区的表添加表分区。
ALTER TABLE LICO_USER //表名
ADD PARTITION USER_05 VALUES LESS THAN(TO_DATE('2016-12-01','YYYY-MM-DD'))
STORAGE (INITIAL 20M NEXT 10M) TABLESPACE TBS_LICO_1 //表分区语句
NOLOGGING;
合并表分区
减少分区的个数,合并后oracle做以下处理。
a.在合并分区时,hash列函数将分区内容分布到一个或多个保留分区中。
b.原来内容所在的分区完全被清除
c.与分区对应的索引也被清除。
d.将一个或多个索引的本地索引分区标识为不可用(UNSABLE)
f.需要将不可用索引进行重建
- 合并复合分区
合并user分区表中的一个HASH分区,代码及运行如下。
alter table user coalesce partition;
- 合并复合分区
把user分区表中的user_tps2分区合并到其他保留的自分区中
alter table user modify partition par3 cocalesce subpartition;
删除表分区
可用从范围分区和复合分区中删除分区,但是散列分区和复合分区的散列自分区,只能通过合并来达到删除的目的。
- 删除一个分区
删除把user分区中的user_tps04分区
alter table user drop partition user_tps04;
- 删除有数据和全局索引的表分区
删除把user分区中的user_tps04分区,然后重建索引user_index
alter table user drop partition user_tps04;
alter index user ware_index rebuild;
如果user_index是范围分区的全局索引,需实现以下代码
alter index user rebuild index_01;
alter index user rebuild index_02;
alter index user rebuild index_03;
- 使用delete和alert table…drop partition语句
首先删除user分区表中10月份数据,再删除数据对应的user_tps10分区
delete from user where create_date >= to_date(‘2016-10-01’,’yyyy-mm-dd’);
alert table user drop partition user_tps10;
- 删除具有完整性约束的分区
- 首先禁止完整性约束,然后执行aliert table…drop patition,最后激活约束
禁用user表中主键约束user_pk,然后删除user表分区user_tps01,最后激活user表中的主键约束user_pk。
alter table user disable constraints user_pk;
alter table user drop patition user_tps01;
alter table user enable constraints user_tps01;
- 首先执行delete语句删除分区中的行,然后用alert table drop patition语句删除分区
delete table from user where create_date <= to_date(‘2016-01-01’,’yyyy-mm-dd’);
alert table user drop partition user_tps01;
并入表分区
将两个相邻的分区合并为一个分区,该合并分区继承原来两个分区的边界;原来两个分区的索引被删除;合并和索引一般需要重建;不可以对hash分区进行操作。
- 合并分区
alter table user merge partition user_tps02,user_tps03 into partition user_tps04;
网友评论