Oracle 表分區的理解整理

1.表空間及分區表的概念

 

    表空間:是一個或多個數據文件的集合,所有的數據對象都存放在指定的表空間中,但主要存放的是表,所以稱作表空間。

    分區表:當表中的數據量不斷增大,查詢數據的速度就會變慢,應用程序的性能就會下降,這時就應該考慮對錶進行分區。表進行分區後,邏輯上表仍然是一張完整的表,只是將表中的數據在物理上存放到多個表空間(物理文件上),這樣查詢數據時,不至於每次都掃描整張表。

 

2.表分區的具體作用

    oracle的表分區功能通過改善可管理性、性能和可用性,從而爲各式應用程序帶來了極大的好處。通常,分區可以使某些查詢以及維護操作的性能大大提高。此外,分區還可以極大簡化常見的管理任務,分區是構建千兆字節數據系統或超高可用性系統的關鍵工具。

    分區功能能夠將表、索引或索引組織表進一步細分爲段,這些數據庫對象的段叫做分區。每個分區有自己的名稱,還可以選擇自己的存儲特性。從數據庫 管理員的角度來看,一個分區後的對象具有多個段,這些段既可進行集體管理,也可單獨管理,這就使數據庫管理員在管理分區後的對象時有相當大的靈活性。但 是,從應用程序的角度來看,分區後的表與非分區表完全相同,使用 SQL DML 命令訪問分區後的表時,無需任何修改。

 

什麼時候使用分區表,官方給的建議是:

a. 表的大小超過2GB。

b. 表中包含歷史數據,新的數據被增加到新的分區中。

 

3.表分區的優缺點

優點:

a.改善查詢性能:對分區對象的查詢可以僅搜索自己關心的分區,提高檢索速度。

b.增強可用性:如果表的某個分區出現故障,表在其他分區的數據仍然可用。

c.維護方便:如果表的某個分區出現故障,需要修復數據,只修復該分區即可。

d.均衡I/O:可以把不同的分區映射到磁盤以平衡I/O,改善整個系統性能。

 

缺點:

分區表相關,已經存在的表沒有方法可以直接轉化爲分區表。不過oracle提供了在線重定義表的功能。

 

4.表分區的幾種類型及操作方法

4.1 範圍分區(range)  maxvalue

    範圍分區將數據基於範圍映射到每一個分區,這個範圍是你在創建分區時指定的分區鍵決定的。這種分區方式是最爲常用的,並且分區鍵經常採用日期。舉個例子:你可能會將銷售數據按照月份進行分區。

    當使用範圍分區時,請考慮以下幾個規則:

a.每一個分區都必須有一個VALUES LESS THEN子句,它指定了一個不包括在該分區中的上限值。分區鍵的任何值等於或者大於這個上限值的記錄都會被加入到下一個高一些的分區中。

b.所有分區,除了第一個,都會有一個隱式的下限值,這個值就是此分區的前一個分區的上限值。

c.如果某些記錄暫無法預測範圍,可以創建maxvalue分區,所有不在指定範圍內的記錄都會被存儲到maxvalue所在分區中。

 

例1假設有一個test表,表中有數據200000行,我們將此表通過id進行分區,每個分區存儲100000行,我們將每個分區保存到單獨的表空間中,這樣數據文件就可以跨越多個物理磁盤。下面是創建表和分區的代碼,如下:

----先創建多個測試表空間

sys@ORCL>create tablespace test_ts01 datafile '/home/oracle/test_01.dbf' size 32m extent management local autoallocate;

Tablespace created.

sys@ORCL>create tablespace test_ts02 datafile '/home/oracle/test_02.dbf' size 32m extent management local autoallocate;

Tablespace created.

sys@ORCL>create tablespace test_ts03 datafile '/home/oracle/test_03.dbf' size 32m extent management local autoallocate;

Tablespace created.

 

----創建test分區表

create table test

(        id number not null,

         first_name varchar2(30) not null,

         last_name varchar2(30) not null,

         phone varchar2(30) not null,

         email varchar2(80),

         status char(1),

         constraint test_id primary key (id)

)

partition by range (id)

(        partition test_part1 values less than (100000) tablespace test_ts01,

         partition test_part2 values less than (200000) tablespace test_ts02,

         partition test_part3 values less than (maxvalue) tablespace test_ts03

);

        

 

 

例2:按時間劃分

create table order_time

(        order_id number(7) not null,

         order_date date,

         total_amount number,

         custotmer_id number(7),

         paid char(1)

)

partition by range(order_date)

(        partition ora_time_part01 values less than (to_date('2016-06-01','yyyy-mm-dd')) tablespace test_ts01,

         partition ora_time_part02 values less than (to_date('2016-07-01','yyyy-mm-dd')) tablespace test_ts02,

         partition ora_time_part03 values less than (to_date('2016-08-01','yyyy-mm-dd')) tablespace test_ts03

);

 

 

例3:maxvalue

create table rangetable

(        rt_id number(8) not null,

         name varchar(10),

         grade int,

         constraint ranget_id primary key (rt_id)

)

partition by range (grade)

(        partition part1 values less than (1000) tablespace test_ts01,

         partition part2 values less than (2000) tablespace test_ts02,

         partition part3 values less than (maxvalue) tablespace test_ts03

);

 

 

4.2 列表分區(list)  default

    List分區也需要指定列的值,其分區值必須明確指定,該分區列只能有一個,不能像range或者hash分區那樣同時指定多個列做爲分區依賴列,但它的單個分區對應值可以是多個。

    在分區時必須確定分區列可能存在的值,一旦插入的列值不在分區範圍內,則插入/更新就會失敗,因此通常建議使用list分區時,要創建一個default分區存儲那些不在指定範圍內的記錄,類似range分區中的maxvalue分區。

    在根據某字段,如城市代碼分區時,可以指定default,把非分區規則的數據,全部放到這個default分區。該分區的特點是某列的值只有幾個,基於這樣的特點我們可以採用列表分區。

 

create tablespace test_ts04 datafile '/home/oracle/test_04.dbf' size 32m extent management local autoallocate;

create tablespace test_ts05 datafile '/home/oracle/test_05.dbf' size 32m extent management local autoallocate;

