oracle 分区
一、背景: 大数据量的表, 数据行数增多,影响表的管理(删查改)和性能
二、原理:把一个表中的行进行划分,归为几部分
三、优点:
1、改善大型表的查询性能, 通过查询对应分区表中,对应的数据,而不查询整个表
2、分区表的数据存储在各个分区中,备份分区,可以快速恢复对应分区的数据,不需要对全表数据进行恢复
四、创建表及范围分区
create table 表名
(
scoreid VARCHAR2(18) not null,
stuid VARCHAR2(11),
courseid VARCHAR2(9),
score NUMBER,
scdate DATE
)
partition by range(scdate)( ---按范围分区
partition 分区名 (values less than ()) 表空间
partition p_score_2018 ( values less than (TO_DATE('2019-01-01 00:00:00','yyyy-mm-ddhh24:mi:ss'))
TABLESPACE TS_2018, ---(表空间)
partition p_score_2019 values less than (TO_DATE('2020-01-01 00:00:00','yyyy-mm-ddhh24:mi:ss'))
TABLESPACE TS_2019,
partition p_score_2020 values less than (MAXVALUE)
TABLESPACE TS_2020
);
五、分区类型(以下例子转载自https://blog.csdn.net/huangbaokang/article/details/94380569)
分区表分为四类:范围分区表、列表分区表、哈希分区表、组合分区表
1、范围分区
create table people(
id number,
age int not null,
address varchar2(100))
partition by range (age)
(partition p1 values less than (10) tablespace_1,
partition p2 values less than (20) tablespace_2,
partition p3 values less than (30) tablespace_3);
2、列表分区
create table people_list (name varchar2(20),city varchar2(20))
partition by list (city)
(partition p1 values ('ganzhou','nankang') tablespace users,
partition p2 values ('quannan','dingnan') tablespace hbk_data);
3、哈希分区
按照分区数量和指定分
create table people_hash (id number,age number)
partition by hash(age) partitions 4;
六、分区表的增删查改
1、增加分区
alter table people add partition padd values less than (90) tablespace users; ---范围
alter table people_hash add partition p_hash tablespace hbk_data; ---哈希
alter table people_list add partition p_list values ('beijing','shanghai') tablespace users; --列表
2、删除分区
delete from people_list partition(p1) where city='ganzhou';
3、查询分区
select * from people_list partition(p1);
4、修改分区
update people_list partition(p1) set city='ganzhou' where name='huangbaokang';
insert into people_list(name,city) values ('huangbaokang','nankang');