亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

oracle_分區(qū)表的新增、修改、刪除、合并。普通

系統(tǒng) 1848 0

一. 分區(qū)表理論知識
Oracle提供了分區(qū)技術(shù)以支持VLDB(Very Large DataBase)。分區(qū)表通過對分區(qū)列的判斷,把分區(qū)列不同的記錄,放到不同的分區(qū)中。分區(qū)完全對應(yīng)用透明。
Oracle的分區(qū)表可以包括多個分區(qū),每個分區(qū)都是一個獨(dú)立的段(SEGMENT),可以存放到不同的表空間中。查詢時可以通過查詢表來訪問各個分區(qū)中的數(shù)據(jù),也可以通過在查詢時直接指定分區(qū)的方法來進(jìn)行查詢。
When to Partition a Table什么時候需要分區(qū)表,官網(wǎng)的2個建議如下:
(1)Tables greater than 2GB should always be considered for partitioning.
(2)Tables containing historical data, in which new data is added into the newest partition. A typical example is a historical table where only the current month's data is updatable and the other 11 months are read only.
在oracle 10g中最多支持:1024k-1個分區(qū):
Tables can be partitioned into up to 1024K-1 separate partitions
聯(lián)機(jī)文檔上有關(guān)分區(qū)表和索引的說明:
Partitioned Tables and Indexes
http://download.oracle.com/docs/cd/B19306_01/server.102/b14220/partconc.htm#sthref2604
分區(qū)提供以下優(yōu)點(diǎn):
(1)由于將數(shù)據(jù)分散到各個分區(qū)中,減少了數(shù)據(jù)損壞的可能性;
(2)可以對單獨(dú)的分區(qū)進(jìn)行備份和恢復(fù);
(3)可以將分區(qū)映射到不同的物理磁盤上,來分散IO;
(4)提高可管理性、可用性和性能。
Oracle 10g提供了以下幾種分區(qū)類型:
(1)范圍分區(qū)(range);
(2)哈希分區(qū)(hash);
(3)列表分區(qū)(list);
(4)范圍-哈希復(fù)合分區(qū)(range-hash);
(5)范圍-列表復(fù)合分區(qū)(range-list)。
Range分區(qū):
  Range分區(qū)是應(yīng)用范圍比較廣的表分區(qū)方式,它是以列的值的范圍來做為分區(qū)的劃分條件,將記錄存放到列值所在的range分區(qū)中。
如按照時間劃分,2010年1月的數(shù)據(jù)放到a分區(qū),2月的數(shù)據(jù)放到b分區(qū),在創(chuàng)建的時候,需要指定基于的列,以及分區(qū)的范圍值。
在按時間分區(qū)時,如果某些記錄暫無法預(yù)測范圍,可以創(chuàng)建maxvalue分區(qū),所有不在指定范圍內(nèi)的記錄都會被存儲到maxvalue所在分區(qū)中。
如:
create table pdba (id number, time date) partition by range (time)
(
partition p1 values less than (to_date('2010-10-1', 'yyyy-mm-dd')),
partition p2 values less than (to_date('2010-11-1', 'yyyy-mm-dd')),
partition p3 values less than (to_date('2010-12-1', 'yyyy-mm-dd')),
partition p4 values less than (maxvalue)
)
Hash分區(qū):
  對于那些無法有效劃分范圍的表,可以使用hash分區(qū),這樣對于提高性能還是會有一定的幫助。hash分區(qū)會將表中的數(shù)據(jù)平均分配到你指定的幾個分區(qū)中,列所在分區(qū)是依據(jù)分區(qū)列的hash值自動分配,因此你并不能控制也不知道哪條記錄會被放到哪個分區(qū)中,hash分區(qū)也可以支持多個依賴列。
如:
create table test
(
transaction_id number primary key,
item_id number(8) not null
)
partition by hash(transaction_id)
(
partition part_01 tablespace tablespace01,
partition part_02 tablespace tablespace02,
partition part_03 tablespace tablespace03
);
在這里,我們指定了每個分區(qū)的表空間。
List分區(qū):
  List分區(qū)也需要指定列的值,其分區(qū)值必須明確指定,該分區(qū)列只能有一個,不能像range或者h(yuǎn)ash分區(qū)那樣同時指定多個列做為分區(qū)依賴列,但它的單個分區(qū)對應(yīng)值可以是多個。
  在分區(qū)時必須確定分區(qū)列可能存在的值,一旦插入的列值不在分區(qū)范圍內(nèi),則插入/更新就會失敗,因此通常建議使用list分區(qū)時,要創(chuàng)建一個default分區(qū)存儲那些不在指定范圍內(nèi)的記錄,類似range分區(qū)中的maxvalue分區(qū)。
在根據(jù)某字段,如城市代碼分區(qū)時,可以指定default,把非分區(qū)規(guī)則的數(shù)據(jù),全部放到這個default分區(qū)。
如:
create table custaddr
(
id varchar2(15 byte) not null,
areacode varchar2(4 byte)
)
partition by list (areacode)
( partition t_list025 values ('025'),
partition t_list372 values ('372') ,
partition t_list510 values ('510'),
partition p_other values (default)
)
組合分區(qū):
如果某表按照某列分區(qū)之后,仍然較大,或者是一些其它的需求,還可以通過分區(qū)內(nèi)再建子分區(qū)的方式將分區(qū)再分區(qū),即組合分區(qū)的方式。
  組合分區(qū)呢在10g中有兩種:range-hash,range-list。注意順序,根分區(qū)只能是range分區(qū),子分區(qū)可以是hash分區(qū)或list分區(qū)。
