Online-Redefinition在线重定义对象

##第四. Online Redefinition在线重定义对象

Online Redefinition在线重定义对象是Oracle中很酷的一种特性,它可以帮助我们在7*24在线的系统中从容地做出数据对象的在线定义修改,是Oracle数据库保证其高可用性的重要技术。

此处输入图片的描述

在线重定义Online Redefinition特性在许多场景中都是十分有用的,例如:

  • 修改表的Storage存储参数
  • 在同一Schema下将表移动到不同的表空间
  • 转换非分区表为分区表
  • 添加或删除分区
  • 重新创建表以减少碎片,降低高水位
  • 将堆组织的表改变为索引组织表
  • 添加或删除列

使用Online Redefinition在线重定义需要用到DBMS_REDEFINITION程序包,EXECUTE_CATALOG_ROLE角色默认被赋予该PL/SQL Package的执行权限。除了执行该程序包的权限外,用户还需要拥有以下权限:

1
2
3
4
5
6
7
CREATE ANY TABLE
ALTER ANY TABLE
DROP ANY TABLE
LOCK ANY TABLE
SELECT ANY TABLE
CREATE ANY TRIGGER
CREATE ANY INDEX

1,构造普通表t_objects

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
conn test1/test1;
create table t_objects as select * from dba_objects;
SQL> select count(1) from t_objects;
COUNT(1)
----------
468738
--t_objects建立主键和索引
alter table t_objects add constraint pk_objects primary key (created, object_id);
create index i_objects on t_objects(object_id, STATUS);
--表有主键,确认表可以重定义:
SQL> EXEC DBMS_REDEFINITION.CAN_REDEF_TABLE('test1','t_objects');
PL/SQL procedure successfully completed.
--若表无主键 可以采用rowid重定义:
SQL> EXEC DBMS_REDEFINITION.CAN_REDEF_TABLE('test1','t_objects',2);
PL/SQL procedure successfully completed.

2,创建重定义需要的临时表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
-- Create table
create table T_OBJECTS_TEMP
(
OWNER VARCHAR2(30),
OBJECT_NAME VARCHAR2(128),
SUBOBJECT_NAME VARCHAR2(30),
OBJECT_ID NUMBER not null,
DATA_OBJECT_ID NUMBER,
OBJECT_TYPE VARCHAR2(19),
CREATED DATE not null,
LAST_DDL_TIME DATE,
TIMESTAMP VARCHAR2(19),
STATUS VARCHAR2(7),
TEMPORARY VARCHAR2(1),
GENERATED VARCHAR2(1),
SECONDARY VARCHAR2(1),
NAMESPACE NUMBER,
EDITION_NAME VARCHAR2(30)
)partition by range(created)(
partition P20130601 values less than (TO_DATE(' 2013-06-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
tablespace DBS_D_GRNOPHQ,
partition P20140607 values less than (TO_DATE(' 2014-06-07 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
tablespace DBS_D_GRNOPHQ,
partition P20140731 values less than (TO_DATE(' 2014-07-31 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
tablespace DBS_D_GRNOPHQ
);

3,开始重定义

1
exec DBMS_REDEFINITION.START_REDEF_TABLE('test1','t_objects','t_objects_temp');

4,开始拷贝表的属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
alter table t_objects_temp add constraint pk_objects_temp primary key (created, object_id) using index local;
create index i_objects_temp on t_objects_temp(object_id, STATUS) local;
DECLARE
error_count pls_integer := 0;
BEGIN
DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS(
uname => 'test1',
orig_table => 't_objects',
int_table => 't_objects_temp',
ignore_errors => TRUE,
num_errors => error_count);
DBMS_OUTPUT.PUT_LINE('errors := ' || TO_CHAR(error_count));
END;
/

5,同步数据

1
exec DBMS_REDEFINITION.SYNC_INTERIM_TABLE (uname => 'test1',orig_table => 't_objects',int_table => 't_objects_temp');

6,收集中间表的统计信息(选做)

1
EXEC DBMS_STATS.gather_table_stats('test1', 't_objects_temp', cascade => TRUE);

7,完成重定义

1
EXEC DBMS_REDEFINITION.FINISH_REDEF_TABLE('TEST1','T_OBJECTS','T_OBJECTS_TEMP');

8,删除临时表

1
drop table t_objects_temp purge;

9,修改索引,约束名称和原表一致

1
2
3
alter index I_OBJECTS_TEMP rename to I_OBJECTS;
alter index PK_OBJECTS_TEMP rename to PK_OBJECTS;
alter table t_objects rename constraint pk_objects_temp to pk_objects;

10,ABORT_REDEF_TABLE使用, 在FINISH_REDEF_TABLE之前,可以使用abort_redef_table停止重定义

1
exec DBMS_REDEFINITION.ABORT_REDEF_TABLE ('test1','t_objects','t_objects_temp');