文章目录

  • 1 概述
    • 1.1 思维导图
  • 2 分类
    • 2.1 传统表分区
      • 2.1.1 范围分区 range
      • 2.1.2 列表分区 list
      • 2.1.3 哈希分区 hash
      • 2.1.4 复合分区 range + list or hash
    • 2.2 11g 新特性分区
      • 2.1.1 引用分区 reference
      • 2.1.2 间隔分区 interval
      • 2.1.3 虚拟列分区 virtual
      • 2.1.4 系统分区 system
  • 3 管理
    • 3.1 表分区
    • 3.2 表空间

1 概述

1. 目的:提高大表的查询效率2. 概念:将一个表划分为多个分区表,"分而治之"3. 优缺点优点:(1) '改善查询性能': 分区对象的查询仅搜索自己关系的分区(2) '增强可用性': 如果某个分区出现故障,其它分区的数据仍然可用(3) '维护方便': 如果某个分区出现故障,仅修复该分区即可(4) '均衡I/O': 将不同的分区放置不同的磁盘,以均衡 I/O,改善整个系统性能缺点:(1) 已经存在的表无法直接转化为分区表 -- 不过有很多间接方法,如:重定义表4. 适用情况(1) 表的大小超过 2GB

1.1 思维导图

2 分类

2.1 传统表分区

2.1.1 范围分区 range

情况1:数值范围分区

create table pt_range_test1(pid   number(10),pname varchar2(30)
) partition by range(pid)(partition p1 values less than(1000) tablespace tetstbs1,partition p2 values less than(2000) tablespace tetstbs2,partition p3 values less than(maxvalue) tablespace tetstbs3
) enable row movement;

插入数据:

insert into pt_range_test1 (pid, pname) values (1, '瑶瑶');
insert into pt_range_test1 (pid, pname) values (1500, '倩倩');
insert into pt_range_test1 (pid, pname) values (null, '优优');
commit;

查询数据:

select * from user_tab_partitions t;select 'P1' 分区名, t.* from pt_range_test1 partition (p1) t union all
select 'P2' 分区名, t.* from pt_range_test1 partition (p2) t union all
select 'P3' 分区名, t.* from pt_range_test1 partition (p3) t

情况2:时间范围分区(同理)

create table pt_range_test2(pid         number(10),pname       varchar2(30),create_date date
) partition by range(create_date)(partition p1 values less than(to_date('2020-01-01', 'YYYY-MM-DD')) tablespace tetstbs1,partition p2 values less than(to_date('2021-01-01', 'YYYY-MM-DD')) tablespace tetstbs2,partition p3 values less than(maxvalue) tablespace tetstbs3
) enable row movement;

2.1.2 列表分区 list

create table pt_list_test(pid   number(10),pname varchar2(30),sex   varchar2(10)
) partition by list(sex)(partition p1 values ('MAN', '男') tablespace tetstbs1,partition p2 values ('WOMAN', '女') tablespace tetstbs2,partition p3 values (default) tablespace tetstbs3
) enable row movement;

插入数据:

insert into pt_list_test (pid, pname, sex) values (1, '瑶瑶', '男');
insert into pt_list_test (pid, pname, sex) values (2, '倩倩', 'WOMAN');
insert into pt_list_test (pid, pname, sex) values (3, '优优', 'GOD');
commit;

查询数据:

select 'P1' 分区名, t.* from pt_list_test partition (p1) t union all
select 'P2' 分区名, t.* from pt_list_test partition (p2) t union all
select 'P3' 分区名, t.* from pt_list_test partition (p3) t

2.1.3 哈希分区 hash

create table pt_hash_test(pid   number(10),pname varchar2(30)
) partition by hash(pid)(partition p1 tablespace tetstbs1,partition p2 tablespace tetstbs2,partition p3 tablespace tetstbs3,partition p4 tablespace tetstbs4
);

简写:

create table pt_hash_test2(pid   number(10),pname varchar2(30)
) partition by hash(pid) partitions 4 store in (tetstbs1, tetstbs2, tetstbs3, tetstbs4);

2.1.4 复合分区 range + list or hash

情况1:range + list

create table pt_range_list_test(pid         number(10),pname       varchar2(30),sex         varchar2(10),create_date date
) partition by range(create_date) subpartition by list(sex)(partition p1 values less than(to_date('2020-01-01', 'YYYY-MM-DD')) tablespace tetstbs1(subpartition sub1p1 values('MAN') tablespace tetstbs1,subpartition sub2p1 values('WOMAN') tablespace tetstbs1,subpartition sub3p1 values(default) tablespace tetstbs1),partition p2 values less than(to_date('2021-01-01', 'YYYY-MM-DD')) tablespace tetstbs2(subpartition sub1p2 values('MAN') tablespace tetstbs2,subpartition sub2p2 values('WOMAN') tablespace tetstbs2,subpartition sub3p2 values(default) tablespace tetstbs2),partition p3 values less than(maxvalue) tablespace tetstbs3(subpartition sub1p3 values('MAN') tablespace tetstbs3,subpartition sub2p3 values('WOMAN') tablespace tetstbs3,subpartition sub3p3 values(default) tablespace tetstbs3)) enable row movement;