create tablespace test_ts06 datafile '/home/oracle/test_06.dbf' size 32m extent management local autoallocate;

alter database datafile '/home/oracle/test_06.dbf' resize 100m;

 

例1

create table problem_tickets

(        problem_id number(7) not null,

         description varchar2(2000),

         customer_id number(7) not null,

         date_entered date not null,

         status varchar2(20),

         constraint problem_tic_id primary key (problem_id)

)

partition by list (status)

(        partition prob_active values ('active') tablespace test_ts04,

         partition prob_inactive values ('inactive') tablespace test_ts05,

         partition prob_other values(default) tablespace test_ts06

);

 

例2

create table ListTable

(        id int,

         name varchar2(20),

         area varchar2(10),

         constraint ListTable_id primary key (id)

)

partition by list (area)

(        partition part1 values ('SH','BJ') tablespace test_ts04,

         partition part2 values ('SC','CQ') tablespace test_ts05,

         partition part3 values ('SD') tablespace test_ts06

);

 

4.3 散列分區(hash)

    對於那些無法有效劃分範圍的表,可以使用hash分區,這樣對於提高性能還是會有一定的幫助。hash分區會將表中的數據平均分配到你指定的幾個分區中,列所在分區是依據分區列的hash值自動分配,因此你並不能控制也不知道哪條記錄會被放到哪個分區中,hash分區也可以支持多個依賴列。

 

例1

create table hash_table

(        col number(8),

         inf varchar2(100)

)

partition by hash(col)

(        partition part01 tablespace test_ts04,

         partition part02 tablespace test_ts05,

         partition part03 tablespace test_ts06

);

 

簡寫:

create tablespace test_ts07 datafile '/home/oracle/test_07.dbf' size 32m extent management local autoallocate;

create tablespace test_ts08 datafile '/home/oracle/test_08.dbf' size 32m extent management local autoallocate;

create tablespace test_ts09 datafile '/home/oracle/test_09.dbf' size 32m extent management local autoallocate;

 

create table emp

(        empno number(4),

         ename varchar2(30),

         sal number

)

partition by hash (empno) partitions 4

store in (test_ts06,test_ts07,test_ts08,test_ts09);

 

 

4.4 組合分區

    如果某表按照某列分區之後,仍然較大,或者是一些其它的需求,還可以通過分區內再建子分區的方式將分區再分區,即組合分區的方式。

    在10g中組合分區主要有兩種:range-hash,range-list。11g中又增加了range-range,list-range,list-list,list-hash,並且 11g裏面還支持Interval分區和虛擬列分區。 注意順序,根分區只能是range分區,子分區可以是hash分區或list分區。

 

----oracle 11g 新特性簡介:

http://blog.csdn.net/tianlesoftware/article/details/5134819

----分區表 之 Interval分區 和 虛擬列 按星期分區表

http://blog.csdn.net/tianlesoftware/article/details/5662337

 

 

4.4.1 範圍-列表複合分區(range-list)

    這種分區是基於範圍分區和列表分區,表首先按某列進行範圍分區,然後再按某列進行列表分區,分區之中的分區被稱爲子分區。

 

create table sales

(        product_id varchar2(5),

         sales_date date,

         sales_cost number(10),

         status varchar2(30)

)

partition by range (sales_date) subpartition by list (status)

(        partition p1 values less than(to_date('2016-06-01','yyyy-mm-dd')) tablespace test_ts07

             (        subpartition p1sub1 values ('active') tablespace test_ts07,

                            subpartition p1sub2 values ('inactive') tablespace test_ts07

         ),

         partition p2 values less than(to_date('2016-07-01','yyyy-mm-dd')) tablespace test_ts08

                   (        subpartition p2sub1 values('active') tablespace test_ts08,

                            subpartition p2sub2 values ('inactive') tablespace test_ts08

                    )

);

 

 

4.4.2 範圍-散列複合分區(range-hash)

    這種分區是基於範圍分區和散列分區,表首先按某列進行範圍分區,然後再按某列進行散列分區。

create tablespace test_ts11 datafile '/home/oracle/test_11.dbf' size 32m extent management local autoallocate;

create tablespace test_ts12 datafile '/home/oracle/test_12.dbf' size 32m extent management local autoallocate;

create tablespace test_ts13 datafile '/home/oracle/test_13.dbf' size 32m extent management local autoallocate;

 

create table dinya_test

 (      transaction_id number,

         item_id number(8) not null,

         item_description varchar2(300),

         transaction_date date,

         constraint dinya_test_id primary key (transaction_id)

 )

partition by range(transaction_date) subpartition by hash(transaction_id)  subpartitions 3

store in (test_ts11,test_ts12,test_ts13)

          (    partition part_01 values less than(to_date('2016-06-01','yyyy-mm-dd')),

              partition part_02 values less than(to_date('2016-12-01','yyyy-mm-dd')),

              partition part_03 values less than(maxvalue)

 );

 

 

5.分區表的維護操作

5.1 添加分區(add)

----添加新的分區有2中情況:

(1)原分區裏邊界是maxvalue或者default。 這種情況下,我們需要把邊界分區drop掉,加上新分區後,在添加上新的分區。 或者採用split(range類型使用at,list使用values)對邊界分區進行拆分。

(2)沒有邊界分區的。 這種情況下,直接添加分區就可以了。

 

----以下代碼給test表添加一個分區

alter table test add partition test_part4 values less than (400000);

create tablespace test_ts14 datafile '/home/oracle/test_14.dbf' size 32m;

alter table test add partition test_part5 values less than (500000) tablespace test_ts14;

----注意:以上添加的分區界限應該高於最後一個分區界限。

 

----以下代碼給sales表的p2分區添加一個p2sub3子分區

create tablespace test_ts15 datafile '/home/oracle/test_15.dbf' size 3g;

alter table sales modify partition p2 add subpartition p2sub3 values('complete') tablespace test_ts15;

 

--------------有邊界分區添加新分區:

-----1> 創建分區表及創建索引

create table custaddr

(        id varchar2 (15 byte) not null,

         areacode varchar2(4 byte)

)

partition by list (areacode)

(        partition t_list555 values ('555') tablespace test_ts15,

         partition p_other values (default) tablespace test_ts15);

 

