Oracle数据库之分区表

来源:互联网 发布:jmeter执行python脚本 编辑:程序博客网 时间:2024/05/17 00:56

分区

作为大型数据库,在实际应用过程中,其数据量的大小都有可能达到TB级别,为了方便对表中的数据的管理,需要考虑分区问题;

**分区:将一个巨型表分成若干个独立的组成部分进行存储和管理,每个相对小的,可以独立管理的部分,称为原来表的分区。

存在以下优点:

**提高数据的安全性,一个分区损坏不影响其他分区中数据的正常使用**将表的各个分区存储在不同的磁盘上,提高数据的并行操作能力**简化数据管理,将部分分区设置为不可用状态,其他分区设置为可用状态....等等其他操作**操作的透明性,对表进行分区并不影响操作数据的SQL语句。

分区方法:

**范围分区根据分区列值的范围对表进行分区,每条记录根据其分区列值所在的范围决定存储到哪个分区中。**列表分区分区不能按照范围分区,同时分区列的取值是一个少数值的集合,采用列表分区。**散列分区基于分区列值的HASH算法,将数据均匀分布到指定的分区中。一个记录最终分布到哪个分区由Hash函数决定。**复合分区结合两种分区方法,采用一个分区方法对表或索引进行分区,然后在采用另外一个分区方法将分区在分成若干个分区。每一个分区的子分区都是数据的一个逻辑子集。

1、范围分区

    create TABLE table(...)    partition by range(column1[,column2,....])    (partition partition1 values less then(literal | maxvalue)[TABLESPACE tablespace]    (partition partition2 values less then(literal | maxvalue)[TABLESPACE tablespace]    ...    )

其中:
partition by range :指明采用范围分区方法
column:分区列
partition partition1:设置分区名称
values less then:设置分区列值的上界
TABLESPACE:设置分区对应的表空间

例子:

    create table hire_time_range(        employees_id NUMBER(6) PRIMARY KEY,        first_name VARCHAR2(20),        last_name VARCHAR2(25),        hire_date DATE    )    partition by group(hiretime)    (partition p1 values less than(to_date('1990-1-1'),'yyyy-mm-dd')    tablespace  oracltbs1;    partition p1 values less than(to_date('1990-1-1'),'yyyy-mm-dd')    tablespace  oracltbs2;    partition p3 values less then(maxvalue) tablespace oracltbs2    );

2、列表分区

    create TABLE table(...)    partition by list(column1[,column2,....])    (partition partition1 values ([literal|null] | (default)) [TABLESPACE tablespace]    (partition partition2 values ([literal|null] | (default)) [TABLESPACE tablespace]    ...    )

例子:

    create table stu_list(      sno number(6) primary key,      sname varchar2(10),      sex char(2) check(sex in ('M','F'))    )    partition by list(sex)    (        partition stu_male values('m') tablespace oracltbs1,        partition stu_male values('f') tablespace oracltbs2    )

3、散列分区

    create TABLE table(...)    partition by hash(column1[,column2,....])    (partition partition1 values [TABLESPACE tablespace]    (partition partition_partition_quantity store in  [tablespace1,....]    ...    )

例子:

    create table stu_hash(      sno number(6) primary key,      sname varchar2(10)    )    partition by hash(sno)    (       partition p1 tablespace oracltbs1,       partition p1 tablespace oracltbs2    );

4、复合分区

首先在create table 语句中使用partition by 子句指定分区方法,分区列,然后使用subpartition by 子句指定子分区方法,子分区列,子分区数量及子分区的描。

应用例子1:

    create table hire_time_range(        employees_id NUMBER(6) PRIMARY KEY,        first_name VARCHAR2(20),        last_name VARCHAR2(25),        hire_date DATE    )    partition by group(hiretime)    ( partition p1 values less than(to_date('1990-1-1'),'yyyy-mm-dd'))            (                subpartition p1_sub1 tablespace oracltbs1,                subpartition p1_sub1 tablespace oracltbs2            )      partition p1 values less than(to_date('1990-1-1'),'yyyy-mm-dd'))                    (                subpartition p2_sub1 tablespace oracltbs3,                subpartition p2_sub1 tablespace oracltbs4            )      partition p3 values less then(maxvalue) tablespace oracltbs5    );
原创粉丝点击