情况2:range + hash

create table pt_range_hash_test(pid         number(10),pname       varchar2(30),sex         varchar2(10),create_date date
) partition by range(create_date) subpartition by hash(pid) subpartitions 4 store in (tetstbs1, tetstbs2, tetstbs3, tetstbs4)(partition p1 values less than(to_date('2020-01-01', 'YYYY-MM-DD')) tablespace tetstbs1,partition p2 values less than(to_date('2021-01-01', 'YYYY-MM-DD')) tablespace tetstbs2,partition p3 values less than(to_date('2022-01-01', 'YYYY-MM-DD')) tablespace tetstbs3,partition p4 values less than(maxvalue) tablespace tetstbs4) enable row movement;

2.2 11g 新特性分区

2.1.1 引用分区 reference

  • 外键列必须 not null
-- 父表
create table pt_reference_father_test(pid         number(10),pname       varchar2(30),create_date date,constraint pk_ptrft_pid primary key(pid)
) partition by range(create_date)(partition p1 values less than(to_date('2020-01-01', 'YYYY-MM-DD')) tablespace tetstbs1,partition p2 values less than(to_date('2021-01-01', 'YYYY-MM-DD')) tablespace tetstbs2,partition p3 values less than(maxvalue) tablespace tetstbs3) enable row movement;-- 子表
create table pt_reference_son_test(pid     number(10) not null, -- 必须 not null,否则报错item_id number(10),constraint pk_ptrst_item_id primary key(item_id),constraint fk_ptrst_pid foreign key(pid)references pt_reference_father_test(pid)
) partition by reference(fk_ptrst_pid)enable row movement;

2.1.2 间隔分区 interval

  • 必须有个初始分区,且无法删除(除非直接删除表)
-- 初始时间范围分区 2020-01-01
-- 之后数据每间隔 1 年,新建一个分区
create table pt_interval_test(pid         number(10),pname       varchar2(30),create_date date
) partition by range(create_date) interval(numtoyminterval(1, 'YEAR'))(partition p1 values less than(to_date('2020-01-01', 'YYYY-MM-DD')) tablespace tetstbs1);

分别插入数据,观察变化:

insert into pt_interval_test(pid, pname, create_date) values(1, '瑶瑶', to_date('2019-01-01', 'YYYY-MM-DD'));insert into pt_interval_test(pid, pname, create_date) values(2, '倩倩', to_date('2020-01-01', 'YYYY-MM-DD'));select * from user_tab_partitions t where t.table_name = upper('pt_interval_test');

2.1.3 虚拟列分区 virtual

  • 将分区建立在某个虚拟列上(函数或表达式 的计算结果上)
create table pt_virtual_test(pid         number(10),pname       varchar2(30),create_date date,create_quarterly as (to_char(create_date,'D')) virtual
) partition by list(create_quarterly)(partition p1 values(1) tablespace tetstbs1,partition p2 values(2) tablespace tetstbs2,partition p3 values(3) tablespace tetstbs3,partition p4 values(4) tablespace tetstbs4,partition p5 values(default) tablespace tetstbs4);

2.1.4 系统分区 system

  • 不能指定分区列
create table pt_system_test(pid   number(10),pname varchar2(30)
) partition by system(partition p1 tablespace tetstbs1,partition p2 tablespace tetstbs2,partition p3 tablespace tetstbs3);

3 管理

3.1 表分区

1. 查询: select * from user_tab_partitions t;2. 添加: alter table <table_name> add partition <partition_name> values less than(to_date('2020-02-02', 'YYYY-MM-DD'));alter table <table_name> add partition <partition_name> values less than(1000);3. 删除: (请注意:无法删除分区表唯一的分区,除非删除表)alter table <table_name> drop partition <partition_name>;alter table <table_name> drop subpartition <subpartition_name>;4. 截断分区('清空某个分区的数据')alter table <table_name> truncate partition <partition_name>;alter table <table_name> truncate subpartition <subpartition_name>;5. 拆分分区('拆分后,原来分区不再存在')alter table <table_name> sblit partition <p12> at(to_date('2020-01-01', 'YYYY-MM-DD')) into (partition p1, partition p2);6. 合并分区alter table <table_name> merge partitions <p1>, <p2> into partition <p12>;7. 重命名分区alter table <table_name> rename partition <pold> to <pnew>

3.2 表空间