create index ix_custaddr_id on custaddr(id)

local (  partition t_list555 tablespace test_ts15,

            partition p_other tablespace test_ts15);

 

----2> 插入測試數據

insert into custaddr values ('1','555');

insert into custaddr values ('2','552');

insert into custaddr values ('3','554');

commit;

select * from custaddr;

ID                             AREACODE

------------------------------ --------

1                              555

2                              552

3                              554

select * from custaddr partition(t_list555);

ID                             AREACODE

------------------------------ --------

1                              555

 

----3> 刪除default分區

shall@ORCL>alter table custaddr drop partition p_other;

Table altered.

shall@ORCL>select * from custaddr;

ID                             AREACODE

------------------------------ --------

1                              555

shall@ORCL>select table_name,partition_name from user_tab_partitions where table_name='CUSTADDR';

TABLE_NAME           PARTITION_NAME

-------------------- ------------------------------

CUSTADDR             T_LIST555

 

----4> 添加新分區,default分區

shall@ORCL> alter table custaddr add partition t_list551 values('551') tablespace test_ts15;

shall@ORCL>alter table custaddr add partition p_other values (default) tablespace test_ts15;

 

shall@ORCL> select table_name,partition_name from user_tab_partitions where table_name='CUSTADDR';

TABLE_NAME           PARTITION_NAME

-------------------- ------------------------------

CUSTADDR             P_OTHER

CUSTADDR             T_LIST551

CUSTADDR             T_LIST555

 

----5> 對於局部索引,oracle會自動增加一個局部分區索引

shall@ORCL> select index_name,table_name,partitioning_type from user_part_indexes where index_name='IX_CUSTADDR_ID';

INDEX_NAME                             TABLE_NAME           PARTITIONING_TYPE

------------------------------------------------------------ -------------------- ------------------

IX_CUSTADDR_ID                            CUSTADDR             LIST

 

shall@ORCL>select index_name,partition_name from user_ind_partitions where index_name='IX_CUSTADDR_ID';

INDEX_NAME                                         PARTITION_NAME

------------------------------------------------------------ ------------------------------

IX_CUSTADDR_ID                                               P_OTHER

IX_CUSTADDR_ID                                               T_LIST551

IX_CUSTADDR_ID                                               T_LIST555

 

----使用split分區拆分方式,接上面--2>進行下面測試

----3> 使用split方式添加分區

alter table custaddr split partition p_other values('552') into (partition t_list552 tablespace test_ts15, partition p_other tablespace test_ts15);

------注意,這裏如果是range類型,使用at,list使用values

 

select table_name,partition_name from user_tab_partitions where table_name='CUSTADDR';

TABLE_NAME           PARTITION_NAME

-------------------- ------------------------------

CUSTADDR             P_OTHER

CUSTADDR             T_LIST552

CUSTADDR             T_LIST555

 

select index_name,partition_name from user_ind_partitions where index_name='IX_CUSTADDR_ID';

INDEX_NAME                                                   PARTITION_NAME

------------------------------------------------------------ ------------------------------

IX_CUSTADDR_ID                                               P_OTHER

IX_CUSTADDR_ID                                               T_LIST552

IX_CUSTADDR_ID                                               T_LIST555

------注意:分區表會自動維護局部分區索引。全局索引會失效,需要rebuild

shall@ORCL>Select index_name,status From user_indexes Where table_name='CUSTADDR';

INDEX_NAME                                                   STATUS

------------------------------------------------------------ ----------------

IX_CUSTADDR_ID                                               N/A

 

 

------查看數據

shall@ORCL>select * from custaddr;

ID                             AREACODE

------------------------------ --------

1                              555

2                              552

3                              554

shall@ORCL>select * from custaddr partition(t_list552);

ID                             AREACODE

------------------------------ --------

2                              552

shall@ORCL>select * from custaddr partition(t_list555);

ID                             AREACODE

------------------------------ --------

1                              555

shall@ORCL>select * from custaddr partition(p_other);

ID                             AREACODE

------------------------------ --------

3                              554

 

 

5.2 刪除分區(drop)

----以下代碼刪除了sales表p2分區

alter table sales drop partition p2;

 

------alter table sales add partition p2 values less than(to_date('2016-07-01','yyyy-mm-dd')) tablespace test_ts08

                   (        subpartition p2sub1 values('active') tablespace test_ts08,

                            subpartition p2sub2 values ('inactive') tablespace test_ts08,

                            subpartition p2sub3 values('complete') tablespace test_ts15

                    );

----刪除sales表p2sub3子分區

alter table sales drop subpartition p2sub3;

----注意:如果刪除的分區是表中唯一的分區,那麼此分區將不能被刪除,要想刪除此分區,必須刪除表。

----同樣會自動維護局部分區索引,同時會使全局索引unusable,需要重建

 

5.3 截斷分區(truncate)

    截斷某個分區是指刪除某個分區中的數據,並不會刪除分區,也不會刪除其它分區中的數據。當表中即使只有一個分區時,也可以截斷該分區。通過以下代碼截斷分區:

alter table sales truncate partition p2;

 

----當然也可以截斷子分區

alter table sales truncate subpartition p2sub2;

 

    Truncate相對delete操作很快,數據倉庫中的大量數據的批量數據加載可能會有用到;截斷分區同樣會自動維護局部分區索引,同時會使全局索引unusable,需要重建

 

5.4 合併分區(merge)

    相鄰的分區可以merge爲一個分區,新分區的下邊界爲原來邊界值較低的分區,上邊界爲原來邊界值較高的分區,原先的局部索引相應也會合並,全局索引會失效,需要rebuild。

 

alter database datafile '/home/oracle/test_08.dbf' resize 500m;

alter table sales merge partitions p1,p2 into partition p2;

 

5.5 拆分分區(split)

    拆分分區將一個分區拆分兩個新分區,拆分後原來分區不再存在。注意不能對HASH類型的分區進行拆分。

alter table sales split partition p2 at (to_date('2016-06-01','yyyy-mm-dd')) into (partition p3,partition p4);

 

5.6 重命名分區(rename)

alter table sales rename partition p3 to p13;

 

5.7 移動分區(move)

