oracle 中在添加分区中如果要在表的开始或中间的位置添加分区,怎么使用split语句,帮忙举个例子谢谢

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle 中在添加分区中如果要在表的开始或中间的位置添加分区,怎么使用split语句,帮忙举个例子谢谢相关的知识,希望对你有一定的参考价值。

参考技术A 下面的SQL,将 p2010Q1 分区,拆分为 P2009 与 P2010 两个分区

SQL> ALTER TABLE sale_data
2 SPLIT PARTITION P2010Q1 AT(TO_DATE('2010-01-01','YYYY-MM-DD'))
3 INTO (PARTITION P2009,PARTITION P2010);
表已更改。

核对结果
SQL> SELECT
2 partition_name, high_value
3 FROM
4 user_tab_partitions
5 WHERE
6 table_name = 'SALE_DATA';

PARTITION_NAME
------------------------------
HIGH_VALUE
--------------------------------------------------------------------------------
P2009
TO_DATE(' 2010-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA

P2010
TO_DATE(' 2010-04-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA

P2010Q2
TO_DATE(' 2010-07-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA

PARTITION_NAME
------------------------------
HIGH_VALUE
--------------------------------------------------------------------------------
P2010Q3
TO_DATE(' 2010-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA

参考资料:http://hi.baidu.com/wangzhiqing999/blog/item/f9cd493766ea2a55ac4b5f61.html

本回答被提问者采纳

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 中在添加分区中如果要在表的开始或中间的位置添加分区,怎么使用split语句,帮忙举个例子谢谢的主要内容,如果未能解决你的问题,请参考以下文章

更改 SQL Server 2008 中在表的计算列中引用的标量函数

12c中在 RMAN 中提供了表级别恢复 RECOVER TABLE

Oracle数据库分区表操作方法

通过动态增加拆分在 oracle 中创建拆分分区

为啥C#中数据库sql语句中在表的前面加上.dbo,不加也能正常执行,两者有啥区别!

Oracle分区交换