Oracle数据库中分区表的操作方法

jopen 10年前

一、为什么要做分区表?

当数据量非常大,比如几百GB或是到T的时候。那查询的速度可想而知,Oracle提供了对表和索引进行分区的技术,以改善大型应用系统的性能。 

使用分区的优点: 
  ·增强可用性:如果表的某个分区出现故障,表在其他分区的数据仍然可用; 
  ·维护方便:如果表的某个分区出现故障,需要修复数据,只修复该分区即可; 
  ·均衡I/O:可以把不同的分区映射到磁盘以平衡I/O,改善整个系统性能; 
  ·改善查询性能:对分区对象的查询可以仅搜索自己关心的分区,提高检索速度。
  Oracle数据库提供对表或索引的分区方法有三种: 
  ·范围分区   ·Hash分区(散列分区)   ·复合分区 

二、下边分别对三种分区方法作操作

    为了方便,先建立三个表空间

create tablespace test1 datafile 'd:/分区test/test1.dnf' size 50M;  create tablespace test2 datafile 'd:/分区test/test2.dnf' size 50M;  create tablespace test3 datafile 'd:/分区test/test3.dnf' size 50M;

 1.范围分区

1.1根据序号进行分区建表

SQL> create table fenqutest(    2  id number,    3  name varchar2(50)    4  )    5  partition by range(id)    6  (partition part1 values less than(5) tablespace test1,    7  partition part2 values less than(10) tablespace test2,    8  partition part3 values less than(maxvalue) tablespace test3);

这是我自己的做的小测试,很简写。

那么当表建完了,数据也添加好了,怎么来查看某个数据在哪张表里呢?

很简单:select * from fenqutest partition(part1);

1.2根据日期进行分区建表

SQL> create table fenqutest(      id number,      time_test date,      name varchar2(50)      )      partition by range(time_test)      (partition part1 values less than(to_date(’2011-02-27’,’yyyy-mm-dd’)) tablespace test1,      partition part2 values less than(to_date(’2014-02-28’,’yyyy-mm-dd’)) tablespace test2,      partition part3 values less than(maxvalue) tablespace test3);

当然你也可以根据别的来分区

2.Hash分区(散列分区)

散列分区为通过指定分区编号来均匀分布数据的一种分区类型,因为通过在I/O设备上进行散列分区,使得这些分区大小一致

SQL> create table fenqutest(      id number,      time_test date,      name varchar2(50)      )      partition by hash(id)      (partition part1 tablespace test1,      partition part2 tablespace test2,      partition part3 tablespace test3);

3.复合分区

有时候我们需要根据范围分区后,每个分区内的数据再散列地分布在几个表空间中,这样我们就要使用复合分区。复合分区是先使用范围分区,然后在每个分区内再使用散列分区的一种分区方法

SQL> create table fenqutest(      id number,      time_test date,      name varchar2(50)      )      partition by range(time_test) subpartition by hash(id)      subpartitions 3 store in (test1,test2,test3)      (partition part1 values less than(to_date(’2011-02-27’,’yyyy-mm-dd’)) tablespace test1,      partition part2 values less than(to_date(’2014-02-28’,’yyyy-mm-dd’)) tablespace test2,      partition part3 values less than(maxvalue) tablespace test3);