alter table test move partition test_part1 tablespace test_ts15;

alter table test move partition test_part1 tablespace test_ts01;

    注意:分區移動會自動維護局部分區索引,不會自動維護全局索引,所以需要我們重新rebuild分區索引,具體需要rebuild哪些索引,可以通過dba_part_indexes,dba_ind_partitions去判斷。

shall@ORCL>Select index_name,status From user_indexes Where table_name='CUSTADDR';

INDEX_NAME                                                   STATUS

------------------------------------------------------------ ----------------

IX_CUSTADDR_ID                                               N/A

 

 

 

6.相關查詢

6.1 查詢表上有多少個分區

select * from user_tab_partitions where table_name='SALES';

select * from dba_tab_partitions where table_name='SALES';

 

 

6.2 查詢索引信息

select object_name,object_type,tablespace_name,sum(value)    from v$segment_statistics

 where statistic_name IN ('physical reads','physical write','logical reads')and object_type='INDEX'

 group by object_name,object_type,tablespace_name

 order by 4 desc;

 

6.3 查詢所有分區表信息

select * from dba_part_tables;

select * from all_part_tables;                   ---當前用戶可訪問的所有分區表信息

select * from user_part_tables;               ---當前用戶的所有分區表信息

 

6.4 查詢子分區信息

select * from dba_tab_subpartitions;

select * from all_tab_subpartitions;

select * from user_tab_subpartitions;

 

6.5 查詢分區列信息

select * from dba_part_key_columns;

select * from all_part_key_columns;

select * from user_part_key_columns;

 

6.6 查詢子分區列信息

select * from dba_subpart_key_columns;

select * from all_subpart_key_columns;

select * from user_subpart_key_columns;

 

6.7 查詢所有的分區表

select * from dba_tables where partitioned='YES';

select * from all_tables where partitioned='YES';

select * from user_tables where partitioned='YES';

 

 

7.普通錶轉分區表方法

reference                   http://blog.csdn.net/tianlesoftware/article/details/6218704

 

將普通錶轉換成分區表有4種方法:

1. Export/import method

2. Insert with a subquery method

3. Partition exchange method

4. DBMS_REDEFINITION

 

7.1 Export/import method(導入導出)

 

----創建普通表:

sys@ORCL>create table shall(id int,name varchar2(20));

sys@ORCL>insert into shall values(100,'zhong');

sys@ORCL>insert into shall values(101,'Jack');

sys@ORCL>insert into shall values(204,'shell');

sys@ORCL>commit;

 

----導出表

[oracle@zyx ~]$ exp \'/ as sysdba\' tables=shall file=shall.dmp

Export: Release 11.2.0.4.0 - Production on Tue Jun 28 12:32:03 2016

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

About to export specified tables via Conventional Path ...

. . exporting table                          SHALL          3 rows exported

Export terminated successfully without warnings.

[oracle@zyx ~]$

 

----刪除原普通表

sys@ORCL>drop table shall;

 

----重新創建爲分區表

create table shall(id int,name varchar2(20))

partition by range(id)

(        partition shall_part1 values less than (100),

         partition shall_part2 values less than (200),

         partition shall_part3 values less than (maxvalue)

);

 

----導入數據

[oracle@zyx ~]$ imp \'/ as sysdba\' file=shall.dmp tables=shall fromuser=sys touser=sys ignore=y;

Import: Release 11.2.0.4.0 - Production on Tue Jun 28 12:38:02 2016

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export file created by EXPORT:V11.02.00 via conventional path

. importing SYS's objects into SYS

. . importing table                        "SHALL"          3 rows imported

Import terminated successfully without warnings.

 

----測試

sys@ORCL>select * from shall;

        ID NAME

---------- ----------------------------------------

       100 zhong

       101 Jack

       204 shell

sys@ORCL>select * from shall partition(shall_part1);

no rows selected

sys@ORCL>select * from shall partition(shall_part2);

        ID NAME

---------- ----------------------------------------

       100 zhong

       101 Jack

sys@ORCL>select * from shall partition(shall_part3);

        ID NAME

---------- ----------------------------------------

       204 shell

sys@ORCL>select * from shall partition(shall_part3) union all select * from shall partition(shall_part2);

        ID NAME

---------- ----------------------------------------

       204 shell

       100 zhong

       101 Jack

sys@ORCL>select table_name,partition_name from user_tab_partitions where table_name='SHALL';

TABLE_NAME PARTITION_NAME

---------- --------------------

SHALL      SHALL_PART1

SHALL      SHALL_PART2

SHALL      SHALL_PART3

 

7.2 Insert with a subquery method(插入查詢)

    這種方法就是使用insert 來實現。 當然在創建分區表的時候可以一起插入數據,也可以創建好後在insert 進去。這種方法採用DDL語句,不產生UNDO,只產生少量REDO,建表完成後數據已經在分佈到各個分區中。

 

----創建普通表:

sys@ORCL>create table test2(id int,name varchar2(20));

sys@ORCL>insert into test2 values(100,'zhong');

sys@ORCL>insert into test2 values(140,'Jack');

sys@ORCL>insert into test2 values(240,'shell');

sys@ORCL>commit;

 

----創建分區表

create table part(id int,name varchar2(20))

partition by range(id)

(        partition part1 values less than (100),

         partition part2 values less than (200),

         partition part3 values less than (maxvalue)

);

 

----插入數據

sys@ORCL>insert into part  select * from test2;

sys@ORCL>commit;

sys@ORCL>select * from part;

 

----刪除原普通表,並將分區表更名爲原普通表

sys@ORCL>drop table test2;           ----確定不需要就刪除,不確定就rename to old

sys@ORCL>alter table part rename to test2;

 

----檢查測試

sys@ORCL>select * from test2;

sys@ORCL>select * from test2 partition (part1);

no rows selected

sys@ORCL>select * from test2 partition (part2);

        ID NAME

---------- ----------------------------------------

       100 zhong

       140 Jack

sys@ORCL>select * from test2 partition (part3);

        ID NAME

---------- ----------------------------------------

       240 shell

 

sys@ORCL>select table_name,partition_name from user_tab_partitions where table_name='TEST2';

TABLE_NAME PARTITION_

---------- ----------

