為了簡化數據庫大表的管理.ORACLE8以后推出了分區選項.分區可將表分離在不同的表空間上,用分而治之的方法來支撐元限膨脹的大表.將大表分割成較小的分區可以改善表的維護、備份、恢復、事務及查詢性能。
分區的優點:
1、 增強可用性:如果表的一個分區由於系統故障而不能使用,表的其余好的分區仍可以使用;
2、 減少修復時間:如果系統故障只影響表的一部份分區,那么只有這部份分區需要修復,可能比整個大表修復花的時間更少;
3、 維護輕松:管理每個公區比管理單個大表要輕松得多;
4、 均衡I/O:可以把表的不同分區分配到不同的磁盤來平衡I/O改善性能;
5、 改善性能:對大表的查詢、增加、修改等操作可以分解到表的不同分區來並行執行,可使運行速度更快。
6、 分區對用戶透明,最終用戶感覺不到分區的存在。
創建測試表空間:
1 SQL> CREATE TABLESPACE wf1 DATAFILE '/disk4/ora10/oradata/ora1/wf1.dbf' SIZE 10M 2 2 AUTOEXTEND ON NEXT 1M MAXSIZE 15M 3 3 EXTENT MANAGEMENT LOCAL UNIFORM SIZE 128K; 4 5 Tablespace created. 6 7 SQL> CREATE TABLESPACE wf2 DATAFILE '/disk4/ora10/oradata/ora1/wf2.dbf' SIZE 10M 8 2 AUTOEXTEND ON NEXT 1M MAXSIZE 15M 9 3 EXTENT MANAGEMENT LOCAL UNIFORM SIZE 128K; 10 11 Tablespace created. 12 13 SQL>
一、按范圍分區:固名思義就是按一定range來分區,看下面的例子:
1 SQL> create table wf_part 2 2 ( 3 3 wf_id integer primary key, 4 4 wf_date date, 5 5 wf_dec varchar2(50) 6 6 )TABLESPACE userdata 7 7 partition by range(wf_date)( 8 8 partition part_01 values less than(to_date('2006-01-01','yyyy-mm-dd')) tablespace wf1, 9 9 partition part_02 values less than(to_date('2007-01-01','yyyy-mm-dd')) tablespace wf2, 10 10 partition part_03 values less than(maxvalue) tablespace userdata); 11 12 Table created. 13 14 SQL>
注意:這里將表,和表分區建立在了不同的表空間中。
分別向三個分區插入數據:
1 SQL> insert into wf_part values(1,to_date('2005-12-30','yyyy-mm-dd'),'less 2006-01-01'); 2 3 1 row created. 4 5 SQL> commit; 6 7 Commit complete. 8 9 SQL> insert into wf_part values(2,to_date('2006-01-01','yyyy-mm-dd'),'equal 2007-01-01'); 10 11 1 row created. 12 13 SQL> insert into wf_part values(3,sysdate,'sysdate'); 14 15 1 row created. 16 17 SQL> commit; 18 19 Commit complete. 20 21 SQL>
做查詢:
1 SQL> select * from wf_part partition(part_01); 2 3 WF_ID WF_DATE WF_DEC 4 ---------- --------- -------------------------------------------------- 5 1 30-DEC-05 less 2006-01-01 6 7 SQL> select * from wf_part partition(part_02); 8 9 WF_ID WF_DATE WF_DEC 10 ---------- --------- -------------------------------------------------- 11 2 01-JAN-06 equal 2007-01-01 12 13 SQL> select * from wf_part partition(part_03); 14 15 WF_ID WF_DATE WF_DEC 16 ---------- --------- -------------------------------------------------- 17 3 19-MAR-12 sysdate 18 19 SQL>
二、索引分區:
局部索引分區的建立:(注意:表必須存在分區,此分區的個數必須和分區表的分區個數一樣,不然是建立不起來的)
1 SQL>create index idx_part_id on wf_part(wf_dec) local ( 2 2 partition idx_part_id01 tablespace userdata, 3 3 partition idx_part_id02 tablespace userdata 4 4 ); 5 create index idx_part_id on wf_part(wf_dec) local ( 6 * 7 ERROR at line 1: 8 ORA-14024: number of partitions of LOCAL index must equal that of the 9 underlying table
以分區字段建立索引
1 SQL> CREATE INDEX idx_part_id on wf_part(wf_date) LOCAL 2 2 ( 3 3 partition idx_part_id01 tablespace userdata, 4 4 partition idx_part_id02 tablespace userdata, 5 5 partition idx_part_id03 tablespace userdata 6 6 ); 7 8 Index created. 9 10 SQL> select owner,index_name,table_name from dba_part_indexes where table_name 11 2 ='WF_PART'; 12 13 OWNER INDEX_NAME TABLE_NAME 14 -------------- --------------- ------------------------------ 15 SYS IDX_PART_ID WF_PART
不指定指定索引的分區:
1 SQL> drop index IDX_PART_ID; 2 3 Index dropped. 4 5 SQL> CREATE INDEX idx_part_id ON wf_part(wf_date) LOCAL; 6 7 Index created. 8 9 SQL>select owner,index_name,table_name from dba_part_indexes where table_name ='WF_PART'; 10 11 OWNER INDEX_NAME TABLE_NAME 12 -------------- --------------- ------------------------------ 13 SYS IDX_PART_ID WF_PART
這個操作要求較大的臨時表空間和排序區。(未理解)
三.分區維護:(只對范圍分區)
(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
四、總結:
分區表是將大表的數據分成稱為分區的許多小的子集,提供四種分區方法:列表分區,范圍分區,哈希分區和混合分區;