Oracle分区交换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle分区交换相关的知识,希望对你有一定的参考价值。

从Oracle8开始,提供了从分区交换的功能,如一个分区或子分区与一个非分区表交换、一个hash分区与另一个表的hash子分区交换等等,详细的交换方式可以参考官方文档。

基本语法:ALTER TABLE...EXCHANGE PARTITION

实验环境:11.2.0.4

[email protected]>select * from v$version;

BANNER
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE    11.2.0.4.0      Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production

一、测试分区交换

创建测试表

--分区表
[email protected]>create table t1
  2  ( id number(2),
  3  name varchar2(15))
  4  tablespace tt
  5  partition by range (id)
  6  (partition p1 values less than (10),
  7  partition p2 values less than (20),
  8  partition p3 values less than (30));

Table created.
--非分区表
[email protected]>create table t2 (id number(2), name varchar2(15)) tablespace users;

Table created.

--插入测试数据
[email protected]>insert into t1 values (1, ‘1‘);

1 row created.

[email protected]>insert into t1 values (11, ‘11‘);

1 row created.

[email protected]>insert into t1 values (21, ‘21‘);

1 row created.

[email protected]>insert into t2 values (2, ‘2‘);

1 row created.

[email protected]>commit;

Commit complete.

[email protected]>select * from t1;

        ID NAME
---------- ---------------------------------------------
         1 1
        11 11
        21 21

[email protected]>select * from t2;

        ID NAME
---------- ---------------------------------------------
         2 2
--查看表存储表空间
--t2在USERS表空间,t1各个分区都在TT表空间
[email protected]>col segment_name for a20
[email protected]>col partition_name for a15
[email protected]>col tablespace_name for a15
[email protected]>select segment_name,partition_name,tablespace_name from dba_segments where segment_name in (‘T1‘,‘T2‘);

SEGMENT_NAME         PARTITION_NAME  TABLESPACE_NAME
-------------------- --------------- ---------------
T2                                   USERS
T1                   P3              TT
T1                   P2              TT
T1                   P1              TT
--查看各表的extent信息
[email protected]>select SEGMENT_NAME,BLOCK_ID,BLOCKS,TABLESPACE_NAME from dba_extents where segment_name=‘T2‘;

SEGMENT_NAME           BLOCK_ID     BLOCKS TABLESPACE_NAME
-------------------- ---------- ---------- ---------------
T2                          192          8 USERS

[email protected]>select SEGMENT_NAME,PARTITION_NAME,BLOCK_ID,BLOCKS,TABLESPACE_NAME from dba_extents where segment_name=‘T1‘;

SEGMENT_NAME         PARTITION_NAME    BLOCK_ID     BLOCKS TABLESPACE_NAME
-------------------- --------------- ---------- ---------- ---------------
T1                   P2                   21376       1024 TT
T1                   P3                   22400       1024 TT
T1                   P1                   20352       1024 TT

t1分区p1与t2表交换分区

--分区
[email protected]>alter table t1 exchange partition p1 with table t2;

Table altered.

[email protected]>select * from t2;

        ID NAME
---------- ---------------------------------------------
         1 1

[email protected]>select * from t1;

        ID NAME
---------- ---------------------------------------------
         2 2
        11 11
        21 21

可以看到p1分区里的数据交换到了t2表里,而t2表里里的数据也存储到了t1表中。再次查看各表所在的表空间和extent

--查看表空间
[email protected]>select segment_name,partition_name,tablespace_name from dba_segments where segment_name in (‘T1‘,‘T2‘);

SEGMENT_NAME         PARTITION_NAME  TABLESPACE_NAME
-------------------- --------------- ---------------
T2                                   TT
T1                   P3              TT
T1                   P2              TT
T1                   P1              USERS
--查看extent
[email protected]>select SEGMENT_NAME,BLOCK_ID,BLOCKS,TABLESPACE_NAME from dba_extents where segment_name=‘T2‘;

SEGMENT_NAME           BLOCK_ID     BLOCKS TABLESPACE_NAME
-------------------- ---------- ---------- ---------------
T2                        20352       1024 TT

[email protected]>select SEGMENT_NAME,PARTITION_NAME,BLOCK_ID,BLOCKS,TABLESPACE_NAME from dba_extents where segment_name=‘T1‘;

SEGMENT_NAME         PARTITION_NAME    BLOCK_ID     BLOCKS TABLESPACE_NAME
-------------------- --------------- ---------- ---------- ---------------
T1                   P1                     192          8 USERS
T1                   P2                   21376       1024 TT
T1                   P3                   22400       1024 TT