TEST2      PART1

TEST2      PART2

TEST2      PART3

 

7.3 Partition exchange method(交換分區)

    這種方法只是對數據字典中分區和表的定義進行了修改,沒有數據的修改或複製,效率最高。適用於包含大數據量的錶轉到分區表中的一個分區的操作。儘量在閒時進行操作。

交換分區的操作步驟如下:

1. 創建分區表,假設有2個分區,P1,P2.

2. 創建表A存放P1規則的數據。

3. 創建表B 存放P2規則的數據。

4. 用表A 和P1 分區交換。 把表A的數據放到到P1分區

5. 用表B 和p2 分區交換。 把表B的數據存放到P2分區。

 

----創建分區表

create table p_emp (sal number(7,2))

partition by range(sal)

(        partition emp_p1 values less than (2000),

         partition emp_p2 values less than (4000)

);

 

----創建測試表

sys@ORCL>create table emp1 as select sal from scott.emp where sal<2000;

sys@ORCL>select count(*) from emp1;

  COUNT(*)

----------

         8

sys@ORCL>create table emp2 as select sal from scott.emp where sal between 2000 and 3999;

sys@ORCL>select count(*) from emp2;

  COUNT(*)

----------

         5

 

----將兩個基本表與兩個分區進行交換

----如果插入的數據不滿足分區規則,會報ORA-14400錯誤

sys@ORCL>alter table p_emp exchange partition emp_p1 with table emp1;

sys@ORCL>select count(*) from emp1;

  COUNT(*)

----------

         0

sys@ORCL>select count(*) from p_emp;

  COUNT(*)

----------

         8

sys@ORCL>alter table p_emp exchange partition emp_p2 with table emp2;

sys@ORCL>select count(*) from p_emp;

  COUNT(*)

----------

        13

sys@ORCL>select count(*) from emp2;

  COUNT(*)

----------

         0

 

----檢查測試

sys@ORCL>select count(*) from p_emp partition (emp_p1);

  COUNT(*)

----------

         8

sys@ORCL>select table_name,partition_name from user_tab_partitions where table_name='P_EMP';

TABLE_NAME PARTITION_NAME

---------- --------------------

P_EMP      EMP_P1

P_EMP      EMP_P2

 

7.4 DBMS_REDEFINITION(在線重定義)

    在線重定義能保證數據的一致性,在大部分時間內,表都可以正常進行DML操作。只在切換的瞬間鎖表,具有很高的可用性。這種方法具有很強的靈活性,對各種不同的需要都能滿足。而且,可以在切換前進行相應的授權並建立各種約束,可以做到切換完成後不再需要任何額外的管理操作。

 

reference                   http://blog.csdn.net/tianlesoftware/article/details/6218693

這個功能只在9.2.0.4以後的版本纔有,在線重定義表具有以下功能:

(1)修改表的存儲參數;

(2)將錶轉移到其他表空間;

(3)增加並行查詢選項;

(4)增加或刪除分區;

(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)創建基礎表A,如果存在,就不需要操作。

(2)創建臨時的分區表B。

(3)開始重定義,將基表A的數據導入臨時分區表B。

(4)結束重定義,此時在DB的 Name Directory裏,已經將2個表進行了交換。即此時基表A成了分區表,我們創建的臨時分區表B 成了普通表。 此時我們可以刪除我們創建的臨時表B。它已經是普通表。

 

----創建測試表

sys@ORCL>create user shall identified by shall;

sys@ORCL>grant connect,resource to shall;

sys@ORCL>grant select on dba_objects to shall;

shall@ORCL>create table zhong(id number(10) primary key,z_date date);

shall@ORCL>insert into zhong select rownum,created from dba_objects;

86435 rows created.

shall@ORCL>create index ind_zhong_z_date on zhong(z_date);

 

----收集統計信息

sys@ORCL>exec dbms_stats.gather_table_stats('shall','zhong',cascade => true);

 

----創建臨時分區表

create table par_table (id number primary key,z_time date)

partition by range(z_time)

(        partition part1 values less than (to_date('2013-7-1','yyyy-mm-dd')),

         partition part2 values less than (to_date('2014-7-1','yyyy-mm-dd')),

         partition part3 values less than (maxvalue)

);

 

----進行重定義操作

------檢查重定義的合理性

sys@ORCL>exec dbms_redefinition.can_redef_table('shall','zhong');

PL/SQL procedure successfully completed.

 

------如果沒有問題,開始重定義,這個過程可能要等一會

------這裏注意:如果分區表和原表列名相同,可以用如下方式進行:

begin

dbms_redefinition.start_redef_table

(        uname => 'SHALL',

         orig_table => 'zhong',

         int_table => 'par_table');

end;

/

 

------如果分區表的列名和原來的不一致,那麼在開始重定義的時候,需要重新指定映射關係:

exec dbms_redefinition.start_redef_table (   'SHALL',    'zhong',    'par_table',      'id id,z_date z_time',         dbms_redefinition.cons_use_pk);

------這一步操作結束後,數據就已經同步到這個臨時分區表裏了

shall@ORCL>select count(*) from par_table partition(part2);

  COUNT(*)

----------

     86198

 

----同步新表,這是可選操作

begin

dbms_redefinition.sync_interim_table

(        uname => 'SHALL',

         orig_table => 'zhong',

         int_table => 'par_table');

end;

/

 

----創建索引(在線重定義數據後,索引需要單獨建立)

shall@ORCL>create index ind_par_date on par_table(z_time);

 

----收集新表統計信息

sys@ORCL>exec dbms_stats.gather_table_stats('shall','par_table',cascade => true);

 

----結束重定義

begin

dbms_redefinition.finish_redef_table

(        uname => 'SHALL',

         orig_table => 'zhong',

         int_table => 'par_table');

end;

/

------結束重定義的意義:基表zhong 和臨時分區表par_table 進行了交換。 此時臨時分區表par_table成了普通表,我們的基表zhong成了分區表。我們在重定義的時候,基表zhong是可以進行DML操作的。 只有在2個表進行切換的時候會有短暫的鎖表。

 

----驗證:

shall@ORCL>select count(*) from par_table partition(part2);

select count(*) from par_table partition(part2)

                     *

