oracle partition 分区建立详解

来源:互联网 发布:数据化生产管理 编辑:程序博客网 时间:2024/06/10 14:20

版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://wto0228.blogbus.com/logs/44423132.html

Oracle9i通过引入列表分区(List Partition),使得当前共有4种分区数据方法,具体列出如下:
字串9

 

  第一种 范围分区 字串4

  1 对表进行单列范围分区:

字串6

  这使最为常用也是最简单方法,具体例子如下: 字串5

  create table emp
  (empno number(4),
  ename varchar2(30),
  sal number)
  partition by range(empno)
  (partition e1 values less than (1000) tablespace emp1,
  partition e2 values less than (2000) tablespace emp2,
  partition e3 values less than (maxvalue) tablespace emp3);
  
  insert into emp values (100,'Tom',1000);
  insert into emp values (500,'Peter',2000);
  insert into emp values (1000,'Scott',3000);
  insert into emp values (1999,'Bill',4000);
  insert into emp values (5000,'Gates',6000);
  commit; 字串6

  从emp表中选择全部纪录如下: 字串8

  SQL> select * from emp;
  
  EMPNO ENAME SAL
  ---------- ------------------------------ ----------
  100 Tom 1000
  500 Peter 2000
  1000 Scott 3000
  1999 Bill 4000
  5000 Gates 6000
字串2

 

  还可以按照分区进行选择: 字串6

  SQL> select * from emp partition (e1);
  EMPNO ENAME SAL
  ---------- ------------------------------ ----------
  100 Tom 1000
  500 Peter 2000
  
  SQL> select * from emp partition (e2)
  EMPNO ENAME SAL
  ---------- ------------------------------ ----------
  1000 Scott 3000
  1999 Bill 4000
  
  SQL> select * from emp partition (e3)
  EMPNO ENAME SAL
  ---------- ------------------------------ ----------
  5000 Gates 6000
字串5

 

  使用了分区,还可以单独针对指定分区进行truncate操作: 字串7

  alter table emp truncate partition e2;
字串3

  2 对表进行多列范围分区:

字串3

 

  多列范围分区主要是基于表中多个列值范围对数据进行分区,例如: 字串1

  drop table emp;
  create table emp
  (empno number(4),
  ename varchar2(30),
  sal number,
  day integer not null,
  month integer not null)
  partition by range(month,day)
  (partition e1 values less than (5,1) tablespace emp1,
  partition e2 values less than (10,2) tablespace emp2,
  partition e3 values less than (maxvalue,maxvalue) tablespace emp3);
  
  SQL> insert into emp values (100,'Tom',1000,10,6);
  SQL> insert into emp values (200,'Peter',2000,3,1);
  SQL> insert into emp values (300,'Jane',3000,23,11);
字串8

 

 

字串7

 第二种 Hash分区: 字串3

  hash分区最主要机制是根据hash算法来计算具体某条纪录应该插入到哪个分区中,hash算法中最重要是hash函数,Oracle中如果你要使用hash分区,只需指定分区数量即可。建议分区数量采用2n次方,这样可以使得各个分区间数据分布更加均匀。

字串1

  具体例子如下: 字串1

  drop table emp;
  create table emp (
  empno number(4),
  ename varchar2(30),
  sal number)
  partition by hash (empno)
  partitions 8
  store in (emp1,emp2,emp3,emp4,emp5,emp6,emp7,emp8); 字串6

  怎么样?很方便吧!

字串8

 

  第三种 复合分区:

字串2

 

  这是一种将前两种分区综合在一起使用方法,例如:

字串2

 

  drop table emp;
  create table emp (
  empno number(4),
  ename varchar2(30),
  hiredate date)
  partition by range (hiredate)
  subpartition by hash (empno)
  subpartitions 2
  (partition e1 values less than (to_date('20020501','YYYYMMDD')),
  partition e2 values less than (to_date('20021001','YYYYMMDD')),
  partition e3 values less than (maxvalue)); 字串9

  上面例子中将雇员表先按照雇佣时间hiredate进行了范围分区,然后再把每个分区分为两个子hash分区。例子中一共将产生6个分区。

字串5

 

  第四种 列表分区: 字串9

  这是Oracle 9i新特性,有了这种分区使得我们可以方便按照值来将数据分为更小片断。 字串1

  例如:

字串1

  drop table emp;
  create table emp (
  empno number(4),
  ename varchar2(30),
  location varchar2(30))
  partition by list (location)
  (partition e1 values ('北京'),
  partition e2 values ('上海','天津','重庆'),
  partition e3 values ('广东','福建'));
字串1

  这里说明一下,列表分区不能有maxvalue,当你试图insert列表中不存在值时候,Oracle会拒绝这条纪录(ORA-14400)。

字串7

  怎么样?看出列表分区很有用了吧?上面列出了Oracle9i中使用分区四种方法,其中例子很简单,真正工作中具体使用那种分区方法要参考你具体需求。

字串6

 

  注:例子中使用e1,e2,e3,e4等是分区名称,emp1,emp2,emp3,emp4等是表空间名称


本文来自: (www.91linux.com) 详细出处参考:http://www.91linux.com/html/article/database/oracle/20080619/12672_2.html