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: 从业务角度可以解决.. (分表,水平 ...
随机推荐
- 【Linux基础】alias命令指定别名
1.alias命令 alias是一个系统自建的shell命令,允许你为名字比较长的或者经常使用的命令指定别名. alias //显示当前定义的所有别名 alias ll='ls -l' //定义别名l ...
- PostGIS中dbf file (.dbf) can not be opened.shapefile import failed
postgis数据库文件shapefile导入 dbf file (.dbf) can not be opened.shapefile import failed. Destination: publ ...
- .net 解决伪静态下,html页面无法访问
1.在<system.web>节点下添加: <!--URL重写文件设置开始--> <httpHandlers> <add verb="*" ...
- raise ValueError("Cannot convert {0!r} to Excel".format(value))
I have hundreds of XML files that I need to extract two values from and ouput in an Excel or CSV fil ...
- 匆忙记录 编译linux kernel zImage
arm的板子. 自己要定制下内核. 下载源码 cp 模板配置 .config make menuconfig 进行定制化 之后make zImage {注意 交叉编译 gcc 也要配置的} 之后 ./ ...
- hash_hmac 签名
<?php /** * =========================================================== * Model_Base * Descriptio ...
- SQL优化工具SQLAdvisor使用
一.简介在数据库运维过程中,优化SQL是业务团队与DBA团队的日常任务.例行SQL优化,不仅可以提升程序性能,还能够降低线上故障的概率. 目前常用的SQL优化方式包括但不限于:业务层优化.SQL逻辑优 ...
- Sphinx 生成 Windows 帮助文件 (.chm文件)
本文不介绍 Sphinx 的用法,只简要罗列 Windows 下生成 .chm 文件的步骤. 0. 首先检查机器是否安装了 HTML Help Workshop 软件,一般安装路径应该是 C:\Pro ...
- Luogu4916 魔力环 莫比乌斯反演、组合、生成函数
传送门 先不考虑循环同构的限制,那么对于一个满足条件的序列,如果它的循环节长度为\(d\),那么与它同构的环在答案中就会贡献\(d\)次. 所以如果设\(f_i\)表示循环节长度恰好为\(i\)的满足 ...
- python--迭代器(Iterator)
博客地址:http://www.cnblogs.com/yudanqu/ 1.可迭代对象 在介绍迭代器之前呢,我们先聊一下可迭代对象(Iterable),可迭代对象就是可以直接作用于for循环的对象. ...