ERROR at line 1:

ORA-14501: object is not partitioned

shall@ORCL>select count(*) from par_table;

  COUNT(*)

----------

     86435

 

shall@ORCL>drop table par_table;

shall@ORCL>alter index ind_par_date rename to ind_zhong_z_date;

 

shall@ORCL>select table_name,partition_name from user_tab_partitions where table_name='ZHONG';

TABLE_NAME           PARTITION_NAME

-------------------- ------------------------------

ZHONG                PART1

ZHONG                PART2

ZHONG                PART3

shall@ORCL>select count(*) from zhong;

  COUNT(*)

----------

     86435

shall@ORCL>select count(*) from zhong partition(part1);

  COUNT(*)

----------

         0

shall@ORCL>select count(*) from zhong partition(part2);

  COUNT(*)

----------

     86198

shall@ORCL>select count(*) from zhong partition(part3);

  COUNT(*)

----------

       237

 

8.分區表索引

    分區索引分爲本地(local index)索引和全局索引(global index)。局部索引比全局索引容易管理, 而全局索引比較快。

 

與索引有關的表:

dba_part_indexes :分區索引的概要統計信息,可以得知每個表上有哪些分區索引,分區索引的類型(local/global)

dba_ind_partitions: 每個分區索引的分區級統計信息

dba_indexes/dba_part_indexes: 可以得到每個表上有哪些非分區索引

 

Local索引肯定是分區索引,Global索引可以選擇是否分區,如果分區,只能是有前綴的分區索引。

 

分區索引分2類:有前綴(prefix)的分區索引和無前綴(nonprefix)的分區索引:

(1)有前綴的分區索引只包含了分區鍵,並且將其作爲引導列的索引。

如:

create index i_id_global on PDBA(id) global    ----引導列

 partition by range(id)      ----分區鍵

 (       partition p1 values less than (200),

         partition p2 values less than (maxvalue)

 );

這裏的ID 就是分區鍵,並且分區鍵id 也是索引的引導列。

 

 

(2)無前綴的分區索引的列不是以分區鍵開頭,或者不包含分區鍵列。

如:

create index ix_custaddr_local_id_p on custaddr(id)

local (        partition t_list556 tablespace test_ts15,

                   partition p_other tablespace test_ts15

);

這個分區是按照areacode來的。但是索引的引導列是ID。 所以它就是非前綴分區索引。

 

全局分區索引不支持非前綴的分區索引,如果創建,報錯如下:

create index i_time_global on PDBA(id) global        ----索引引導列

partition by range(time)          ----分區鍵

(        partition p1 values less than (TO_DATE('2010-12-1', 'YYYY-MM-DD')),

         partition p2 values less than (maxvalue)

);

partition by range(time)

*

第 2 行出現錯誤:

ORA-14038: GLOBAL 分區索引必須加上前綴

 

8.1 Local本地索引

    對於local索引,當表的分區發生變化時,索引的維護由Oracle自動進行。

注意事項:

1> 局部索引一定是分區索引,分區鍵等同於表的分區鍵。

2> 前綴和非前綴索引都可以支持索引分區消除,前提是查詢的條件中包含索引分區鍵。

3> 局部索引只支持分區內的唯一性,無法支持表上的唯一性,因此如果要用局部索引去給表做唯一性約束,則約束中必須要包括分區鍵列。

4> 局部分區索引是對單個分區的,每個分區索引只指向一個表分區;全局索引則不然,一個分區索引能指向n個表分區,同時,一個表分區,也可能指向n個索引分區,對分區表中的某個分區做truncate或者move,shrink等,可能會影響到n個全局索引分區,正因爲這點,局部分區索引具有更高的可用性。

5> 位圖索引必須是局部分區索引。

6> 局部索引多應用於數據倉庫環境中。

7> B樹索引和位圖索引都可以分區,但是HASH索引不可以被分區。

 

示例:

shall@ORCL>drop index IX_CUSTADDR_ID;

shall@ORCL> create index ix_custaddr_local_id on custaddr(id) local;

和下面SQL 效果相同,因爲local索引就是分區索引:

shall@ORCL>Select index_name,status From user_indexes Where table_name='CUSTADDR';

shall@ORCL>drop index IX_CUSTADDR_LOCAL_ID;

create index ix_custaddr_local_id_p on custaddr(id)

local (

partition t_list555 tablespace test_ts15,

partition p_other tablespace test_ts15

);

create index ix_custaddr_local_id_p on custaddr(id)

                                       *

ERROR at line 1:

ORA-14024: number of partitions of LOCAL index must equal that of the underlying table

 

 

shall@ORCL>select partition_name from user_tab_partitions where table_name=upper('custaddr');

PARTITION_NAME

------------------------------------------------------------

P_OTHER

T_LIST552

T_LIST555

 

create index ix_custaddr_local_id_p on custaddr(id)

local (

partition t_list552 tablespace test_ts15,

partition t_list555 tablespace test_ts15,

partition p_other tablespace test_ts15

);

 

create index ix_custaddr_local_areacode on custaddr(areacode) local;

 

驗證2個索引的類型:

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_areacode            custaddr           list              local         prefixed

ix_custaddr_local_id                 custaddr            list             local          non_prefixed

 

    因爲我們的custaddr表是按areacode進行分區的,所以索引ix_custaddr_local_areacode是有前綴的索引(prefixed)。而ix_custaddr_local_id是非前綴索引。

 

 

4.2 Global索引

    對於global索引,可以選擇是否分區,而且索引的分區可以不與表分區相對應。全局分區索引只能是B樹索引,到目前爲止(10gR2),oracle只支持有前綴的全局索引

    另外oracle不會自動的維護全局分區索引,當我們在對錶的分區做修改之後,如果對分區進行維護操作時不加上update global indexes的話,通常會導致全局索引的INVALDED,必須在執行完操作後 REBUILD

 

注意事項:

1> 全局索引可以分區,也可以是不分區索引,全局索引必須是前綴索引,即全局索引的索引列必須是以索引分區鍵作爲其前幾列。

2> 全局索引可以依附於分區表,也可以依附於非分區表。

3> 全局分區索引的索引條目可能指向若干個分區,因此,對於全局分區索引,即使只截斷一個分區中的數據,都需要rebulid若干個分區甚至是整個索引