如:
create table test
(
transaction_id number primary key,
transaction_date date
)
partition by range(transaction_date) subpartition by hash(transaction_id)
subpartitions 3 store in (tablespace01,tablespace02,tablespace03)
(
partition part_01 values less than(to_date(’2009-01-01’,’yyyy-mm-dd’)),
partition part_02 values less than(to_date(’2010-01-01’,’yyyy-mm-dd’)),
partition part_03 values less than(maxvalue)
);
create table emp_sub_template (deptno number, empname varchar(32), grade number)
partition by range(deptno) subpartition by hash(empname)
subpartition template
(subpartition a tablespace ts1,
subpartition b tablespace ts2,
subpartition c tablespace ts3,
subpartition d tablespace ts4
)
(partition p1 values less than (1000),
partition p2 values less than (2000),
partition p3 values less than (maxvalue)
);
create table quarterly_regional_sales
(deptno number, item_no varchar2(20),
txn_date date, txn_amount number, state varchar2(2))
tablespace ts4
partition by range (txn_date)
subpartition by list (state)
(partition q1_1999 values less than (to_date('1-apr-1999','dd-mon-yyyy'))
(subpartition q1_1999_northwest values ('or', 'wa'),
subpartition q1_1999_southwest values ('az', 'ut', 'nm'),
subpartition q1_1999_northeast values ('ny', 'vm', 'nj'),
subpartition q1_1999_southeast values ('fl', 'ga'),
subpartition q1_1999_northcentral values ('sd', 'wi'),
subpartition q1_1999_southcentral values ('ok', 'tx')
),
partition q2_1999 values less than ( to_date('1-jul-1999','dd-mon-yyyy'))
(subpartition q2_1999_northwest values ('or', 'wa'),
subpartition q2_1999_southwest values ('az', 'ut', 'nm'),
subpartition q2_1999_northeast values ('ny', 'vm', 'nj'),
subpartition q2_1999_southeast values ('fl', 'ga'),
subpartition q2_1999_northcentral values ('sd', 'wi'),
subpartition q2_1999_southcentral values ('ok', 'tx')
),
partition q3_1999 values less than (to_date('1-oct-1999','dd-mon-yyyy'))
(subpartition q3_1999_northwest values ('or', 'wa'),
subpartition q3_1999_southwest values ('az', 'ut', 'nm'),
subpartition q3_1999_northeast values ('ny', 'vm', 'nj'),
subpartition q3_1999_southeast values ('fl', 'ga'),
subpartition q3_1999_northcentral values ('sd', 'wi'),
subpartition q3_1999_southcentral values ('ok', 'tx')
),
partition q4_1999 values less than ( to_date('1-jan-2000','dd-mon-yyyy'))
(subpartition q4_1999_northwest values ('or', 'wa'),
subpartition q4_1999_southwest values ('az', 'ut', 'nm'),
subpartition q4_1999_northeast values ('ny', 'vm', 'nj'),
subpartition q4_1999_southeast values ('fl', 'ga'),
subpartition q4_1999_northcentral values ('sd', 'wi'),
subpartition q4_1999_southcentral values ('ok', 'tx')
)
);
在Oracle 11g中,組合分區(qū)功能這塊有所增強(qiáng),又增加了range-range,list-range,
list-list,list-hash,并且11g里面還支持Interval分區(qū)和虛擬列分區(qū)。
這塊可以參考Blog:
Oracle 11g 新特性簡介
http://blog.csdn.net/tianlesoftware/archive/2010/01/06/5134819.aspx
分區(qū)表 之 Interval分區(qū) 和 虛擬列 按星期分區(qū)表
http://blog.csdn.net/tianlesoftware/archive/2010/06/10/5662337.aspx
二. 普通表轉(zhuǎn)分區(qū)表方法
將普通表轉(zhuǎn)換成分區(qū)表有4種方法:
1. Export/import method
2. Insert with a subquery method
3. Partition exchange method
4. DBMS_REDEFINITION
具體參考:
How to Partition a Non-partitioned Table [ID 1070693.6]
http://blog.csdn.net/tianlesoftware/archive/2011/03/02/6218704.aspx
邏輯導(dǎo)出導(dǎo)入這里就不做說明,我們看看其他三種方法。
2.1 插入: Insert with a subquery method
這種方法就是使用insert 來實(shí)現(xiàn)。當(dāng)然在創(chuàng)建分區(qū)表的時候可以一起插入數(shù)據(jù),也可以創(chuàng)建好后在insert 進(jìn)去。這種方法采用DDL語句,不產(chǎn)生UNDO,只產(chǎn)生少量REDO,建表完成后數(shù)據(jù)已經(jīng)在分布到各個分區(qū)中。
SQL> select count(*) from dba;
COUNT(*)
----------
2713235
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
會話已更改。
SQL> select time_fee from dba where rownum<5;
TIME_FEE
-------------------
2011-02-17 19:29:09
2011-02-17 19:29:15
2011-02-17 19:29:18
2011-02-17 19:29:20
SQL>
2.1.1 Oracle 11g的Interval
在11g里的Interval創(chuàng)建,這種方法對沒有寫全的分區(qū)會自動創(chuàng)建。比如我這里只寫了1月日期,如果插入的數(shù)據(jù)有其他月份的,會自動生成對應(yīng)的分區(qū)。
/* Formatted on 2011/03/02 15:41:09 (QP5 v5.115.810.9015) */
CREATETABLE intervaldave
PARTITIONBYRANGE(time_fee)
INTERVAL(NUMTOYMINTERVAL(1,'MONTH'))
(PARTITION part1
VALUESLESSTHAN(TO_DATE('01/12/2010','MM/DD/YYYY')))
AS
SELECT ID, TIME_FEE FROMDAVE;
SQL> select table_name,partition_name from user_tab_partitions where table_name='INTERVALDAVE';
TABLE_NAME PARTITION_NAME
------------------------------ ------------------------------
INTERVALDAVE PART1
INTERVALDAVE SYS_P24
INTERVALDAVE SYS_P25
INTERVALDAVE SYS_P26
INTERVALDAVE SYS_P33
INTERVALDAVE SYS_P27
INTERVALDAVE SYS_P28
2.1.2 Oracle 10g 版本
在10g里面,我需要寫全所有的分區(qū)。
sql> create table pdba (id, time) partition by range (time)
2 (partition p1 values less than (to_date('2010-10-1', 'yyyy-mm-dd')),
3partition p2 values less than (to_date('2010-11-1', 'yyyy-mm-dd')),
4partition p3 values less than (to_date('2010-12-1', 'yyyy-mm-dd')),
5partition p4 values less than (maxvalue))
6as select id, time_fee from dba;
表已創(chuàng)建。
SQL> select table_name,partition_name from user_tab_partitions where table_name='PDBA';
TABLE_NAME PARTITION_NAME
------------------------------ ------------------------------
PDBA P1
PDBA P2
PDBA P3
PDBA P4
sql> select count(*) from pdba partition (p1);
count(*)
----------
1718285
sql> select count(*) from pdba partition (p2);
count(*)
----------
183667
sql> select count(*) from pdba partition (p3);
count(*)
----------
188701
sql> select count(*) from pdba partition (p4);
count(*)
----------
622582
sql>
現(xiàn)在分區(qū)表已經(jīng)建好了,但是表名不一樣,需要用rename對表重命名一下:
SQL> rename dba to dba_old;
表已重命名。
SQL> rename pdba to dba;
表已重命名。
SQL> select table_name,partition_name from user_tab_partitions where table_name='DBA';
TABLE_NAME PARTITION_NAME
------------------------------ ------------------------------
DBA P1
DBA P2
DBA P3
DBA P4
2.2 . 交換分區(qū):Partition exchange method
這種方法只是對數(shù)據(jù)字典中分區(qū)和表的定義進(jìn)行了修改,沒有數(shù)據(jù)的修改或復(fù)制,效率最高。適用于包含大數(shù)據(jù)量的表轉(zhuǎn)到分區(qū)表中的一個分區(qū)的操作。盡量在閑時進(jìn)行操作。
交換分區(qū)的操作步驟如下:
1. 創(chuàng)建分區(qū)表,假設(shè)有2個分區(qū),P1,P2.
2. 創(chuàng)建表A存放P1規(guī)則的數(shù)據(jù)。
3. 創(chuàng)建表B 存放P2規(guī)則的數(shù)據(jù)。
4. 用表A 和P1 分區(qū)交換。把表A的數(shù)據(jù)放到到P1分區(qū)
5. 用表B 和p2 分區(qū)交換。把表B的數(shù)據(jù)存放到P2分區(qū)。
創(chuàng)建分區(qū)表:
sql> create table p_dba
2(id number,time date)
3partition by range(time)
4(
5partition p1 values less than (to_date('2010-09-1', 'yyyy-mm-dd')),
6partition p2 values less than (to_date('2010-11-1', 'yyyy-mm-dd'))
7);
表已創(chuàng)建。
注意:我這里只創(chuàng)建了2個分區(qū),沒有創(chuàng)建存放其他數(shù)據(jù)的分區(qū)。
創(chuàng)建2個分別對應(yīng)分區(qū)的基表:
SQL> CREATE TABLE dba_p1 as SELECT id,time_fee FROM dba_old WHEREtime_fee<TO_DATE('2010-09-1', 'YYYY-MM-DD');
表已創(chuàng)建。
SQL> CREATE TABLE dba_p2 as SELECT id,time_fee FROM dba_old WHEREtime_fee<TO_DATE('2010-11-1', 'YYYY-MM-DD') and time_fee>TO_DATE('2010-09-1', 'YYYY-MM-DD');
表已創(chuàng)建。
SQL> select count(*) from dba_p1;
COUNT(*)
----------
1536020
SQL> select count(*) from dba_p2;
COUNT(*)
----------
365932
SQL>
講2個基表與2個分區(qū)進(jìn)行交換:
SQL> alter table p_dba exchange partition p1 with table dba_p1;
表已更改。
SQL> alter table p_dba exchange partition p2 with table dba_p2;
表已更改。
查詢2個分區(qū):
SQL> select count(*) from p_dba partition(p1);
COUNT(*)
----------
1536020
SQL> select count(*) from p_dba partition(p2);
COUNT(*)
----------
365932
注意:數(shù)據(jù)和之前的基表一致。
查詢原來的2個基表:
SQL> select count(*) from dba_p2;
COUNT(*)
----------
0
SQL> select count(*) from dba_p1;
COUNT(*)
----------
0
注意: 2個基表的數(shù)據(jù)變成成0。
在這里我們看一個問題,一般情況下,我們在創(chuàng)建分區(qū)表的時候,都會有一個其他分區(qū),用來存放不匹配分區(qū)規(guī)則的數(shù)據(jù)。在這個例子中,我只創(chuàng)建了2個分區(qū),沒有創(chuàng)建maxvalue分區(qū)?,F(xiàn)在我來插入一條不滿足規(guī)則的數(shù)據(jù),看結(jié)果:
SQL> insert into p_dba values(999999,to_date('2012-12-29','yyyy-mm-dd'));
insert into p_dba values(999999,to_date('2012-12-29','yyyy-mm-dd'))
*
第 1 行出現(xiàn)錯誤:
ORA-14400: 插入的分區(qū)關(guān)鍵字未映射到任何分區(qū)
SQL> insert into p_dba values(999999,to_date('2009-12-29','yyyy-mm-dd'));
已創(chuàng)建 1 行。
SQL> select * from p_dba where id=999999;
ID TIME
---------- --------------
999999 29-12月-09
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
會話已更改。
SQL> select * from p_dba where id=999999;
ID TIME
---------- -------------------
999999 2009-12-29 00:00:00
SQL>
通過這個測試可以清楚,如果插入的數(shù)據(jù)不滿足分區(qū)規(guī)則,會報ORA-14400錯誤。
2.3 . 使用在線重定義:DBMS_REDEFINITION
在線重定義能保證數(shù)據(jù)的一致性,在大部分時間內(nèi),表都可以正常進(jìn)行DML操作。只在切換的瞬間鎖表,具有很高的可用性。這種方法具有很強(qiáng)的靈活性,對各種不同的需要都能滿足。而且,可以在切換前進(jìn)行相應(yīng)的授權(quán)并建立各種約束,可以做到切換完成后不再需要任何額外的管理操作。
關(guān)于DBMS_REDEFINITION的介紹,參考官方連接:
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_redefi.htm#CBBFDJBC
關(guān)于用在線重定義創(chuàng)建分區(qū)表,參考:
How To Partition Existing Table Using DBMS_Redefinition [ID 472449.1]
http://blog.csdn.net/tianlesoftware/archive/2011/03/02/6218693.aspx
這個功能只在9.2.0.4以后的版本才有,在線重定義表具有以下功能:
(1)修改表的存儲參數(shù);
(2)將表轉(zhuǎn)移到其他表空間;
(3)增加并行查詢選項;
(4)增加或刪除分區(qū);
(5)重建表以減少碎片;
(6)將堆表改為索引組織表或相反的操作;
(7)增加或刪除一個列。
使用在線重定義的一些限制條件:
(1) There must be enough space to hold two copies of the table.
(2) Primary key columns cannot be modified.
(3)Tables must have primary keys.
(4) Redefinition must be done within the same schema.
(5) New columns added cannot be made NOT NULL until after the redefinition operation.
(6) Tables cannot contain LONGs, BFILEs or User Defined Types.
(7) Clustered tables cannot be redefined.
(8)Tables in the SYS or SYSTEM schema cannot be redefined.
(9) Tables with materialized view logs or materialized views defined on them cannot be redefined.
(10) Horizontal sub setting of data cannot be performed during the redefinition.
在Oracle 10.2.0.4和11.1.0.7 版本下,在線重定義可能會遇到如下bug:
Bug 7007594 - ORA-600 [12261]
http://blog.csdn.net/tianlesoftware/archive/2011/03/02/6218681.aspx
在線重定義的大致操作流程如下:
(1)創(chuàng)建基礎(chǔ)表A,如果存在,就不需要操作。
(2)創(chuàng)建臨時的分區(qū)表B。
(3)開始重定義,將基表A的數(shù)據(jù)導(dǎo)入臨時分區(qū)表B。
(4)結(jié)束重定義,此時在DB的Name Directory里,已經(jīng)將2個表進(jìn)行了交換。即此時基表A成了分區(qū)表,我們創(chuàng)建的臨時分區(qū)表B 成了普通表。此時我們可以刪除我們創(chuàng)建的臨時表B。它已經(jīng)是普通表。
下面看一個示例:
1. 創(chuàng)建基本表和索引
sql> conn icd/icd;
已連接。
sql> create table unpar_table (
2id number(10) primary key,
3create_date date
4);
表已創(chuàng)建。
sql> insert into unpar_table select rownum, created from dba_objects;
已創(chuàng)建72288行。
sql> create index create_date_ind on unpar_table(create_date);
索引已創(chuàng)建。
sql> commit;
提交完成。
2. 收集表的統(tǒng)計信息
sql> exec dbms_stats.gather_table_stats('icd', 'unpar_table', cascade => true);
pl/sql 過程已成功完成。
3. 創(chuàng)建臨時分區(qū)表
sql> create table par_table (id number primary key, time date) partition by range (time)
2(partition p1 values less than (to_date('2004-7-1', 'yyyy-mm-dd')),
3partition p2 values less than (to_date('2005-1-1', 'yyyy-mm-dd')),
4partition p3 values less than (to_date('2005-7-1', 'yyyy-mm-dd')),
5partition p4 values less than (maxvalue));
表已創(chuàng)建。
4. 進(jìn)行重定義操作
4.1 檢查重定義的合理性
sql> exec dbms_redefinition.can_redef_table('icd', 'unpar_table');
pl/sql 過程已成功完成。
4.2 如果4.1 沒有問題,開始重定義,這個過程可能要等一會。
這里要注意:如果分區(qū)表和原表列名相同,可以用如下方式進(jìn)行:
SQL> BEGIN
DBMS_REDEFINITION.start_redef_table(
uname => 'ICD',
orig_table => 'unpar_table',
int_table => 'par_table');
END;
/
如果分區(qū)表的列名和原表不一致,那么在開始重定義的時候,需要重新指定映射關(guān)系:
SQL> EXEC DBMS_REDEFINITION.START_REDEF_TABLE(
'ICD',
'unpar_table',
'par_table',
'ID ID, create_date TIME', -- 在這里指定新的映射關(guān)系
DBMS_REDEFINITION.CONS_USE_PK);
這一步操作結(jié)束后,數(shù)據(jù)就已經(jīng)同步到這個臨時的分區(qū)表里來了。
4.3 同步新表,這是可選的操作
SQL> BEGIN
2dbms_redefinition.sync_interim_table(
3uname => 'ICD',
4orig_table => 'unpar_table',
5int_table => 'par_table');
6END;
7/
PL/SQL 過程已成功完成。
4.4 創(chuàng)建索引,在線重定義只重定義數(shù)據(jù),索引還需要單獨(dú)建立。
sql> create index create_date_ind2 on par_table(time);
索引已創(chuàng)建。
4.5 收集新表的統(tǒng)計信息
sql> exec dbms_stats.gather_table_stats('icd', 'par_table', cascade => true);
pl/sql 過程已成功完成。
4.6 結(jié)束重定義
SQL> BEGIN
2dbms_redefinition.finish_redef_table(
3uname => 'ICD',
4orig_table => 'unpar_table',
5int_table => 'par_table');
6END;
7/
PL/SQL 過程已成功完成。
結(jié)束重定義的意義:
基表unpar_table 和臨時分區(qū)表par_table 進(jìn)行了交換。此時臨時分區(qū)表par_table成了普通表,我們的基表unpar_table成了分區(qū)表。
我們在重定義的時候,基表unpar_table是可以進(jìn)行DML操作的。只有在2個表進(jìn)行切換的時候會有短暫的鎖表。
5. 刪除臨時表
SQL> DROP TABLE par_table;
表已刪除。
6. 索引重命名
SQL> ALTER INDEX create_date_ind2 RENAME TO create_date_ind;
索引已更改。
7. 驗(yàn)證
sql> select partitioned from user_tables where table_name = 'UNPAR_TABLE';
par
---
yes
sql> select partition_name from user_tab_partitions where table_name = 'UNPAR_TABLE';
partition_name
------------------------------
p1
p2
p3
p4
sql> select count(*) from unpar_table;
count(*)
----------
72288
sql> select count(*) from unpar_table partition (p4);
count(*)
----------
72288
sql>
三. 分區(qū)表的其他操作
3.1 添加新的分區(qū)
添加新的分區(qū)有2中情況:
(1)原分區(qū)里邊界是maxvalue或者default。這種情況下,我們需要把邊界分區(qū)drop掉,加上新分區(qū)后,在添加上新的分區(qū)?;蛘卟捎胹plit,對邊界分區(qū)進(jìn)行拆分。
(2)沒有邊界分區(qū)的。這種情況下,直接添加分區(qū)就可以了。
以邊界分區(qū)添加新分區(qū)示例:
(1)分區(qū)表和索引的信息如下:
SQL> create table custaddr
2(
3id varchar2(15 byte) not null,
4areacode varchar2(4 byte)
5)
6 partition by list (areacode)
7(
8partition t_list556 values ('556') tablespace icd_service,
9partition p_other values (default)tablespace icd_service
10);
表已創(chuàng)建。
SQL> create index ix_custaddr_id on custaddr(id)
2local (
3partition t_list556 tablespace icd_service,
4partition p_other tablespace icd_service
5);
索引已創(chuàng)建。
(2)插入幾條測試數(shù)據(jù):
SQL> insert into custaddr values('1','556');
已創(chuàng)建 1 行。
SQL> insert into custaddr values('2','551');
已創(chuàng)建 1 行。
SQL> insert into custaddr values('3','555');
已創(chuàng)建 1 行。
SQL> commit;
提交完成。
SQL> select * from custaddr;
ID AREA
--------------- ----
1 556
2 551
3 555
SQL> select * from custaddr partition(t_list556);
ID AREA
--------------- ----
1 556
SQL>
(3)刪除default分區(qū)
sql> alter table custaddr drop partition p_other;
表已更改。
sql> select table_name,partition_name from user_tab_partitions where table_name='CUSTADDR';
table_name partition_name
------------------------------ ------------------------------
custaddr t_list556
(4)添加新分區(qū)
SQL> alter table custaddr add partition t_list551 values('551') tablespace icd_service;
表已更改。
SQL> select table_name,partition_name from user_tab_partitions where table_name='CUSTADDR';
TABLE_NAME PARTITION_NAME
------------------------------ ------------------------------
CUSTADDR T_LIST556
CUSTADDR T_LIST551
(5)添加default 分區(qū)
SQL> alter table custaddr add partition p_other values (default)tablespace icd_service;
表已更改。
SQL> select table_name,partition_name from user_tab_partitions where table_name='CUSTADDR';
TABLE_NAME PARTITION_NAME
------------------------------ ------------------------------
CUSTADDR T_LIST556
CUSTADDR T_LIST551
CUSTADDR P_OTHER
(6)對于局部索引,oracle會自動增加一個局部分區(qū)索引。驗(yàn)證一下:
sql> select owner,index_name,table_name,partitioning_type from dba_part_indexes where index_name='ix_custaddr_id';
owner index_nametable_name
---------------------- ------------------------------ ------------------
icd ix_custaddr_idcustaddr
sql> select index_owner,index_name,partition_name from dba_ind_partitions where index_name='ix_custaddr_id';
index_owner index_namepartition_name
------------------------------ ------------------------------ ------------------
icd ix_custaddr_idp_other
icd ix_custaddr_idt_list551
icd ix_custaddr_idt_list556
分區(qū)索引自動創(chuàng)建了。
3.2 split 分區(qū)拆分
在3.1 中,我們說明了可以使用split的方式來添加分區(qū)。這里我們用split方法繼續(xù)上面的實(shí)驗(yàn)。
sql> alter table custaddr split partition p_other values('552') into (partition t_list552 tablespace icd_service, partition p_other tablespace icd_service);
表已更改。
--注意這里紅色的地方,如果是Range類型的,使用at,List使用Values。
SQL> select table_name,partition_name from user_tab_partitions where table_name='CUSTADDR';
TABLE_NAME PARTITION_NAME
------------------------------ ------------------------------
CUSTADDR T_LIST556
CUSTADDR T_LIST551
CUSTADDRT_LIST552
CUSTADDR P_OTHER
SQL> select index_owner,index_name,partition_name from dba_ind_partitions where index_name='IX_CUSTADDR_ID';
index_owner index_namepartition_name
------------------------------ ------------------------------ ------------------
icd ix_custaddr_idp_other
icd ix_custaddr_idt_list551
icd ix_custaddr_idt_list552
icd ix_custaddr_idt_list556
注意:分區(qū)表會自動維護(hù)局部分區(qū)索引。全局索引會失效,需要進(jìn)行rebuild。
3.3 合并分區(qū)Merge
相鄰的分區(qū)可以merge為一個分區(qū),新分區(qū)的下邊界為原來邊界值較低的分區(qū),上邊界為原來邊界值較高的分區(qū),原先的局部索引相應(yīng)也會合并,全局索引會失效,需要rebuild。
SQL> alter table custaddr merge partitions t_list552,p_other into partition p_other;
表已更改。
SQL> select index_owner,index_name,partition_name from dba_ind_partitions where index_name='IX_CUSTADDR_ID';
index_owner index_namepartition_name
-------------------- ------------------------------ ------------------
icd ix_custaddr_idp_other
icd ix_custaddr_idt_list551
icd ix_custaddr_idt_list556
SQL> select table_name,partition_name from user_tab_partitions where table_name='CUSTADDR';
table_name partition_name
------------------------------ ------------------------------
custaddr t_list556
custaddr t_list551
custaddr p_other
3.4 . 移動分區(qū)
SQL> alter table custaddr move partition P_OTHER tablespace system;
表已更改。
SQL> alter table custaddr move partition P_OTHER tablespace icd_service;
表已更改。
注意:分區(qū)移動會自動維護(hù)局部分區(qū)索引,oracle不會自動維護(hù)全局索引,所以需要我們重新rebuild分區(qū)索引,具體需要rebuild哪些索引,可以通過dba_part_indexes,dba_ind_partitions去判斷。
SQL> Select index_name,status From user_indexes Where table_name='CUSTADDR';
INDEX_NAME STATUS
------------------------------ --------
IX_CUSTADDR_ID N/A
3.5. Truncate分區(qū)
SQL> select * from custaddr partition(T_LIST556);
ID AREA
--------------- ----
1 556
SQL> alter table custaddr truncate partition(T_LIST556);
表被截斷。
SQL> select * from custaddr partition(T_LIST556);
未選定行
說明:
Truncate相對delete操作很快,數(shù)據(jù)倉庫中的大量數(shù)據(jù)的批量數(shù)據(jù)加載可能會有用到;截斷分區(qū)同樣會自動維護(hù)局部分區(qū)索引,同時會使全局索引unusable,需要重建
3.6. Drop分區(qū)
SQL> alter table custaddr drop partition T_LIST551;
表已更改。
SQL> select table_name,partition_name from user_tab_partitions where table_name='CUSTADDR';
TABLE_NAME PARTITION_NAME
------------------------------ ------------------------------
CUSTADDR T_LIST556
CUSTADDR P_OTHER
同樣會自動維護(hù)局部分區(qū)索引,同時會使全局索引unusable,需要重建
四. 分區(qū)表的索引
分區(qū)索引分為本地(local index)索引和全局索引(global index)。局部索引比全局索引容易管理, 而全局索引比較快。
與索引有關(guān)的表:
dba_part_indexes 分區(qū)索引的概要統(tǒng)計信息,可以得知每個表上有哪些分區(qū)索引,分區(qū)索引的類型(local/global)
dba_ind_partitions 每個分區(qū)索引的分區(qū)級統(tǒng)計信息
dba_indexes/dba_part_indexes 可以得到每個表上有哪些非分區(qū)索引
Local索引肯定是分區(qū)索引,Global索引可以選擇是否分區(qū),如果分區(qū),只能是有前綴的分區(qū)索引。
分區(qū)索引分2類:有前綴(prefix)的分區(qū)索引和無前綴(nonprefix)的分區(qū)索引:
(1)有前綴的分區(qū)索引指包含了分區(qū)鍵,并且將其作為引導(dǎo)列的索引。
如:
create index i_id_global on PDBA(id) global --引導(dǎo)列
2partition by range(id) --分區(qū)鍵
3(partition p1 values less than (200),
4partition p2 values less than (maxvalue)
5);
這里的ID 就是分區(qū)鍵,并且分區(qū)鍵id 也是索引的引導(dǎo)列。
(2)無前綴的分區(qū)索引的列不是以分區(qū)鍵開頭,或者不包含分區(qū)鍵列。
如:
create index ix_custaddr_local_id_p on custaddr(id)
local (
partition t_list556 tablespace icd_service,
partition p_other tablespace icd_service
)
這個分區(qū)是按照areacode來的。但是索引的引導(dǎo)列是ID。所以它就是非前綴分區(qū)索引。
全局分區(qū)索引不支持非前綴的分區(qū)索引,如果創(chuàng)建,報錯如下:
SQL> create index i_time_global on PDBA(id) global --索引引導(dǎo)列
2partition by range(time) --分區(qū)建
3(partition p1 values less than (TO_DATE('2010-12-1', 'YYYY-MM-DD')),
4partition p2 values less than (maxvalue)
5);
partition by range(time)
*
第 2 行出現(xiàn)錯誤:
ORA-14038: GLOBAL 分區(qū)索引必須加上前綴
4.1 Local 本地索引
對于local索引,當(dāng)表的分區(qū)發(fā)生變化時,索引的維護(hù)由Oracle自動進(jìn)行。
注意事項:
(1)局部索引一定是分區(qū)索引,分區(qū)鍵等同于表的分區(qū)鍵。
(2) 前綴和非前綴索引都可以支持索引分區(qū)消除,前提是查詢的條件中包含索引分區(qū)鍵。
(3)局部索引只支持分區(qū)內(nèi)的唯一性,無法支持表上的唯一性,因此如果要用局部索引去給表做唯一性約束,則約束中必須要包括分區(qū)鍵列。
(4)局部分區(qū)索引是對單個分區(qū)的,每個分區(qū)索引只指向一個表分區(qū);全局索引則不然,一個分區(qū)索引能指向n個表分區(qū),同時,一個表分區(qū),也可能指向n個索引分區(qū),對分區(qū)表中的某個分區(qū)做truncate或者move,shrink等,可能會影響到n個全局索引分區(qū),正因?yàn)檫@點(diǎn),局部分區(qū)索引具有更高的可用性。
(5)位圖索引必須是局部分區(qū)索引。
(6)局部索引多應(yīng)用于數(shù)據(jù)倉庫環(huán)境中。
(7) B樹索引和位圖索引都可以分區(qū),但是HASH索引不可以被分區(qū)。
示例:
sql> create index ix_custaddr_local_id on custaddr(id) local;
索引已創(chuàng)建。
和下面SQL 效果相同,因?yàn)閘ocal索引就是分區(qū)索引:
create index ix_custaddr_local_id_p on custaddr(id)
local (
partition t_list556 tablespace icd_service,
partition p_other tablespace icd_service
)
SQL> create index ix_custaddr_local_areacode on custaddr(areacode) local;
索引已創(chuàng)建。
驗(yàn)證2個索引的類型:
SQL> select index_name,table_name,partitioning_type,locality,ALIGNMENT from user_part_indexes where table_name='CUSTADDR';
index_name table_name partition locali alignment
------------------------------ ---------- --------- ------ ------------
ix_custaddr_local_areacodecustaddr list localprefixed
ix_custaddr_local_id custaddrlist local non_prefixed
因?yàn)槲覀兊腸ustaddr表是按areacode進(jìn)行分區(qū)的,所以索引ix_custaddr_local_areacode是有前綴的索引(prefixed)。而ix_custaddr_local_id是非前綴索引。
4.2 Global索引
對于global索引,可以選擇是否分區(qū),而且索引的分區(qū)可以不與表分區(qū)相對應(yīng)。全局分區(qū)索引只能是B樹索引,到目前為止(10gR2),oracle只支持有前綴的全局索引。
另外oracle不會自動的維護(hù)全局分區(qū)索引,當(dāng)我們在對表的分區(qū)做修改之后,如果對分區(qū)進(jìn)行維護(hù)操作時不加上update global indexes的話,通常會導(dǎo)致全局索引的INVALDED,必須在執(zhí)行完操作后 REBUILD。
注意事項:
(1)全局索引可以分區(qū),也可以是不分區(qū)索引,全局索引必須是前綴索引,即全局索引的索引列必須是以索引分區(qū)鍵作為其前幾列。
(2)全局索引可以依附于分區(qū)表;也可以依附于非分區(qū)表。
(3)全局分區(qū)索引的索引條目可能指向若干個分區(qū),因此,對于全局分區(qū)索引,即使只截斷一個分區(qū)中的數(shù)據(jù),都需要rebulid若干個分區(qū)甚至是整個索引。
(4)全局索引多應(yīng)用于oltp系統(tǒng)中。
(5)全局分區(qū)索引只按范圍或者散列分區(qū),hash分區(qū)是10g以后才支持。
(6)oracle9i以后對分區(qū)表做move或者truncate的時可以用update global indexes語句來同步更新全局分區(qū)索引,用消耗一定資源來換取高度的可用性。
(7)表用a列作分區(qū),索引用b做局部分區(qū)索引,若where條件中用b來查詢,那么oracle會掃描所有的表和索引的分區(qū),成本會比分區(qū)更高,此時可以考慮用b做全局分區(qū)索引。
注意:Oracle只支持2中類型的全局分區(qū)索引:
range partitioned 和 Hash Partitioned.
官網(wǎng)的說明如下:
Global Partitioned Indexes
Oracle offers two types of global partitioned index: range partitioned and hash partitioned.
(1)Global Range Partitioned Indexes
Global range partitioned indexes are flexible in that the degree of partitioning and the partitioning key are independent from the table's partitioning method. They are commonly used for OLTP environments and offer efficient access to any individual record.
The highest partition of a global index must have a partition bound, all of whose values are MAXVALUE. This ensures that all rows in the underlying table can be represented in the index. Global prefixed indexes can be unique or nonunique.
You cannot add a partition to a global index because the highest partition always has a partition bound of MAXVALUE. If you wish to add a new highest partition, use the ALTER INDEX SPLIT PARTITION statement. If a global index partition is empty, you can explicitly drop it by issuing the ALTER INDEX DROP PARTITION statement. If a global index partition contains data, dropping the partition causes the next highest partition to be marked unusable. You cannot drop the highest partition in a global index.
(2)Global Hash Partitioned Indexes
Global hash partitioned indexes improve performance by spreading out contention when the index is monotonically growing. In other words, most of the index insertions occur only on the right edge of an index.
(3)Maintenance of Global Partitioned Indexes
By default, the following operations on partitions on a heap-organized table mark all global indexes as unusable:
ADD (HASH)
COALESCE (HASH)
DROP
EXCHANGE
MERGE
MOVE
SPLIT
TRUNCATE
示例1 全局索引,全局索引對所有分區(qū)類型都支持:
sql> create index ix_custaddr_ global_id on custaddr(id) global;
索引已創(chuàng)建。
示例2:全局分區(qū)索引,只支持Range 分區(qū)和Hash 分區(qū):
(1)創(chuàng)建2個測試分區(qū)表:
sql> create table pdba (id number, time date) partition by range (time)
2(
3partition p1 values less than (to_date('2010-10-1', 'yyyy-mm-dd')),
4partition p2 values less than (to_date('2010-11-1', 'yyyy-mm-dd')),
5partition p3 values less than (to_date('2010-12-1', 'yyyy-mm-dd')),
6partition p4 values less than (maxvalue)
7);
表已創(chuàng)建。
SQL> create table Thash
2(
3id number primary key,
4item_id number(8) not null
5)
6partition by hash(id)
7(
8partition part_01,
9partition part_02,
10partition part_03
11);
表已創(chuàng)建。
(2)創(chuàng)建分區(qū)索引
示例2:全局分區(qū)索引
SQL> create index i_id_global on PDBA(id) global
2partition by range(id)
3(partition p1 values less than (200),
4partition p2 values less than (maxvalue)
5);
索引已創(chuàng)建。
--這個是有前綴的分區(qū)索引。
SQL> create index i_time_global on PDBA(id) global
2partition by range(time)
3(partition p1 values less than (TO_DATE('2010-12-1', 'YYYY-MM-DD')),
4partition p2 values less than (maxvalue)
5);
partition by range(time)
*
第 2 行出現(xiàn)錯誤:
ORA-14038: GLOBAL 分區(qū)索引必須加上前綴
SQL> create index i_time_global on PDBA(time) global
2partition by range(time)
3(partition p1 values less than (TO_DATE('2010-12-1', 'YYYY-MM-DD')),
4partition p2 values less than (maxvalue)
5);
索引已創(chuàng)建。
--有前綴的分區(qū)索引
SQL> select index_name,table_name,partitioning_type,locality,ALIGNMENT from user_part_indexes where table_name='PDBA';
index_name table_name partition locali alignment
------------------------------ ---------- --------- ------ ------------
i_id_global pdba rangeglobal prefixed
i_time_global pdba rangeglobal prefixed
SQL> CREATE INDEX ix_hash ON PDBA (id,time) GLOBAL
2PARTITION BY HASH (id)
3(PARTITION p1,
4PARTITION p2,
5PARTITION p3,
6PARTITION p4);
索引已創(chuàng)建。
只要索引的引導(dǎo)列包含分區(qū)鍵,就是有前綴的分區(qū)索引。
4.3 索引重建問題
(1)分區(qū)索引
對于分區(qū)索引,不能整體進(jìn)行重建,只能對單個分區(qū)進(jìn)行重建。語法如下:
Alter index idx_name rebuild partition index_partition_name [online nologging]
說明:
online:表示重建的時候不會鎖表。
nologging:表示建立索引的時候不生成日志,加快速度。
如果要重建分區(qū)索引,只能drop表原索引,在重新創(chuàng)建:
SQL>create index loc_xxxx_col on xxxx(col) local tablespace SYSTEM;
這個操作要求較大的臨時表空間和排序區(qū)。
示例:
SQL> select index_name,partition_name from user_ind_partitions where index_name='I_TIME_GLOBAL';
INDEX_NAME PARTITION_NAME
------------------------------ ------------------------------
I_TIME_GLOBAL P1
I_TIME_GLOBAL P2
SQL> alter index I_TIME_GLOBAL rebuild partition p1 online nologging;
索引已更改。
SQL> alter index I_TIME_GLOBAL rebuild partition p2 online nologging;
索引已更改。
(2)全局索引
Oracle 會自動維護(hù)分區(qū)索引,對于全局索引,如果在對分區(qū)表操作時,沒有指定update index,則會導(dǎo)致全局索引失效,需要重建。
SQL> select owner,index_name,table_name,status from dba_indexes where INDEX_NAME='IX_PDBA_GLOBAL';
owner index_nametable_name status
------------------------------ ------------------------------ ---------- -------
sys ix_pdba_globalpdbavalid
刪除一個分區(qū):
SQL> alter table pdba drop partition p2;
表已更改。
SQL> select owner,index_name,table_name,status from dba_indexes where INDEX_NAME='IX_PDBA_GLOBAL';
owner index_nametable_name status
------------------------------ ------------------------------ ---------- -------
sys ix_pdba_globalpdba valid
split 分區(qū):
SQL> alter table pdba split partition P4 at(TO_DATE('2010-12-21 00:00:00','YYYY-MM-DD HH24:MI:SS')) into (partition P4, partition P5);
表已更改。
SQL> select owner,index_name,table_name,status from dba_indexes where INDEX_NAME='IX_PDBA_GLOBAL';
owner index_nametable_name status
------------------------------ ------------------------------ ---------- -------
sys ix_pdba_globalpdba valid
drop 分區(qū)時使用update indexes
SQL> alter table pdba drop partition P4 UPDATE INDEXES;
表已更改。
SQL> select owner,index_name,table_name,status from dba_indexes where INDEX_NAME='IX_PDBA_GLOBAL';
owner index_nametable_name status
---------------------- ------------------------------ ---------- -------
sys ix_pdba_globalpdbavalid
做了幾個drop分區(qū)操作,全局索引沒有失效,有點(diǎn)奇怪。不過如果在生產(chǎn)環(huán)境中,還是小心點(diǎn)。
重建全局索引命令如下:
Alter index idx_name rebuild [online nologging]
示例:
SQL> Alter index ix_pdba_global rebuild online nologging;
索引已更改。
補(bǔ)充一點(diǎn),分區(qū)表存儲空間的問題:
SQL> select table_name,partition_name,tablespace_name from user_tab_partitions where table_name='DBA';
TABLE_NAME PARTITION_NAME TABLESPACE_NAME
---------- ------------------------------ ------------------------------
DBA P1 SYSTEM
DBA P2 SYSTEM
DBA P3 SYSTEM
DBA P4 SYSTEM
通過user_tab_partitions 表可以查看到每個分區(qū)對應(yīng)的tablesapce_name. 但是,如果通過all_tables 表,卻查不到分區(qū)表對應(yīng)表空間的信息。
分區(qū)表:
SQL> select owner,table_name,tablespace_name,cluster_name from all_tables where table_name='DBA';
OWNER TABLE_NAME TABLESPACE_NAME CLUSTER_NAME
----- ---------- ------------------------------ -----------------------------------------------------
SYS DBA
普通表:
SQL> select owner,table_name,tablespace_name,cluster_name from all_tables where table_name='DAVE';
OWNER TABLE_NAME TABLESPACE_NAME CLUSTER_NAME
----- ---------- ------------------------------ ---------------------------------------------------
SYS DAVE SYSTEM
轉(zhuǎn)載: http://adamxgl.blog.163.com/blog/static/29094652011117111221690/

