1. 程式人生 > >Oracle 分割槽表相關語法

Oracle 分割槽表相關語法

Oracle offers six different ways to partition your table data:

  1. range partition
  2. interval partition
  3. hash partition
  4. list partition
  5. reference partition
  6. system partition

range partition

範圍分割槽是第一個被引入到Oracle中的分割槽技術。範圍分割槽技術一般用在準備以時間作為分割槽列的表上。

範圍分割槽一-單一的分割槽鍵值

create table TABLE_RANGE1
(
  as_of_date      DATE
not null, org_unit_id VARCHAR2(32), gl_account_id VARCHAR2(10), iso_currency_cd CHAR(3) ) partition by range (AS_OF_DATE) ( partition P20160720 values less than (TO_DATE(' 2016-07-21 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')), partition P20160721 values less than (TO_DATE(' 2016-07-22 00:00:00'
, 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')) );

範圍分割槽二-多分割槽鍵值

-- Create table
create table TABLE_RANGE2
(
  order_no       NUMBER,
  year_of_order  INTEGER not null,
  month_of_order INTEGER not null,
  day_of_order   INTEGER not null
)
partition by range (YEAR_OF_ORDER, MONTH_OF_ORDER, DAY_OF_ORDER)
(
  partition PART_Q1 values
less than (2016, 4, 1), partition PART_Q2 values less than (2016, 7, 1), partition PART_Q3 values less than (2016, 10, 1), partition PART_Q4 values less than (2017, 1, 1) );

Caution
It is common in range-partition tables to use a catchall partition as the very last one. The last partition will contain values less than a value called maxvalue, which is simply any value higher than the values in the second-to-last partition.
這段話的意思就是說,不管怎麼樣,建議在最大的分割槽上定義一個叫 maxvalue 的分割槽用於兜底,一旦發生超過最大分割槽的資料產生,那麼可以進行使用 maxvalue 分割槽進行儲存資料。

範圍分割槽三-有maxvalue分割槽

create table TABLE_RANGE3
(
  as_of_date      DATE not null,
  org_unit_id     VARCHAR2(32),
  gl_account_id   VARCHAR2(10),
  iso_currency_cd CHAR(3)
)
partition by range (AS_OF_DATE)
(
  partition P20160720 values less than (TO_DATE(' 2016-07-21 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')),
  partition P20160721 values less than (TO_DATE(' 2016-07-22 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')),
  partition pmax values less than (maxvalue)
);

Note that each partition has a specific name and is stored in a separate tablespace.
需要注意的是每一個分割槽都有一個名字,儲存在獨立的空間中。

Interval Partition

Interval partitioning is an extension of the traditional range-partitioning method. In order to implement interval partitioning for a table, you must first specify a minimum of one range partition for that table. Whether you use the minimum singe-range partition or multiple-range partitions, the high value of the range partitioning key is called the transition point. The database automatically creates interval partitions after the data in the table crosses the transition point.

Interval分割槽技術是傳統的範圍分割槽的一種延伸,可以為超過分割槽鍵值的資料自動建立對應的分割槽。但是為了使用Interval分割槽,那麼必須在分割槽表上至少含有一個以上的分割槽。

Interval 分割槽一-根據時間(年)

create table TABLE_INTERVAL1
(
  as_of_date    DATE,
  org_unit_id   VARCHAR2(30),
  gl_account_id VARCHAR2(30)
)
partition by range (AS_OF_DATE)
INTERVAL(NUMTOYMINTERVAL(1, 'YEAR'))
(
  partition P2016 values less than (TO_DATE(' 2017-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
  partition P2017 values less than (TO_DATE(' 2018-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
);

Interval 分割槽二-根據時間(月)

create table TABLE_INTERVAL2
(
  as_of_date    DATE,
  org_unit_id   VARCHAR2(30),
  gl_account_id VARCHAR2(30)
)
partition by range (AS_OF_DATE)
INTERVAL(NUMTOYMINTERVAL(1, 'MONTH'))
(
  partition P201701 values less than (TO_DATE(' 2017-02-01', 'YYYY-MM-DD')),
  partition P201702 values less than (TO_DATE(' 2018-03-01', 'YYYY-MM-DD'))
);

Interval 分割槽三-根據時間(天)

create table TABLE_INTERVAL3
(
  as_of_date    DATE,
  org_unit_id   VARCHAR2(30),
  gl_account_id VARCHAR2(30)
)
partition by range (AS_OF_DATE)
INTERVAL(NUMTODSINTERVAL(1, 'DAY'))
(
  partition P20170101 values less than (TO_DATE(' 2017-01-02', 'YYYY-MM-DD')),
  partition P20170102 values less than (TO_DATE(' 2018-01-03', 'YYYY-MM-DD'))
);

Interval 分割槽四-根據數字

-- Create table
create table TABLE_INTERVAL4
(
  id   INTEGER not null,
  name VARCHAR2(30)
)
partition by range (ID)
INTERVAL(1000)
(
  partition P1 values less than (1000),
  partition P2 values less than (2000)
);

Interval 分割槽五-指定分割槽

create table TABLE_INTERVAL5
(
  id   INTEGER not null,
  name VARCHAR2(20)
)
partition by range (ID)
INTERVAL(1000) STORE IN (REPORT_TS, ETL_TS)
(
  partition P1 values less than (1000),
  partition P2 values less than (2000)
);

Range 分割槽轉換為 Interval 分割槽

ALTER TABLE TABLE_RANGE2 SET INTERVAL(NUMTODSINTERVAL(1, 'DAY'));

Caution
具有maxvalue分割槽的範圍分割槽無法變成Interval分割槽。

Interval 分割槽轉換為 Range 分割槽

ALTER TABLE TABLE_INTERVAL4 SET INTERVAL();

Interval 分割槽新增指定分割槽(新增RAYLEE)

ALTER TABLE TABLE_INTERVAL5 SET STORE IN (REPORT_TS, ETL_TS, RAYLEE);

Interval 分割槽刪除指定分割槽(刪除RAYLEE)

ALTER TABLE TABLE_INTERVAL5 SET STORE IN (REPORT_TS, ETL_TS);

如果要刪除的表空間上已經存在分割槽

ALTER TABLE TABLE_NAME MOVE PARTITION PARTITION_NAME TABLESPACE TARGET_TABLESPACE;

Interval 分割槽注意事項

  • Use the INTERVAL caluse in the CREATE TABLE statement to create an
    interval-partitioned table.
    在建表語句中使用INTERVAL關鍵字建立Interval 分割槽

  • Specify at least one range partition using the partition clause,
    before specifying your interval partitions.
    建立Interval 分割槽最少使用存在一個範圍分割槽

  • The partitioning key must be of the NUMBER or DATE type.
    分割槽的列必須是 NUMBER 或者 DATE 型別

  • You can optionally specify the tablespaces for the partition data by
    including the STORE IN clause in the CREATE TABLE statement.
    可以選擇是否在建表語句中使用 STORE IN 子句用於指定表空間

HASH 分割槽

Use the hash-partitioning, all you have to do is decide on the number of partitions, and Oracle’s hashing algorithms will assign a hash value to each row’s partitioning key and place it in the appropriate partition.

對於 HASH 分割槽,你所需要做的就是決定要 hash 成多少個分割槽,Oracle的 hash 分割槽演算法將會給每一個行的分割槽列的鍵值一個hash 值,並把它放置到對應的分割槽去

HASH 分割槽程式碼

CREATE TABLE TABLE_HASH
(   ID INT NOT NULL,
    YEAR_OF_ORDER INT NOT NULL,
    MONTH_OF_ORDER INT NOT NULL,
    DAY_OF_ORDER INT NOT NULL)
PARTITION BY HASH(ID)
PARTITIONS 2
STORE IN (REPORT_TS, ETL_TS);

LIST 分割槽

對於非連續型的分割槽的一種補充使用 LIST 分割槽。

LIST 分割槽程式碼-使用預設分割槽

-- Create table
create table TABLE_LIST1
(
  alpha_id CHAR(1),
  name     VARCHAR2(20)
)
partition by list (ALPHA_ID)
(
  partition P_AG values ('A', 'B', 'C', 'D', 'E', 'F', 'G'),
  partition P_H values ('H', 'I', 'J', 'K', 'L', 'M', 'N')
);

LIST 分割槽程式碼-指定分割槽

create table TABLE_LIST2
(
  alpha_id CHAR(1),
  name     VARCHAR2(20)
)
partition by list (ALPHA_ID)
(
  partition P_AG values ('A', 'B', 'C', 'D', 'E', 'F', 'G') 
  TABLESPACE REPORT_TS,
  partition P_H values ('H', 'I', 'J', 'K', 'L', 'M', 'N') 
  TABLESPACE ETL_TS
);

Reference 分割槽(未完成)