4> 全局索引多應用於oltp系統中。

5> 全局分區索引只按範圍或者散列分區,hash分區是10g以後才支持。

6> oracle9i以後對分區表做move或者truncate的時可以用update global indexes語句來同步更新全局分區索引,用消耗一定資源來換取高度的可用性。

7> 表用a列作分區,索引用b做局部分區索引,若where條件中用b來查詢,那麼oracle會掃描所有的表和索引的分區,成本會比分區更高,此時可以考慮用b做全局分區索引。

 

注意:Oracle只支持2中類型的全局分區索引:

range partitioned 和 Hash Partitioned.

官網的說明如下:

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 全局索引,全局索引對所有分區類型都支持:

Select index_name,status From user_indexes Where table_name='CUSTADDR';

drop index IX_CUSTADDR_LOCAL_ID_P;

create index ix_custaddr_global_id on custaddr(id) global;

 

----示例2:全局分區索引,只支持Range 分區和Hash 分區:

1> 創建2個測試分區表:

create table pdba (id number, time date) 

partition by range (time)

(         partition p1 values less than (to_date('2016-1-1', 'yyyy-mm-dd')),

          partition p2 values less than (to_date('2016-6-1', 'yyyy-mm-dd')),

          partition p3 values less than (to_date('2016-12-1', 'yyyy-mm-dd')),

          partition p4 values less than (maxvalue)

          );

 

create table Thash

 (        id number primary key,

          item_id number(8) not null

          )

 partition by hash(id)

 (        partition part_01,

          partition part_02,

          partition part_03

 );

 

2> 創建分區索引

----示例2:全局分區索引

create index i_id_global on PDBA(id) global

 partition by range(id)

 (       partition p1 values less than (200),

         partition p2 values less than (maxvalue)

 );

----這個是有前綴的分區索引。

 

create index i_time_global on PDBA(id) global

 partition by range(time)

 (       partition p1 values less than (TO_DATE('2010-12-1', 'YYYY-MM-DD')),

         partition p2 values less than (maxvalue)

 );

partition by range(time)

*

第 2 行出現錯誤:

ORA-14038: GLOBAL 分區索引必須加上前綴

 

create index i_time_global on PDBA(time) global

 partition by range(time)

 (       partition p1 values less than (TO_DATE('2016-12-1', 'YYYY-MM-DD')),

         partition p2 values less than (maxvalue)

 );

----有前綴的分區索引

 

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        range         global      prefixed

i_time_global   pdba        range        global       prefixed

 

CREATE INDEX ix_hash ON PDBA (id,time) GLOBAL

 PARTITION BY HASH (id)

 (       PARTITION p1,

         PARTITION p2,

         PARTITION p3,

         PARTITION p4);

----只要索引的引導列包含分區鍵,就是有前綴的分區索引。

 

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        range         global      prefixed

i_time_global   pdba        range        global       prefixed

ix_hash            pdba         hash                   global       prefixed

 

4.3 索引重建問題

1> 分區索引重建

    對於分區索引,不能整體進行重建,只能對單個分區進行重建。語法如下:

Alter index idx_name rebuild partition index_partition_name [online nologging]

說明:

online:表示重建的時候不會鎖表。

nologging:表示建立索引的時候不生成日誌,加快速度。

 

Select index_name,status From user_indexes Where table_name='PDBA';

select PARTITION_NAME from user_tab_partitions where table_name='PDBA';

select partition_name from user_ind_partitions where index_name='I_ID_GLOBAL';

alter index I_ID_GLOBAL rebuild partition p1;

alter index I_TIME_GLOBAL rebuild partition p2 online;

 

 

    如果要重建整個分區索引,只能drop表原索引,在重新創建:

drop index I_ID_GLOBAL;

create index i_id_global on PDBA(id) local tablespace test_ts15;

select partition_name,tablespace_name from user_ind_partitions where index_name='I_ID_GLOBAL';

 

----在線重建操作要求較大的臨時表空間和排序區:

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

 

alter index I_TIME_GLOBAL rebuild partition p1 online nologging;

alter index I_TIME_GLOBAL rebuild partition p2 online nologging;

 

 

2> 全局索引重建

    Oracle 會自動維護分區索引,對於全局索引,如果在對分區表操作時,沒有指定update index,則會導致全局索引失效,需要重建。

Select index_name,status From user_indexes Where table_name='PDBA';

select index_name,table_name,status from user_indexes where INDEX_NAME='I_ID_GLOBAL';

index_name     table_name    status

------------------------------ ------------------------------ ---------- -------

I_ID_GLOBAL             pdba                  valid

 

----刪除一個分區:

select PARTITION_NAME from user_tab_partitions where table_name='PDBA';

select partition_name from user_ind_partitions where index_name='I_ID_GLOBAL';

alter table pdba drop partition p2;

 

select index_name,table_name,status from user_indexes where INDEX_NAME='I_ID_GLOBAL';

index_name     table_name    status

------------------------------ ------------------------------ ---------- -------

I_ID_GLOBAL             pdba                  valid

 

----split 分區:

alter table pdba split partition P4 at(TO_DATE('2016-12-21 00:00:00','YYYY-MM-DD HH24:MI:SS')) into (partition P4, partition P5);

 

select PARTITION_NAME from user_tab_partitions where table_name='PDBA';

select partition_name from user_ind_partitions where index_name='I_ID_GLOBAL';

 

select index_name,table_name,status from user_indexes where INDEX_NAME='I_ID_GLOBAL';

index_name     table_name    status

------------------------------ ------------------------------ ---------- -------

I_ID_GLOBAL             pdba                  valid

 

 

----drop 分區時使用update indexes

alter table pdba drop partition P4 UPDATE INDEXES;

select index_name,table_name,status from user_indexes where INDEX_NAME='I_ID_GLOBAL';

index_name     table_name    status

------------------------------ ------------------------------ ---------- -------

I_ID_GLOBAL             pdba                  valid

 

----做了幾個drop分區操作,全局索引沒有失效,有點奇怪。 不過如果在生產環境中,還是小心點。

 

 

重建全局索引命令如下:

