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: 从业务角度可以解决.. (分表,水平 ...
随机推荐
- 官网下载Git方法
最近去官网下载Git,奇慢,下到一半直接挂掉,挂VPN也是一样 https://git-scm.com/ 今天学到一个方法,下载速度可以达到2m/s,那就是复制下载地址,用迅雷下载,可能是迅雷有P2 ...
- Md5的生成
1.使用hashlib包(一) import hashlib src = 'anthing' m1 = hash.new() m1.update(src) print (m1.hexdigest()) ...
- 7-EL表达式和JSTL表达式
引入jar包 一.EL表达式1.表达式语言,用于jsp网页中获取和计算数据2.语法:${表达式}3.用于取值:可以从pageContext,request,session,application这些域 ...
- Mqtt用户认证
http://emqtt.com/docs/v2/guide.html 1默认是匿名认证,不用输入用户名和密码,直接可连接 2如何开启用户名和密码认证模式 2-1关闭匿名认证 在你的MQTT安装目录下 ...
- RabbitMq 6种使用模式
RabbitMQ的5种模式与实例 1.1 简单模式Hello World 功能:一个生产者P发送消息到队列Q,一个消费者C接收 生产者实现思路: 创建连接工厂ConnectionFactory,设置服 ...
- Could not get a resource from the pool 错误解决
错误关键信息:Could not get a resource from the pool 通常原因是因为远程服务器上的redis没有配置好. 解决方案如下:(1)将redis.conf中的bind: ...
- Pycharm远程调试服务器代码(使用Pipenv管理虚拟环境)
准备工作 1.随便准备一个项目工程,在本地用Pipenv创建一个虚拟环境并生成Pipfile和pipfile.lock文件,如下: 2.准备一台服务器,我这里使用阿里云的ECS SSH连接上 $ ss ...
- IntelliJ IDEA 高效率配置
之前学习和开发的时候一直用Eclipse,现在转战IDEA,记录一下IDEA的个性化设置,有助于提高效率.(参考:http://www.cnblogs.com/huaxingtianxia/p/586 ...
- Linux Docker命令
命令查看你当前的内核版本:uname -r yum 包更新到最新:yum update 安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemap ...
- Mac 小记 — iTerm2、Zsh、Homebrew
前言 写完 "Ubuntu 自动化配置" 这篇文章后,每次连服务器心情指数都上升好几个百分点,于是想着应该将 macOs 的开发环境也梳理梳理,应该会对开发效率有所增益. 1. i ...