Oracle 12.2新特性----在线把非分区表转为分区表
admin
2023-04-26 13:23:24
0

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

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

ALTER TABLE table_name MODIFY table_partitioning_clauses
  [ filter_condition ]
  [ ONLINE ]
  [ UPDATE INDEXES [ ( index { local_partitioned_index | global_partitioned_index | GLOBAL }
                     [, index { local_partitioned_index | global_partitioned_index | GLOBAL } ]... )
                   ]
  ]

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

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

zx@ORA12C>create table emp as select * from scott.emp;

Table created.

zx@ORA12C>create index idx_emp_no on emp(empno);

Index created.

zx@ORA12C>create index idx_emp_job on emp(job);

Index created.

zx@ORA12C>col table_name for a30
zx@ORA12C>col index_name for a30
zx@ORA12C>select table_name,partitioned from user_tables where table_name='EMP';

TABLE_NAME                     PAR
------------------------------ ---
EMP                            NO

zx@ORA12C>select index_name,partitioned,status from user_indexes where table_name='EMP';

INDEX_NAME                     PAR STATUS
------------------------------ --- --------
IDX_EMP_NO                     NO  VALID
IDX_EMP_JOB                    NO  VALID

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

zx@ORA12C>alter table emp modify
  2    partition by range (deptno) interval (10)
  3    ( partition p1 values less than (10),
  4      partition p2 values less than (20)
  5    ) online
  6  ;

Table altered.

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

zx@ORA12C>select table_name,partitioned from user_tables where table_name='EMP';

TABLE_NAME                     PAR
------------------------------ ---
EMP                            YES

zx@ORA12C>select index_name,partitioned,status from user_indexes where table_name='EMP';

INDEX_NAME                     PAR STATUS
------------------------------ --- --------
IDX_EMP_NO                     NO  VALID
IDX_EMP_JOB                    NO  VALID

zx@ORA12C>select table_name,partition_name from user_tab_partitions where table_name='EMP';

TABLE_NAME                     PARTITION_NAME
------------------------------ ------------------------------
EMP                            P1
EMP                            P2
EMP                            SYS_P405
EMP                            SYS_P406

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

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

zx@ORA12C>alter table emp modify
  2    partition by range (deptno) interval (10)
  3    ( partition p1 values less than (10),
  4      partition p2 values less than (20)
  5    ) online
  6    update indexes
  7    (idx_emp_no local)
  8  ;

Table altered.

zx@ORA12C>col table_name for a30
zx@ORA12C>col index_name for a30
zx@ORA12C>select table_name,partitioned from user_tables where table_name='EMP';

TABLE_NAME                     PAR
------------------------------ ---
EMP                            YES

zx@ORA12C>select index_name,partitioned,status from user_indexes where table_name='EMP';

INDEX_NAME                     PAR STATUS
------------------------------ --- --------
IDX_EMP_NO                     YES N/A
IDX_EMP_JOB                    NO  VALID

zx@ORA12C>select table_name,partition_name from user_tab_partitions where table_name='EMP';

TABLE_NAME                     PARTITION_NAME
------------------------------ ------------------------------
EMP                            P1
EMP                            P2
EMP                            SYS_P403
EMP                            SYS_P404

zx@ORA12C>select index_name,partition_name,status from user_ind_partitions where index_name='IDX_EMP_NO';

INDEX_NAME                     PARTITION_NAME                 STATUS
------------------------------ ------------------------------ --------
IDX_EMP_NO                     P1                             USABLE
IDX_EMP_NO                     P2                             USABLE
IDX_EMP_NO                     SYS_P403                       USABLE
IDX_EMP_NO                     SYS_P404                       USABLE

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

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

When using the UPDATE INDEXES clause, 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 the UPDATE INDEXES clause 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 the INDEXES clause or the INDEXES clause 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


相关内容

热门资讯

美媒:中美元首会晤或讨论AI护... 据凤凰卫视援引美国媒体报道,美方希望借中美领导人会晤的机会,开启有关人工智能安全与管控的对话,并推动...
卫生间水管布置图尺寸高度 卫生间的水管布置可能比较复杂一些,因为卫生间的管道比较多。比如有热水器的通水管道,有座便器的冲水管道...
电脑尺寸在哪里看 电脑尺寸在哪里看1、可以选择手工测量法来测量电脑尺寸,这时测量一下屏幕对角线,因为显示面积都会小于显...
电脑机箱尺寸标准 常规的机箱一般是立体式,或桌面式,再就是横式。电脑机箱尺寸,目前市面上根据不同的规格有不同的方案。 ...
斯塔默发声:不会辞职 【环球时报驻英国特约记者 纪双城 环球时报特约记者 甄翔 于文】执政党工党在英国地方选举中遭遇“历史...
抽油烟机管道有异味怎么办 抽油烟机是厨房中不可或缺的设备,它能有效地排出油烟,保证室内空气的清新。但是,长时间使用后,抽油烟机...
抽油烟机管道有油垢怎么办 抽油烟机管道是我们家庭生活中必不可少的设备,但长时间的使用会导致管道内部积累大量的油垢,如不及时清理...
沈伯洋与台北市长格局有落差,叶... 海峡导报综合报道 民进党将正式拍板征召民代沈伯洋出战台北市长,并发布采访通知,预计在13日下午举行台...
核查:上世纪已有多款汉坦疫苗上... 明查员 冯梦速览- 人类感染汉坦病毒已有数十年历史。朝鲜战争期间,联合国军就曾有过大规模感染。汉坦病...
如何从“一证在手”走向“一技傍... 教员在给学员讲解理论知识。 学员在进行无人机试飞训...