PARTITION RANGE分区创建

Oracle的分区技术在处理大量数据时非常有效,尤其体现在range分区上。本文介绍了range分区的概念,通过时间列按月分区作为示例,并详细阐述了如何创建range分区表、查询分区信息、创建global和local索引,以及local索引的高级用法——自定义表空间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Oracle是分区技术的先行者,在oracle7的时候就提出了分区技术,分区技术对于海量数据的存储和高效检索起到了不可或缺的作用。range分区是使用最多的一种分区

1.range分区简介
以列值的取值范围作为分区的划分条件
按照表的时间列对每月的数据进行分区就是一个很好的range分区的例子。
不在指定范围内的记录会被存储在maxvalue所在的分区

 

 

2.创建range分区表所需要的表空间

 

 

 

sec@ora10g> create tablespace tbs_part01 datafile '/oracle/oradata/ora10g/tbs_part01.dbf' size 100m;
sec@ora10g> create tablespace tbs_part02 datafile '/oracle/oradata/ora10g/tbs_part02.dbf' size 100m;
sec@ora10g> create tablespace tbs_part03 datafile '/oracle/oradata/ora10g/tbs_part03.dbf' size 100m;
sec@ora10g> create tablespace tbs_part04 datafile '/oracle/oradata/ora10g/tbs_part04.dbf' size 100m;

 

3.创建分区表

 

sec@ora10g> create table t_partition_range (id number,name varchar2(50))
  partition by range(id)(
  partition t_range_p1   values less than (10)       tablespace tbs_part01,
  partition t_range_p2   values less than (20)       tablespace tbs_part02,
  partition t_range_p3   values less than (30)       tablespace tbs_part03,
  partition t_range_pmax values less than (maxvalue) tablespace tbs_part04);

 

 

Table created.

 

4.查询user_part_tables视图获得“分区的表”的信息

 

sec@ora10g> select table_name,partitioning_type,partition_count from user_part_tables where table_name='T_PARTITION_RANGE';

TABLE_NAME                     PARTITI PARTITION_COUNT
------------------------------ ------- ---------------
T_PARTITION_RANGE              RANGE                 4

 

5.查询user_tab_partitions视图获得“表的分区”信息

 

sec@ora10g> col TABLE_NAME for a20
sec@ora10g> col partition_name for a20
sec@ora10g> col HIGH_VALUE for a10
sec@ora10g> col TABLESPACE_NAME for a15
sec@ora10g> select table_name,partition_name,high_value,tablespace_name from user_tab_partitions where table_name='T_PARTITION_RANGE' order by partition_position;

TABLE_NAME           PARTITION_NAME       HIGH_VALUE TABLESPACE_NAME
-------------------- -------------------- ---------- ---------------
T_PARTITION_RANGE    T_RANGE_P1           10         TBS_PART01
T_PARTITION_RANGE    T_RANGE_P2           20         TBS_PART02
T_PARTITION_RANGE    T_RANGE_P3           30         TBS_PART03
T_PARTITION_RANGE    T_RANGE_PMAX         MAXVALUE   TBS_PART04

 

6.创建global索引range分区

 

sec@ora10g> create index idx_parti_range_id on t_partition_range(id)
  global partition by range(id)(
  partition i_range_p1   values less than (10)       tablespace tbs_part01,
  partition i_range_p2   values less than (40)       tablespace tbs_part02,
  partition i_range_pmax values less than (maxvalue) tablespace tbs_part03);
Index created.

 

7.查询user_part_indexes视图获得“分区的索引”的信息

 

sec@ora10g> select index_name, partitioning_type, partition_count from user_part_indexes where index_name = 'IDX_PARTI_RANGE_ID';

INDEX_NAME                     PARTITI PARTITION_COUNT
------------------------------ ------- ---------------
IDX_PARTI_RANGE_ID             RANGE                 3

 

8.查询user_ind_partitions视图获得“索引的分区”信息

 

sec@ora10g> select index_name,partition_name,high_value,tablespace_name from user_ind_partitions where index_name = 'IDX_PARTI_RANGE_ID' order by partition_position;

INDEX_NAME                     PARTITION_NAME       HIGH_VALUE TABLESPACE_NAME
------------------------------ -------------------- ---------- ---------------
IDX_PARTI_RANGE_ID             I_RANGE_P1           10         TBS_PART01
IDX_PARTI_RANGE_ID             I_RANGE_P2           40         TBS_PART02
IDX_PARTI_RANGE_ID             I_RANGE_PMAX         MAXVALUE   TBS_PART03

 

 

9.创建local索引range分区

 

删除原有的索引

 

sec@ora10g> drop index idx_parti_range_id;

Index dropped.

 

创建

 

sec@ora10g> create index idx_parti_range_id on t_partition_range(id) local;

Index created.

 

查询local索引信息

 

sec@ora10g> select index_name, partitioning_type, partition_count from user_part_indexes where index_name = 'IDX_PARTI_RANGE_ID';

INDEX_NAME                     PARTITI PARTITION_COUNT
------------------------------ ------- ---------------
IDX_PARTI_RANGE_ID             RANGE                 4

sec@ora10g> select index_name,partition_name, high_value, tablespace_name from user_ind_partitions where index_name = 'IDX_PARTI_RANGE_ID' order by partition_position;

INDEX_NAME                     PARTITION_NAME       HIGH_VALUE TABLESPACE_NAME
------------------------------ -------------------- ---------- ---------------
IDX_PARTI_RANGE_ID             T_RANGE_P1           10         TBS_PART01
IDX_PARTI_RANGE_ID             T_RANGE_P2           20         TBS_PART02
IDX_PARTI_RANGE_ID             T_RANGE_P3           30         TBS_PART03
IDX_PARTI_RANGE_ID             T_RANGE_PMAX         MAXVALUE   TBS_PART04

 

 

结论:local索引的分区维护完全依赖于其索引所在表

 

 

10.local索引的较高级的玩法--自定义表空间

 

 

sec@ora10g> drop index idx_parti_range_id;

Index dropped.

sec@ora10g> create index idx_parti_range_id on t_partition_range(id) local (
  2  partition i_range_p1   tablespace tbs_part01,
  3  partition i_range_p2   tablespace tbs_part01,
  4  partition i_range_p3   tablespace tbs_part02,
  5  partition i_range_pmax tablespace tbs_part02
  6  );

Index created.

sec@ora10g> select index_name, partitioning_type, partition_count from user_part_indexes where index_name = 'IDX_PARTI_RANGE_ID';

INDEX_NAME                     PARTITI PARTITION_COUNT
------------------------------ ------- ---------------
IDX_PARTI_RANGE_ID             RANGE                 4

sec@ora10g> select index_name,partition_name, high_value, tablespace_name from user_ind_partitions where index_name = 'IDX_PARTI_RANGE_ID' order by partition_position;

INDEX_NAME                     PARTITION_NAME       HIGH_VALUE TABLESPACE_NAME
------------------------------ -------------------- ---------- ---------------
IDX_PARTI_RANGE_ID             I_RANGE_P1           10         TBS_PART01
IDX_PARTI_RANGE_ID             I_RANGE_P2           20         TBS_PART01
IDX_PARTI_RANGE_ID             I_RANGE_P3           30         TBS_PART02
IDX_PARTI_RANGE_ID             I_RANGE_PMAX         MAXVALUE   TBS_PART02

 

比照上面的结果,这里的表空间已经调整

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值