从结果看到T2已经到了TT表空间,而T1的P1分区移动到了USERS表空间,而且P1分区与T2表的extent也做了交换,可以推断实际表里的数据没有移动位置,只是把数据字典里的相关信息做了更换。

二、再看看交换分区对于分区表的索引的影响

在分区表中创建索引

--全局索引
[email protected]>create index idx_t1_id on t1(id) ;

Index created.
--分区索引
[email protected]>create index idx_t1_name on t1(name) local;

Index created.

[email protected]>select index_name,status from user_indexes where index_name like ‘IDX_T1%‘;

INDEX_NAME                                                                                 STATUS
------------------------------------------------------------------------------------------ ------------------------
IDX_T1_ID                                                                                  VALID
IDX_T1_NAME                                                                                N/A

[email protected]>select index_name,partition_name,status from user_ind_partitions where index_name like ‘IDX_T1%‘;

INDEX_NAME                                                                                 PARTITION_NAME  STATUS
------------------------------------------------------------------------------------------ --------------- ------------------------
IDX_T1_NAME                                                                                P1              USABLE
IDX_T1_NAME                                                                                P2              USABLE
IDX_T1_NAME                                                                                P3              USABLE

交换分区查看是否对索引有影响

[email protected]>alter table t1 exchange partition p1 with table t2;

Table altered.

[email protected]>select index_name,status from user_indexes where index_name like ‘IDX_T1%‘;

INDEX_NAME                                                                                 STATUS
------------------------------------------------------------------------------------------ ------------------------
IDX_T1_NAME                                                                                N/A
IDX_T1_ID                                                                                  UNUSABLE

[email protected]>select index_name,partition_name,status from user_ind_partitions where index_name like ‘IDX_T1%‘;

INDEX_NAME                                                                                 PARTITION_NAME  STATUS
------------------------------------------------------------------------------------------ --------------- ------------------------
IDX_T1_NAME                                                                                P1              UNUSABLE
IDX_T1_NAME                                                                                P2              USABLE
IDX_T1_NAME                                                                                P3              USABLE

看到全局索引IDX_T1_ID失效了,分区P1对应的分区索引也失效了,但其他分区的分区没有受到影响

交换分区时加入 UPDATE INDEXES子句

[email protected]>alter index idx_t1_id rebuild;

Index altered.

[email protected]>alter index idx_t1_name rebuild partition p1;

Index altered.

[email protected]>select index_name,status from user_indexes where index_name like ‘IDX_T1%‘;

INDEX_NAME                                                                                 STATUS
------------------------------------------------------------------------------------------ ------------------------
IDX_T1_NAME                                                                                N/A
IDX_T1_ID                                                                                  VALID

[email protected]>select index_name,partition_name,status from user_ind_partitions where index_name like ‘IDX_T1%‘;

INDEX_NAME                                                                                 PARTITION_NAME  STATUS
------------------------------------------------------------------------------------------ --------------- ------------------------
IDX_T1_NAME                                                                                P1              USABLE
IDX_T1_NAME                                                                                P2              USABLE
IDX_T1_NAME                                                                                P3              USABLE

[email protected]>alter table t1 exchange partition p1 with table t2 update indexes ;

Table altered.

[email protected]>select index_name,status from user_indexes where index_name like ‘IDX_T1%‘;

INDEX_NAME                                                                                 STATUS
------------------------------------------------------------------------------------------ ------------------------
IDX_T1_NAME                                                                                N/A
IDX_T1_ID                                                                                  VALID

[email protected]>select index_name,partition_name,status from user_ind_partitions where index_name like ‘IDX_T1%‘;

INDEX_NAME                                                                                 PARTITION_NAME  STATUS
------------------------------------------------------------------------------------------ --------------- ------------------------
IDX_T1_NAME                                                                                P1              UNUSABLE
IDX_T1_NAME                                                                                P2              USABLE
IDX_T1_NAME                                                                                P3              USABLE

可以看到全局索引没有受影响,但是分区索引仍然失效。


更多信息参考官方文档:http://docs.oracle.com/cd/E11882_01/server.112/e25523/part_admin002.htm#i1107555


本文出自 “DBA Fighting!” 博客,请务必保留此出处http://hbxztc.blog.51cto.com/1587495/1892937

以上是关于Oracle分区交换的主要内容,如果未能解决你的问题,请参考以下文章

Oracle用分区表分区交换做历史数据迁移

oracle交换分区方法以及注意事项

oracle数据库管理安装到物理内存交换空间时为啥失败?

linux怎么安装oracle

求助,分区无法交换 ORA-14097

linux内存不足,swap交换分区创建