實驗】【PARTITION】RANGE分割槽建立【轉】

47328983發表於2011-04-25
前言:可以說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 --


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/241379/viewspace-693644/,如需轉載,請註明出處,否則將追究法律責任。

相關文章