1. 查询select * from user_tablespaces t;2. 创建 -- 创建表空间时,可选项有很多,此处仅列出必选项create tablespace "tbs"datafile 'D:\oracle\tbs_01.dbf'size 10m;3. 删除仅删除表空间:drop tablespace tbs;删除表空间和数据文件:drop tablespace tbs including contents and datafiles;

扩展:Oracle 表空间详解(tablespace)

Oracle 表分区详解(partition table)相关推荐

  1. oracle表分区详解

    此文从以下几个方面来整理关于分区表的概念及操作: 1.表空间及分区表的概念 2.表分区的具体作用 3.表分区的优缺点 4.表分区的几种类型及操作方法 5.对表分区的维护性操作. (1.) 表空间及分区 ...

  2. (转)oracle表分区详解

    此文从以下几个方面来整理关于分区表的概念及操作: 1.表空间及分区表的概念 2.表分区的具体作用 3.表分区的优缺点 4.表分区的几种类型及操作方法 5.对表分区的维护性操作. (1.) 表空间及分区 ...

  3. oracle 表分区详解

    目录 一.概念 二.类型及操作方法 1. 范围分区 range -最常用 2. 列表分区 list 3. 散列分区 hash 4. 组合分区 a. 范围-列表分区 b. 范围-散列分区 三.表分区的查 ...

  4. Oracle表分区详解(优缺点)

    此文从以下几个方面来整理关于分区表的概念及操作:         1.表空间及分区表的概念         2.表分区的具体作用         3.表分区的优缺点         4.表分区的几种类 ...

  5. MySQL 表分区详解MyiSam引擎和InnoDb 区别(实测)

    MySQL 表分区详解MyiSam引擎和InnoDb 区别(实测) 一.什么是表分区 通俗地讲表分区是将一大表,根据条件分割成若干个小表.mysql5.1开始支持数据表分区了. 如:某用户表的记录超过 ...

  6. mysql 社区版 innodb_MySQL 表分区详解MyiSam引擎和InnoDb 区别(实测)

    MySQL 表分区详解MyiSam引擎和InnoDb 区别(实测) / --------------------         写在最前面            ------------------ ...

  7. Oracle表空间_PK是什么意思,Oracle表空间详解

    关键字:Oracle表空间详解 一.============  查询 =================== 1.查询oracle用户的默认表空间和临时表空间 select default_table ...

  8. Oracle 的表分区详解

    --本文转自https://www.cnblogs.com/leiOOlei/archive/2012/06/08/2541306.html 从以下几个方面来整理关于分区表的概念及操作: 表空间及分区 ...

  9. oracle表空间表分区详解及oracle表分区查询使用方法

    此文从以下几个方面来整理关于分区表的概念及操作: 1.表空间及分区表的概念 2.表分区的具体作用 3.表分区的优缺点 4.表分区的几种类型及操作方法 5.对表分区的维护性操作. 表空间及分区表的概念 ...

最新文章

  1. 如何确定Scrum团队的最佳规模?
  2. spring系统学习:day4--Spring配置: 集合类型属性的注入
  3. sgu 207 Robbers
  4. boost::random模块实现如何使用随机数库的简短演示程序
  5. [Qt教程] 第20篇 2D绘图(十)图形视图框架(下)
  6. [项目过程中所遇到的各种问题记录]图表篇——asp.net上不错的图表选择—FunsionCharts...
  7. android subString
  8. 【Python 2.7】str和unicode的互相转换,摘自《Effective Python》
  9. java 企业号 临时素材_查看“获取临时素材文件”的源代码
  10. wps启用编辑按钮在哪里_WPS 新功能上线,官宣首发!人人都会用的图片设计
  11. 一个女人在公司做领导是如何在4年内做到年薪200万的?
  12. 前端知识 — HTML内容、CSS基础
  13. Git工具代码版本管理以及基本使用说明
  14. 智慧树源码_知到智慧树_网课查题网站源码_答案章节作业期末答案
  15. [matlab数字图像处理10]对一副图像进行二值化,ostu算法等
  16. qq不加好友实现网页在线聊天(qq在线客服)
  17. QT QML 3D模型查看器
  18. 文件名的命名规则是什么
  19. 搜索引擎的概念鄂州_搜索引擎的基本概念
  20. WYSE POCKETCLOUD手把手教你如何用手机遥控你的电脑!!(转)

热门文章

  1. 解决前端小程序Provisional headers are shown问题
  2. where和having、sql语句执行顺序
  3. 蓝鲸平台MySQL数据库管理规范建议
  4. 微服务 fegin 404
  5. 在linux系统环境中 常用的关机命令,Linux常用基础命令整理:关机命令、查看目录下文件命令等...
  6. 微信这3个隐藏功能很实用!强烈建议开启!
  7. java与php的优缺点_java和php的发展前景以及优劣势
  8. hydra-microservice 中文手册(3W字预警)
  9. oppo与vivo手机低版本兼容问题
  10. iOS从零开始使用Swift:如何在设备上测试iOS应用程序