Oracle 表分区(Partition)
表分区功能能够改善应用程序性能,提高数据库可管理性和可用性,是数据库管理非常关键的技术。数据库通过使用分区提高查询性能,简化日常管理维护工作。
1 分区优点
1) 减少维护工作量,独立管理每个表分区比管理整个大表要轻松的多
2) 增加数据库的可用性,由于将数据分散到各个分区中,减少了数据损坏的可能性
3) 均衡I/O,减少竞争,通过把表的不同分区分配到不同的磁盘来平衡I/O改善性能
4) 分区对用户保持透明,用户感受不到它的存在
5) 提高查询速度,对于大表的DML操作可以分解到表的不同分区来执行,可以加快执行速度
2 分区缺点
已经存在的表,不能直接转化为分区表
3 什么时候使用分区表
Range分区是应用范围比较广的表分区方式,它是以列的值的范围来做为分区的划分条件,将记录存放到列值所在的range分区中。
如按照时间划分,2017年第一季度的数据放到第一分区,二季度的数据放到第二分区,在创建的时候,需要指定基于的列,以及分区的范围值。在按时间分区时, 如果某些记录暂无法预测范围,可以创建 maxvalue 分区,所有不在指定范围内的记录都会被存储到 maxvalue 所在分区中。
create table emp_range
(
empno number not null primary key,
deptno number not null,
first_name varchar2(30) not null,
last_name varchar2(30) not null,
status char(1),
hire_date date not null
)
partition by range(hire_date)
(
partition hire_part1 values less than(to_date('2017-04-01','yyyy-mm-dd')) tablespace emp_space01,
partition hire_part2 values less than(to_date('2017-07-01','yyyy-mm-dd')) tablespace emp_space02,
partition hire_part3 values less than(to_date('2017-10-01','yyyy-mm-dd')) tablespace emp_space03,
partition hire_part4 values less than(to_date('2018-01-01','yyyy-mm-dd')) tablespace emp_space04
);
测试数据
insert into emp_range(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 10,'latiny1','liu','1', to_date('2017-01-02','yyyy-mm-dd'));
insert into emp_range(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 20,'latiny2','liu2','1', to_date('2017-04-02','yyyy-mm-dd'));
insert into emp_range(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 30,'latiny3','liu3','1', to_date('2017-07-02','yyyy-mm-dd'));
insert into emp_range(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 40,'latiny4','liu4','1', to_date('2017-10-02','yyyy-mm-dd'));
按分区查询结果
select *
from emp_range partition(hire_part1);
2) HASH分区
create table emp_hash
(
empno number not null primary key,
deptno number not null,
first_name varchar2(30) not null,
last_name varchar2(30) not null,
status char(1),
hire_date date not null
)
partition by hash(deptno)
(
partition dep_part1 tablespace emp_space01,
partition dep_part2 tablespace emp_space02,
partition dep_part3 tablespace emp_space03
);
测试数据
insert into emp_hash(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 10,'latiny1','liu','1', to_date('2017-01-02','yyyy-mm-dd'));
insert into emp_hash(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 20,'latiny2','liu2','1', to_date('2017-04-02','yyyy-mm-dd'));
insert into emp_hash(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 30,'latiny3','liu3','1', to_date('2017-07-02','yyyy-mm-dd'));
insert into emp_hash(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 40,'latiny4','liu4','1', to_date('2017-10-02','yyyy-mm-dd'));
insert into emp_hash(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 50,'latiny5','liu5','1', to_date('2017-11-02','yyyy-mm-dd'));
insert into emp_hash(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 60,'latiny6','liu6','1', to_date('2017-08-02','yyyy-mm-dd'));
select *
from emp_hash partition(dep_part1);
散列分区最主要的机制是根据Hash算法来计算具体某条纪录应该插入到哪个分区中, Hash算法中最重要的是Hash函数,Oracle中如果你要使用Hash分区,只需指定分区的数量即可。建议分区的数量采用2的n次方,这样可以使得各个分区间数据分布更加均匀。
create table emp_list
(
empno number not null primary key,
deptno number not null,
first_name varchar2(30) not null,
last_name varchar2(30) not null,
status char(1),
hire_date date not null
)
partition by list(status)
(
partition status_part1 values('1') tablespace emp_space01,
partition status_part2 values('0') tablespace emp_space02
);
insert into emp_list(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 10,'latiny1','liu','1', to_date('2017-01-02','yyyy-mm-dd'));
insert into emp_list(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 20,'latiny2','liu2','0', to_date('2017-04-02','yyyy-mm-dd'));
insert into emp_list(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 30,'latiny3','liu3','1', to_date('2017-07-02','yyyy-mm-dd'));
insert into emp_list(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 40,'latiny4','liu4','0', to_date('2017-10-02','yyyy-mm-dd'));
insert into emp_list(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 50,'latiny5','liu5','1', to_date('2017-11-02','yyyy-mm-dd'));
insert into emp_list(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 60,'latiny6','liu6','0', to_date('2017-08-02','yyyy-mm-dd'));
select *
from emp_list partition(status_part2);
create table emp_range_hash
(
empno number not null primary key,
deptno number not null,
first_name varchar2(30) not null,
last_name varchar2(30) not null,
status char(1),
hire_date date not null
)
partition by range(hire_date)subpartition by hash(deptno) subpartitions 4 store in (emp_space01,emp_space02,emp_space03,emp_space04)
(
partition part_01 values less than(to_date('2017-04-01','yyyy-mm-dd')),
partition part_02 values less than(to_date('2017-07-01','yyyy-mm-dd')),
partition part_03 values less than(to_date('2017-10-01','yyyy-mm-dd')),
partition part_04 values less than(to_date('2018-01-01','yyyy-mm-dd'))
);
-- 测试数据
insert into emp_range_hash(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 10,'latiny1','liu','1', to_date('2017-01-02','yyyy-mm-dd'));
insert into emp_range_hash(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 20,'latiny2','liu2','0', to_date('2017-04-02','yyyy-mm-dd'));
insert into emp_range_hash(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 30,'latiny3','liu3','1', to_date('2017-07-02','yyyy-mm-dd'));
insert into emp_range_hash(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 40,'latiny4','liu4','0', to_date('2017-10-02','yyyy-mm-dd'));
insert into emp_range_hash(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 50,'latiny5','liu5','1', to_date('2017-11-02','yyyy-mm-dd'));
insert into emp_range_hash(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 60,'latiny6','liu6','0', to_date('2017-08-02','yyyy-mm-dd'));
insert into emp_range_hash(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 20,'latiny7','liu7','1', to_date('2017-01-02','yyyy-mm-dd'));
insert into emp_range_hash(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 30,'latiny8','liu8','1', to_date('2017-01-02','yyyy-mm-dd'));
select *
from emp_range_hash partition(part_01);
范围--列表分区
这种分区是基于范围分区和列表分区,表首先按某列进行范围分区,然后再按某列进行列表分区,分区之中的分区被称为子分区。
create table emp_range_list
(
empno number not null primary key,
deptno number not null,
first_name varchar2(30) not null,
last_name varchar2(30) not null,
status char(1),
hire_date date not null
)
partition by range(hire_date) subpartition by list(status)
(
partition part_01 values less than(to_date('2017-04-01','yyyy-mm-dd')) tablespace emp_space01
(
subpartition p1sub1 values('1') tablespace emp_space01,
subpartition p1sub2 values('0') tablespace emp_space01
),
partition part_02 values less than(to_date('2017-07-01','yyyy-mm-dd')) tablespace emp_space02
(
subpartition p2sub1 values('1') tablespace emp_space02,
subpartition p2sub2 values('0') tablespace emp_space02
)
);
-- 测试数据
insert into emp_range_list(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 10,'latiny1','liu','1', to_date('2017-01-02','yyyy-mm-dd'));
insert into emp_range_list(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 20,'latiny2','liu2','0', to_date('2017-04-02','yyyy-mm-dd'));
insert into emp_range_list(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 30,'latiny3','liu3','1', to_date('2017-06-02','yyyy-mm-dd'));
insert into emp_range_list(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 40,'latiny4','liu4','1', to_date('2017-05-02','yyyy-mm-dd'));
insert into emp_range_list(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 30,'latiny5','liu5','0', to_date('2017-06-02','yyyy-mm-dd'));
insert into emp_range_list(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 40,'latiny6','liu6','0', to_date('2017-05-02','yyyy-mm-dd'));
insert into emp_range_list(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 10,'latiny7','liu7','0', to_date('2017-01-02','yyyy-mm-dd'));
insert into emp_range_list(empno, deptno, first_name, last_name, status, hire_date)
values (seq_par_id.nextval, 20,'latiny8','liu8','1', to_date('2017-01-02','yyyy-mm-dd'));
select *
from emp_range_list partition(part_01);
5 分区管理维护
1)添加表分区
对于已经存在表分区的表,如果要添加一个新的表分区,使用如下语法(我们对范围分区表实例添加一个新的分区):
alter table emp_range
add partition hire_date5
values less than (to_date('2018-04-01','yyyy-mm-dd')) tablespace emp_space0;
-- 注意:以上添加的分区界限应该高于最后一个分区界限。
给emp_range_list 表的part_02 分区添加一个子分区,p2sub3:
alter table emp_range_list modify partition part_02 add subpartition p2sub3 values('3') tablespace emp_space02;
Oracle 表分区(Partition)的更多相关文章
- Oracle 表分区partition(http://love-flying-snow.iteye.com/blog/573303)
http://www.jb51.net/article/44959.htm Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库表中某一字段的值的范围来划 ...
- oracle表分区详解
原文来自:http://www.cnblogs.com/leiOOlei/archive/2012/06/08/2541306.html oracle表分区详解 从以下几个方面来整理关于分区表的概念及 ...
- 转:Oracle表分区
Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库表中某一字段的值的范围来划分分区,例如: 1. create table graderecord 2. ...
- Oracle表分区[转]
废话少说,直接讲分区语法. Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库表中某一字段的值的范围来划分分区,例如: create table gra ...
- oracle表分区的,分区操作,分区查询,子分区查询
一.摘要 有关表分区的一些维护性操作: 注:分区根据具体情况选择. 表分区有以下优点: 1.数据查询:数据被存储到多个文件上,减少了I/O负载,查询速度提高. 2.数据修剪:保存历史数据非常的理想. ...
- oracle 表分区例子
oracle表分区详解-一步一步教你oracle分区表详解 .创建三个不同的表空间,模拟在不同磁盘上的保存不同范围的数据 create tablespace test01 datafile ...
- oracle表分区、表分析及oracle数据泵文件导入导出开心版
1.先说oracle表分区是什么吧,这样吧我们来举个桃子,栗子太小,我们就不举了,我们来举个桃子. 你有500万份文件,你要把他存在磁盘上,好嘛,我们就一个文件夹,500万分文件在那儿杵着,我们想找到 ...
- oracle表分区、表分析及oracle数据泵文件导入导出
1.先说oracle表分区是什么吧 你有500万份文件,你要把他存在磁盘上,好嘛,我们就一个文件夹,500万分文件在那儿杵着,我们想找到要的那个打开,嘿嘿,我们得找到什么时候. 这时候,有个人告诉你, ...
- mysql表分区 partition
表分区 partition 当一张表的数据非常多的时候,比如单个.myd文件都达到10G, 这时,必然读取起来效率降低. 可不可以把表的数据分开在几张表上? 1: 从业务角度可以解决.. (分表,水平 ...
随机推荐
- JavaScrip 入门第一课
一.代码引入的三种方式 1.直接在head中书写 在head标签里面可以写,在body标签里面也可以写,放到head标签里面和放到body标签里面到底有什么区别,我们后续在讲~ <head> ...
- deeplearn.js
http://blog.csdn.net/rongxiang111/article/details/78083867 <!DOCTYPE html> <html> <he ...
- ubuntu下配置rsync,实现远程备份
rysnc(remote synchronize)在CentOS系统默认安装在/usr/bin,此外rysnc在windows平台下也有相应版本.主页地址为: http://rsync.samba.o ...
- CAS适用场景
转载:http://www.jb51.net/article/86192.htm 下面小编就为大家带来一篇Java并发编程总结——慎用CAS详解.小编觉得挺不错的, 现在就分享给大家,也给大家做个参考 ...
- Linux的目录结构--一切从根开始
Linux目录结构的特点 # 举例-linux下面使用光盘 ###.把光盘放入到光驱中 ###.linux中使用光盘 /dev/cdrom [root@oldboyedu- ~]# ll /dev/c ...
- 6.04-news_xpath3
from lxml import etree html = """ <html> <body> <ul> <li>1 &l ...
- 【vue】vue +element 实现批量删除
相关资料:http://blog.csdn.net/eagle_88/article/details/70238836 作者:smile.轉角 QQ:493177502
- 001_HTTP参数中Etag的重要性
在研究tornado时,有个Etag比较好奇,从网上查询摘录如下:
- 用java语言写一个简易版本的登录页面,包含用户注册、用户登录、用户注销、修改密码等功能
package com.Summer_0421.cn; import java.util.Arrays; import java.util.Scanner; /** * @author Summer ...
- 【LOJ 2144】「SHOI2017」摧毁「树状图」
LOJ 2144 84pts 首先\(op2\)很简单.直接并查集一搞就好了(话说我现在什么东西都要写个并查集有点...) 然后\(op0\)我不会,就直接\(O(n^2)\)枚举一下\(P\)这个人 ...