oracle_分區(qū)表的新增、修改、刪除、合并。普通表轉(zhuǎn)分區(qū)表方法


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日本一级在线观看视频播放 | 国产亚洲欧美另类久久久 | 在线视频一二三区 | 久久综合丁香 | 99久久综合狠狠综合久久 | 国产精品久久自在自线观看 | 草草影院第一页 | 在线久久 | 国产免费精彩视频 | 国产亚洲欧美在线观看的 | 不卡的中文字幕 | 久久成人国产 | 国产亚洲精品久久久久91网站 | 亚洲一区二区欧美日韩 | 色综合久久久久久中文网 | 天天干天天草天天 | 99精品国内不卡在线观看 | 欧美色综合高清免费 | 天天干成人网 | 三级a做爰大乳在线观看 | 999视频在线观看 | 欧美精品亚洲 | 毛片在线播 | 亚洲日本一区二区三区在线不卡 | 日本最黄视频 | 久久久久依人综合影院 | 色 在线播放 | 青草网址 | 在线不卡一区 | 久久久免费观成人影院 | 日韩在线无 | 成人a毛片 | 日韩字幕一中文在线综合 | 日韩有码在线视频 | 91在线欧美 | 黄色综合 | 久草最新在线 | 热99re久久精品天堂vr | a级黄片毛片 | 在线播放亚洲 | 牛牛影院免费永久地址 |