Alter index idx_name rebuild [online nologging]

示例:

Select index_name,status From user_indexes Where table_name='PDBA';

select PARTITION_NAME from user_tab_partitions where table_name='PDBA';

select partition_name from user_ind_partitions where index_name='I_ID_GLOBAL';

 

SQL> Alter index I_ID_GLOBAL rebuild online nologging;

索引已更改。

 

    select table_name,partition_name,tablespace_name from user_tab_partitions where table_name='PDBA';

    通過user_tab_partitions 表可以查看到每個分區對應的tablesapce_name. 但是,如果通過all_tables 表,卻查不到分區表對應表空間的信息。

分區表:看不到tablespace_name的

SQL> select owner,table_name,tablespace_name,cluster_name from all_tables where table_name='PDBA';

普通表:是可以看到tablespace_name的

SQL> select owner,table_name,tablespace_name,cluster_name from all_tables where table_name='test1';

 

3> 分區索引與全局索引 重建案例2

reference                            http://blog.csdn.net/weiwangsisoftstone/article/details/37615245

 

----創建分區表:

create table test( 

      id number, 

      name varchar2(20) 

partition by range(id)

(       partition p1 values less than (1000), 

          partition p2 values less than (2000), 

          partition p3 values less than (maxvalue) 

); 

 

----創建分區索引

----LOCAL索引結構

create index ind_test_id_local on test(id) local;

 

----重新在name列上創建一個GLOBAL的索引

create index ind_test_name_global on test(name) global;

 

Select index_name,status From user_indexes Where table_name='TEST';

select PARTITION_NAME from user_tab_partitions where table_name='TEST';

select partition_name from user_ind_partitions where index_name='IND_TEST_ID_LOCAL';

select partition_name from user_ind_partitions where index_name='IND_TEST_NAME_GLOBAL';

 

insert into test values(999,'p1');

insert into test values(1999,'p2');

insert into test values(2999,'p3');

 

SQL> select * from test;

        ID NAME

---------- ----------

       999 p1

      1999 p2

      2999 p3

 

----查詢當前用戶下有哪些是分區表: 

SELECT table_name, partitioning_type,partition_count FROM USER_PART_TABLES;

TABLE_NAME                     PARTITION PARTITION_COUNT

------------------------------ --------- ---------------

TEST                           RANGE                   3

 

----查詢當前用戶下有哪些分區索引: 

SELECT index_name,table_name FROM USER_PART_INDEXES;

INDEX_NAME                     TABLE_NAME

------------------------------ ------------------------------

ID_LOCAL                       TEST

 

----索引對應的分區以及索引的狀態

select index_name,partition_name,status  from user_ind_partitions;

INDEX_NAME                     PARTITION_NAME                 STATUS

------------------------------ ------------------------------ --------

ID_LOCAL                       P2                             USABLE

ID_LOCAL                       P1                             USABLE

ID_LOCAL                       P3                             USABLE

 

----移動分區表使索引失效

alter table test move partition p1  tablespace users;

alter table test move partition p2  tablespace users;

 

----本地分區失效

select index_name,partition_name,status  from user_ind_partitions;

INDEX_NAME                     PARTITION_NAME       STATUS

------------------------------ -------------------- ----------------

IND_TEST_ID_LOCAL              P3                   USABLE

IND_TEST_ID_LOCAL              P2                   UNUSABLE

IND_TEST_ID_LOCAL              P1                   UNUSABLE

 

----重建分區索引

alter index IND_TEST_ID_LOCAL rebuild partition p1;

 

----索引狀態已更改。

select index_name,partition_name,status  from user_ind_partitions;

INDEX_NAME                     PARTITION_NAME       STATUS

------------------------------ -------------------- ----------------

IND_TEST_ID_LOCAL              P1                   USABLE

IND_TEST_ID_LOCAL              P3                   USABLE

IND_TEST_ID_LOCAL              P2                   UNUSABLE

 

alter table test modify partition p2 rebuild unusable local indexes;

SQL> select index_name,partition_name,status  from user_ind_partitions;

INDEX_NAME                     PARTITION_NAME       STATUS

------------------------------ -------------------- ----------------

IND_TEST_ID_LOCAL              P2                   USABLE

IND_TEST_ID_LOCAL              P1                   USABLE

IND_TEST_ID_LOCAL              P3                   USABLE

 

----全局索引的狀態:

select index_name,table_name,status from user_indexes where index_name='IND_TEST_NAME_GLOBAL';

INDEX_NAME                     TABLE_NAME                     STATUS

------------------------------ ------------------------------ --------

IND_TEST_NAME_GLOBAL                    TEST                           UNUSABLE

 

----重建全局分區索引

alter index IND_TEST_NAME_GLOBAL rebuild;

----索引狀態已更改。

select index_name,table_name,status from user_indexes where index_name='IND_TEST_NAME_GLOBAL';

INDEX_NAME                     TABLE_NAME                     STATUS

------------------------------ ------------------------------ --------

IND_TEST_NAME_GLOBAL                    TEST                          VALID

                  

----刪除分區也會導致全局分區索引失效

alter table test truncate partition p1;

 

select index_name,partition_name,status  from user_ind_partitions;

INDEX_NAME                     PARTITION_NAME       STATUS

------------------------------ -------------------- ----------------

IND_TEST_ID_LOCAL              P2                   USABLE

IND_TEST_ID_LOCAL              P1                   USABLE

IND_TEST_ID_LOCAL              P3                   USABLE

 

select index_name,table_name,status from user_indexes where index_name='IND_TEST_NAME_GLOBAL';

INDEX_NAME                     TABLE_NAME                     STATUS

------------------------------ ------------------------------ --------

IND_TEST_NAME_GLOBAL                    TEST                           UNUSABLE

 

 

 

 

 

reference                            http://www.cnblogs.com/leiOOlei/archive/2012/06/08/2541306.html

                                            http://blog.csdn.net/hijiankang/article/details/9173877

 

 

 

 

 

來自 “ ITPUB博客 ” ,鏈接:http://blog.itpub.net/30130773/viewspace-2121413/,如需轉載,請註明出處,否則將追究法律責任。

轉載於:http://blog.itpub.net/30130773/viewspace-2121413/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章