爲了簡化數據庫大表的管理,例如在數據倉庫中一般都是TB級的數量級.ORACLE8以後推出了分區選項.分區將表分離在若於不同的表空間上,用分而治之的方法來支撐元限膨脹的大表,組大表在物理一級的可管理性.將大表分割成較小的分區可以改善表的維護、備份、恢復、事務及查詢性能。
分區的優點:
1、 增強可用性:如果表的一個分區由於系統故障而不能使用,表的其餘好的分區仍可以使用;
2、 減少關閉時間:如果系統故障隻影響表的一部份分區,那麼只有這部份分區需要修復,礦能比整個大表修復花的時間更少;
3、 維護輕鬆:如果需要得建表,獨產管理每個公區比管理單個大表要輕鬆得多;
4、 均衡I/O:可以把表的不同分區分配到不同的磁盤來平衡I/O改善性能;
5、 改善性能:對大表的查詢、增加、修改等操作可以分解到表的不同分區來並行執行,可使運行速度更快,在數據倉庫的TP查詢特別有用。
6、 分區對用戶透明,最終用戶感覺不到分區的存在。
create tablespace dw1
datafile 'D:/oracle/oradata/ora9/dw11.ora' size 50M
create tablespace dw2
datafile 'D:/oracle/oradata/ora9/dw21.ora' size 50M
一、按範圍分區:固名思義就是按一定range來分區,看下面的例子:
SQL> set linesize 1000
SQL> create table niegc_part
2 (
3 part_id integer primary key,
4 part_date date,
5 part_dec varchar2(100)
6 )
7 partition by range(part_date)
8 (
9 partition part_01 values less than(to_date('2006-01-01','yyyy-mm-dd')) tablespace dw1,
10 partition part_02 values less than(to_date('2007-01-01','yyyy-mm-dd')) tablespace dw2,
11 partition part_03 values less than(maxvalue) tablespace dw1
12 );
表已創建。
SQL>
SQL> insert into niegc_part values(1,to_date('2005-12-30','yyyy-mm-dd'),'less 20
06-01-01');
已創建 1 行。
SQL> commit;
提交完成。
SQL> insert into niegc_part values(2,to_date('2006-01-01','yyyy-mm-dd'),'equal 2
007-01-01');
已創建 1 行。
SQL> commit;
提交完成。
SQL> insert into niegc_part values(3,sysdate,'sysdate');
已創建 1 行。
SQL> commit;
提交完成。
SQL>
SQL>
SQL> select * from niegc_part partition(part_01);
PART_ID PART_DATE PART_DEC
---------- ---------- ----------------------------------------------------------
------------------------------------------
1 30-12月-05 less 2006-01-01
SQL>
相信只要對oracle 有點熟,都能知道上面的range分區的意思了.
兩個字段以上的range分區大同小異,請看下面的例子:
create table niegc_part
(
part_id integer primary key,
part_date date,
part_dec varchar2(100)
)
partition by range(part_id,part_date)
(
partition part_01 values less than(1,to_date('2006-01-01','yyyy-mm-dd')) tablespace dw,
partition part_02 values less than(10,to_date('2007-01-01','yyyy-mm-dd')) tablespace dw,
partition part_03 values less than(maxvalue,maxvalue) tablespace dw
);
二、Hash分區(散列分區)。 散列分區通過指定分區編號來均勻分佈數據的一種分區類型,因爲通過在I/O設備上進行散列分區,使行這些分區大小一致。如將part_id的數據根據自身的情況散列地存放在指定的三個表空間中:
create table niegc_part
(
part_id integer primary key,
part_date date,
part_dec varchar2(100)
)
partition by hash(part_id)
(
partition part_01 tablespace dw1,
partition part_02 tablespace dw2
);
系統將按part_id將記錄散列地插入三個分區中,這裏也就是二個不同的表空間中。
三、複合分區。根據範圍分區後,每個分區內的數據再散列地分佈在幾個表空間中,這樣我們就要使用複合分區。複合分區是先使用範圍分區,然後在每個分區同再使用散列分區的一種分區方法,如將part_date的記錄按時間分區,然後每個分區中的數據分三個子分區,將數據散列地存儲在三個指定的表空間中:
create table niegc_part
(
part_id integer primary key,
part_date date,
part_dec varchar2(100)
)
partition by range(part_date) subpartition by hash(part_id)
subpartitions 2 store in(dw1,dw2)
(
partition part_01 values less than(to_date('2006-01-01','yyyy-mm-dd')) tablespace dw1,
partition part_02 values less than(to_date('2007-01-01','yyyy-mm-dd')) tablespace dw2,
partition part_03 values less than(maxvalue) tablespace dw1
);
先根據part_date進行範圍分區,然後根據交易的ID將記錄散列地存儲在二個表空間中。
四、索引分區:
注意: 對某個字段已做了分區了,是不允許再建立索引分區的。這一點要非常注意。
全局索引建立時global子句允許指定索引的範圍值,這個範圍值爲索引字段的範圍值:
create index idx_part_id on niegc_part(part_dec)
global partition by range(part_dec)
(
partition idx_1 values less than('1000') tablespace dw,
partition idx_2 values less than(maxvalue) tablespace dw
)
局部索引分區的建立:(注意:表必須存在分區,此分區的個數必須和分區表的分區個數一樣,不然是建立不起來的)
create index idx_part_id on niegc_part(part_dec)
local
(
partition idx_1 tablespace dw1,
partition idx_2 tablespace dw2
)
五、分區維護:(只對範圍分區)
(1)、增加一個分區:分區範圍只能往上增,不能增加一個少於原有的分區:
alter table niegc_part add partition part_03 values less than(maxvalue)
(2)、合併分區:(合併後的分區必須指下最後一個大value的分區)
alter table niegc_part merge partitions part_02,part_03 into partition part_03
(3)、刪除一個分區:
alter table niegc_part drop partition part_01
六、總結:
需要說明的是,本文在舉例說名分區表事務操作的時候,都指定了分區,因爲指定了分區,系統在執行的時候則只操作該分區的記錄,提高了數據處理的速度。不要指定分區直接操作數據也是可以的。在分區表上建索引及多索引的使用和非分區表一樣。此外,因爲在維護分區的時候可能對分區的索引會產生一定的影響,可能需要在維護之後重建索引,相關內容請google分區表索引部分的文檔
Oracle分區命令集
-- Create table(創建分區表)
create table BILL_MONTHFEE_ZERO
(
SERV_ID NUMBER(20) not null,
BILLING_CYCLE_MONTH NUMBER(6) not null,
DATE_TYPE NUMBER(1),
ACC_NBR VARCHAR2(80)
)
partition by range (BILLING_CYCLE_MONTH)
(partition p_200407 values less than (200407)
tablespace TS_ZIKEN
storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0),
partition p_200408 values less than (200408)
tablespace TS_ZIKEN
storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0))
;
create index idx_bill_monthfee_zero_idx01 on bill_monthfee_zero(billing_cycle_month)
tablespace TS_ZIKEN_idx
storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0) nologging;
grant all on bill_monthfee_zero to dxsq_dev;
--增加分區表
alter table BILL_MONTHFEE_ZERO add Partition p_200409
values less than (200409) tablespace ts_ziken;
--刪除一分區
alter table part_tbl drop Partition part_tbl_08;
--將一個分區分爲兩個分區
alter table bill_monthfee_zero split Partition p_200409 at (200409)
into (Partition p_200409_1 tablespace ts_ziken,
Partition p_200409_2 tablespace ts_ziken_idx);
--合併分區
ALTERTABLE bill_monthfee_zero
MERGE PARTITIONS p_200408, p_200409 INTOPARTITION p_all
--將分區改名
altertable bill_monthfee_zero rename Partition p_200408 to p_fee_200408
--將分區改表空間
altertable bill_monthfee_zero move Partition p_200409
tablespace ts_ziken_01 nologging
--查詢特定分區
select count(*) from BILL_MONTHFEE_ZERO partition (p_200407);
--添加數據
insert into bill_monthfee_zero select * from bill_monthfee_zero partition (p_200407)
--分區表的導出
userid=dxsq/teledoone@jndxsq154
buffer=102400
tables=bill_monthfee:P_200401,
file=E:"exp_para"exp_dxsq_tables.dmp
log=E:"exp_para"exp_dxsq_tables.log
技巧:
刪除表中一個字段:
alter table bill_monthfee_zero set unused column date_type;
添加一個字段:alter table bill_monthfee_zero add date_type number(1);
顯示分區表信息
顯示當前用戶可訪問的所有分區表信息:ALL_PART_TABLES
顯示當前用戶所有分區表的信息:USER_PART_TABLES
顯示錶分區信息 顯示數據庫所有分區表的詳細分區信息:DBA_TAB_PARTITIONS
顯示當前用戶可訪問的所有分區表的詳細分區信息:ALL_TAB_PARTITIONS
顯示當前用戶所有分區表的詳細分區信息:USER_TAB_PARTITIONS
顯示子分區信息 顯示數據庫所有組合分區表的子分區信息:DBA_TAB_SUBPARTITIONS
顯示當前用戶可訪問的所有組合分區表的子分區信息:ALL_TAB_SUBPARTITIONS
顯示當前用戶所有組合分區表的子分區信息:USER_TAB_SUBPARTITIONS
顯示分區列 顯示數據庫所有分區表的分區列信息:DBA_PART_KEY_COLUMNS
顯示當前用戶可訪問的所有分區表的分區列信息:ALL_PART_KEY_COLUMNS
顯示當前用戶所有分區表的分區列信息:USER_PART_KEY_COLUMNS
顯示子分區列 顯示數據庫所有分區表的子分區列信息:DBA_SUBPART_KEY_COLUMNS
顯示當前用戶可訪問的所有分區表的子分區列信息:ALL_SUBPART_KEY_COLUMNS
顯示當前用戶所有分區表的子分區列信息:USER_SUBPART_KEY_COLUMNS
oracle分區表
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.