oracle批量联机,Oracle 12.2 联机重定义多个分区并将其移动到不同的表空间中

下面的例子将演示如何联机重定义多个分区并将基于范围分区的表salestable的两个分区移动到新表空间中。原始表jy.salestable的创建如下:

SQL> create table jy.salestable

2 (s_productid number,

3 s_saledate date,

4 s_custid number,

5 s_totalprice number)

6 tablespace users

7 partition by range(s_saledate)

8 (partition sal10q1 values less than (to_date('01-apr-2010', 'dd-mon-yyyy')),

9 partition sal10q2 values less than (to_date('01-jul-2010', 'dd-mon-yyyy')),

10 partition sal10q3 values less than (to_date('01-oct-2010', 'dd-mon-yyyy')),

11 partition sal10q4 values less than (to_date('01-jan-2011', 'dd-mon-yyyy')));

Table created.

这个例子会将分区sal10q1与sal10q2移动到example表空间中。sal10q3与sal10q4分区不会被移动。为了移动分区表空间example必须存在。这里已经先创建好了表空间example。对原始表jy.salestable创建一个本地分区索引,操作如下:

SQL> create index jy.sales_index on jy.salestable (s_saledate, s_productid, s_custid) local;

Index created.

注意,在12.2中也可以执行alter table ... move partition ... online语句来将分区移动到其它表空间中。

联机重定义操作如下:

1.用要执行联机重定义操作的用户登录数据库

SQL> conn jy/jy@jypdb

Connected.

2.验证原始表jy.salestable是否可以执行联机重定义

SQL> begin

2 dbms_redefinition.can_redef_table(

3 uname => 'jy',

4 tname => 'salestable',

5 options_flag => DBMS_REDEFINITION.CONS_USE_ROWID,

6 part_name => 'sal10q1, sal10q2');

7 end;

8 /

PL/SQL procedure successfully completed.

3.在新表空间example中创建中间表。因为这是对分区执行联机重定义,因此中间表不能是分区表。

SQL> create table jy.int_salestb1

2 (s_productid number,

3 s_saledate date,

4 s_custid number,

5 s_totalprice number)

6 tablespace example;

Table created.

SQL> create table jy.int_salestb2

2 (s_productid number,

3 s_saledate date,

4 s_custid number,

5 s_totalprice number)

6 tablespace example;

Table created.

4.使用rowid方法来执行重定义操作

SQL> begin

2 dbms_redefinition.start_redef_table(

3 uname => 'jy',

4 orig_table => 'salestable',

5 int_table => 'int_salestb1, int_salestb2',

6 col_mapping => NULL,

7 options_flag => DBMS_REDEFINITION.CONS_USE_ROWID,

8 part_name => 'sal10q1, sal10q2',

9 continue_after_errors => TRUE);

10 end;

11 /

PL/SQL procedure successfully completed.

注意,part_name参数用来指定所有要重定义的分区,int_table参数用来指定每个分区所对应的中间表,continue_after_errors参数被设置为true,因此重定义操作即使当某个特定分区遇到错误也会继续执行。

5.在中间表上创建任何本地索引

SQL> create index jy.int_sales1_index on jy.int_salestb1

2 (s_saledate, s_productid, s_custid)

3 tablespace example;

Index created.

SQL> create index jy.int_sales2_index on jy.int_salestb2

2 (s_saledate, s_productid, s_custid)

3 tablespace example;

Index created.

6.可选操作同步中间表

SQL> begin

2 dbms_redefinition.sync_interim_table(

3 uname => 'jy',

4 orig_table => 'salestable',

5 int_table => 'int_salestb1, int_salestb2',

6 part_name => 'sal10q1, sal10q2',

7 continue_after_errors => TRUE);

8 end;

9 /

PL/SQL procedure successfully completed.

7.完成重定义操作

SQL> begin

2 dbms_redefinition.finish_redef_table(

3 uname => 'jy',

4 orig_table => 'salestable',

5 int_table => 'int_salestb1, int_salestb2',

6 part_name => 'sal10q1, sal10q2',

7 continue_after_errors => TRUE);

8 end;

9 /

PL/SQL procedure successfully completed.

8.可选操作,查询dba_redefinition_status视图来确保对每个分区都重定义操作成功

SQL> select base_table_owner, base_table_name, operation, status from dba_redefinition_status;

no rows selected

如果有任何分区重定义失败,视图dba_redefinition_errors会显示出错误原因,修正故障重新执行联机重定义操作。

下面的查询显示了表jy.salestable有两个分区已经移动到了新的表空间example中了

SQL> select partition_name, tablespace_name from dba_tab_partitions where table_name = 'SALESTABLE' and table_owner='JY';

PARTITION_NAME TABLESPACE_NAME

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

SAL10Q1 EXAMPLE

SAL10Q2 EXAMPLE

SAL10Q3 USERS

SAL10Q4 USERS

到此联机重定义操作完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值