您的位置:首页 > 其它

【实验】【PARTITION】RANGE分区创建

2009-10-29 13:47 393 查看
前言:可以说Oracle是分区技术的先行者,在oracle7的时候就提出了分区技术,而且在最新版本的11g中分区的类型又有所加强(如复合索引的组合方式增多了),分区技术对于海量数据的存储和高效检索起到了不可或缺的作用。range分区是使用最多的一种分区,这个小文儿将进行一些关于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))
2 partition by range(id)(
3 partition t_range_p1 values less than (10) tablespace tbs_part01,
4 partition t_range_p2 values less than (20) tablespace tbs_part02,
5 partition t_range_p3 values less than (30) tablespace tbs_part03,
6 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)
2 global partition by range(id)(
3 partition i_range_p1 values less than (10) tablespace tbs_part01,
4 partition i_range_p2 values less than (40) tablespace tbs_part02,
5 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

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

-- The End --
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: