Oracle分区表

缺乏、安全感 2022-09-28 13:22 357阅读 0赞

作用:将在张大表的数据分布到多个表分区段,不同分区彼此独立,从而提高了表的可用性和性能

种类:范围分区,散列分区(使用HASH算法,最常使用),列表分区,范围/散列组合分区,范围/列表组合分区

范围分区表

创建范围分区表

  1. create table t(v number,b number)
  2. partition by range(v) (
  3. partition p1 values less than ('11') tablespace test1,
  4. partition p2 values less than ('21') tablespace test2);

增加与删除分区

#增加分区

  1. alter table t add partition p3 values less than ('31') tablespace test3;
  2. alter table t drop partition p3

一个时间分区的例子

  1. alter session set nls_data_lanage=AMERICAN;
  2. alter session set nls_data_format='DD-MON-YYYY'
  3. create table t(v_date date,b number)
  4. partition by range(v_date)(
  5. partition p1 values less than ('01-APR-2009') tablespace test1,
  6. partition p2 values less than ('01-JUN-2009') tablespace test2);

2.散列分区表(最常用)

创建

  1. create table t1(
  2. v number,b number)
  3. partition by hash(v)
  4. (partition p1 tablespace test1,
  5. partition p2 tablespace test2);

增加分区

  1. alter table t add partition p3 tablespace test3;

删除分区

  1. alter table t drop coalesce partition;

3.列表分区

建列表分区

  1. create table t(
  2. v varchar2(10),
  3. b number
  4. )partition by list(v)
  5. (partition p1 values('a','b') tablespace test1,
  6. partition p2 values('c','d') tablespace test2);

#插入数据

  1. SQL> insert into t values('a',10);
  2. SQL> insert into t values('d',20);

#注意,插入数据时第一个字段只能为a,b,c,d

  1. SQL> insert into t values('f',30);
  2. ERROR at line 1:
  3. ORA-14400: inserted partition key does not map to any partition

#查询

  1. select * from t;
  2. select * from t partition(p1);
  3. select * from t partition(p2);
  4. select * from t where v=XXX

增加分区

  1. alter table t add partition p3 values('31','32') tablespace test3;

删除分区

  1. alter table t drop partition p3

4.范围/散列组合分区

建立散列组合分区

  1. create table t(
  2. v number,b number)
  3. partition by range(v)
  4. subpartition by hash(b) subpartitions 2
  5. store in (test1,test2)(
  6. partition p1 values less than ('11'),
  7. partition p2 values less than ('21'));

查询

  1. select * from t;
  2. select * from t partition(p1);
  3. select * from t where ....

增加主分区和子分区

  1. alter table t add partition p3 values less than ('31') tablespace test3;
  2. alter table t modify partition p3 add subpartition;

删除分区

  1. alter table t coalesce partition;
  2. alter table t modify partition p1 coalesce subpartition;

5.范围/列表组合分区

创建

  1. create table t(
  2. v number,b number)
  3. partition by range(v)
  4. subpartition by list(b)
  5. (partition p1 values less than ('11') tablespace test1(
  6. subpartition p1_1 values('1','3'),
  7. subpartition p1_2 values('5','6')
  8. ),
  9. partition p2 values less than ('21') tablespace test2(
  10. subpartition p2_1 values('13','14'),
  11. subpartition p2_2 values('15','16')
  12. ));

查询

  1. select * from t
  2. select * from t partition(p1)
  3. select * from t subpartition(p1_1)
  4. select * from t where .....
  5. select segment_name,partition_name,tablespace_name
  6. from user_segments where segment_name='T';

增加分区和子分区

  1. alter table t add partition p3 values less than ('31') tablespace test3(
  2. subpartition p3_1 values('25','26'),
  3. subpartition p3_2 values('22','23'));
  4. alter table t modify partition r3
  5. add subpartition r3_3 tablespace test3 values('28','29');

删除分区

  1. alter table t modify partition p1 coalesce subpartition;

其它设置

  1. 交换分区数据
  2. alter table t exchange partition p1 with table tt;
  3. 载断分区
  4. alter table t truncate partition p1;
  5. 修改分区名
  6. alter table t rename partition p2_1 to p2;
  7. 合并分区
  8. alter table t merge partitions p1,p2 into partition p01
  9. 重组分区
  10. alter table t move partition p1 tablespace test04
  11. 为列表分区和子分区加值
  12. alter table t modify partition p1 add values('111');
  13. alter table t modify subpartition p3_1 add values('111');
  14. 从列表分区和子分区中删除值
  15. alter table t modify partition p1 drop values('111')
  16. alter table t modify subpartition p3_1 drop values('111')

分区表常用的数据字典

  1. 分区表信息: dba_part_tables
  2. 显示分区: dba_tab_partitions
  3. 显示子分区: dba_tab_subpartitions
  4. 显示分区列: dba_part_key_columns
  5. 显示子分区列:dba_subpart_dey_columns
  6. 显示分区索引:dba_part_indexes
  7. 显示索引分区:dba_ind_partitions

发表评论

表情:
评论列表 (有 0 条评论,357人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Oracle分区

    作用:将在张大表的数据分布到多个表分区段,不同分区彼此独立,从而提高了表的可用性和性能 种类:范围分区,散列分区(使用HASH算法,最常使用),列表分区,范围/散列组合分区,

    相关 oracle 分区

    oracle大数据表建分区优缺点 oracle给出的建议是按照表的大小给出的,10g的建议是2G,也就是说表的大小大于2G,那么就最好建立分区。 为什么要建立分区?主

    相关 Oracle分区

    废话少说,直接讲分区语法。 Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区。 一:范围分区 就是根据数据库表中某一字段的值的范围来划分分区