Oracle 12.2新特性----在线把非分区表转为分区表

在Oracle12.2版本之前,如果想把一个非分区表转为分区表常用的有这几种方法:1、建好分区表然后insert into select 把数据插入到分区表中;2、使用在线重定义(DBMS_REDEFinitioN)的方法。它们的币是:第一种方法,如果对表有频繁的DML操作,尤其是update操作,就需要停业务来做转换。第二种方法可以在线进行操作,不需要停业务,但操作步骤比较复杂,且可能出错。

Oracle12cR2版本中提供了一种新特性,一条语句就可以把非分区表转换为分区表,语法如下:

ALTERTABLEtable_nameMODIFYtable_partitioning_clauses
[filter_condition]
[ONLINE]
[UPDATEINDEXES[(index{local_partitioned_index|global_partitioned_index|GLOBAL}
[,index{local_partitioned_index|global_partitioned_index|GLOBAL}]...)
]
]

下面来测试一下这个新特性

1、创建测试表及相关索引,并查看状态

zx@ORA12C>createtableempasselect*fromscott.emp;

Tablecreated.

zx@ORA12C>createindexidx_emp_noonemp(empno);

Indexcreated.

zx@ORA12C>createindexidx_emp_jobonemp(job);

Indexcreated.

zx@ORA12C>coltable_namefora30
zx@ORA12C>colindex_namefora30
zx@ORA12C>selecttable_name,partitionedfromuser_tableswheretable_name='EMP';

TABLE_NAMEPAR
---------------------------------
EMPNO

zx@ORA12C>selectindex_name,partitioned,statusfromuser_indexeswheretable_name='EMP';

INDEX_NAMEPARSTATUS
-----------------------------------------
IDX_EMP_NONovalID
IDX_EMP_JOBNovalID

2、使用alter table语句,执行分区表转换操作

zx@ORA12C>altertableempmodify
2partitionbyrange(deptno)interval(10)
3(partitionp1valueslessthan(10),4partitionp2valueslessthan(20)
5)online
6;

Tablealtered.

3、查看现在的表和索引的状态

zx@ORA12C>selecttable_name,partitionedfromuser_tableswheretable_name='EMP';

TABLE_NAMEPAR
---------------------------------
EMPYES

zx@ORA12C>selectindex_name,statusfromuser_indexeswheretable_name='EMP';

INDEX_NAMEPARSTATUS
-----------------------------------------
IDX_EMP_NONovalID
IDX_EMP_JOBNovalID

zx@ORA12C>selecttable_name,partition_namefromuser_tab_partitionswheretable_name='EMP';

TABLE_NAMEPARTITION_NAME
------------------------------------------------------------
EMPP1
EMPP2
EMPSYS_P405
EMPSYS_P406

现在表EMP已经被转换为分区表了,索引转换为分区索引,但索引状态是正常的。

4、如果想在转换表时同时转换索引可以使用UPDATE INDEXES子句

zx@ORA12C>altertableempmodify
2partitionbyrange(deptno)interval(10)
3(partitionp1valueslessthan(10),4partitionp2valueslessthan(20)
5)online
6updateindexes
7(idx_emp_nolocal)
8;

Tablealtered.

zx@ORA12C>coltable_namefora30
zx@ORA12C>colindex_namefora30
zx@ORA12C>selecttable_name,statusfromuser_indexeswheretable_name='EMP';

INDEX_NAMEPARSTATUS
-----------------------------------------
IDX_EMP_NOYESN/A
IDX_EMP_JOBNovalID

zx@ORA12C>selecttable_name,partition_namefromuser_tab_partitionswheretable_name='EMP';

TABLE_NAMEPARTITION_NAME
------------------------------------------------------------
EMPP1
EMPP2
EMPSYS_P403
EMPSYS_P404

zx@ORA12C>selectindex_name,partition_name,statusfromuser_ind_partitionswhereindex_name='IDX_EMP_NO';

INDEX_NAMEPARTITION_NAMESTATUS
--------------------------------------------------------------------
IDX_EMP_nop1USABLE
IDX_EMP_nop2USABLE
IDX_EMP_NOSYS_P403USABLE
IDX_EMP_NOSYS_P404USABLE

从上面的执行结果来看,不仅表EMP转换为分区表,而且索引IDX_EMP_NO也转换分区索引,所有索引状态均正常。

下面是官方文档里的一些注意事项:

When using theUPDATEINDEXESclause,note the following.

  • This clause can be used to change the partitioning state of indexes and storage properties of the indexes being converted.

  • The specification of theINDEXESclause is optional.

    Indexes are maintained both for the online and offline conversion to a partitioned table.

  • This clause cannot change the columns on which the original list of indexes are defined.

  • This clause cannot change the uniqueness property of the index or any other index property.

  • If you do not specify the tablespace for any of the indexes,then the following tablespace defaults apply.

    • Local indexes after the conversion collocate with the table partition.

    • Global indexes after the conversion reside in the same tablespace of the original global index on the non-partitioned table.

  • If you do not specify theINDEXESclause or theINDEXESclause does not specify all the indexes on the original non-partitioned table,then the following default behavior applies for all unspecified indexes.

    • Global partitioned indexes remain the same and retain the original partitioning shape.

    • Non-prefixed indexes become global nonpartitioned indexes.

    • Prefixed indexes are converted to local partitioned indexes.

      Prefixed means that the partition key columns are included in the index deFinition,but the index deFinition is not limited to including the partitioning keys only.

    • Bitmap indexes become local partitioned indexes,regardless whether they are prefixed or not.

      Bitmap indexes must always be local partitioned indexes.

  • The conversion operation cannot be performed if there are domain indexes.

参考:http://docs.oracle.com/database/122/VLDBG/evolve-nopartition-table.htm#VLDBG-GUID-5FDB7D59-DD05-40E4-8AB4-AF82EA0D0FE5

相关文章

Java Oracle 结果集是Java语言中处理数据库查询结果的一种方...
Java AES和Oracle AES是现代加密技术中最常使用的两种AES加密...
Java是一种广泛应用的编程语言,具备可靠性、安全性、跨平台...
随着移动互联网的发展,抽奖活动成为了营销活动中不可或缺的...
Java和Oracle都是在计算机领域应用非常广泛的技术,他们经常...
Java 是一门非常流行的编程语言,它可以运行于各种操作系统上...