Oracle 普通表与分区表转换
Oracle 9i提供了dbms_redefinition包来实现数据库的表的在线重定义功能。在实际的应用上,我们可以利用这个包来进行:
(1)堆表与分区之间进行转换。(2)重建表以减少HWM。10g能shrink,9i如果用move tablespace and rebuild index在move的时候会锁表,如果想实现在线降低HWM,只能用这个了。
(3)在线更改表结构,如更改列的前后顺序,将column_a,column_b改成column_b,column_a
1.创建测试表
create table rebuild_table as select * from dba_objects;
insert into rebuild_table select * from rebuild_table;
insert into rebuild_table select * from rebuild_table;
insert into rebuild_table select * from rebuild_table;
commit;
delete from rebuild_table;
insert into rebuild_table select * from dba_objects;
insert into rebuild_table select * from rebuild_table;
insert into rebuild_table select * from rebuild_table;
commit;
update rebuild_table set object_id=rownum;
alter table rebuild_table add CONSTRAINT P_YY PRIMARY KEY (OBJECT_ID);
select * from rebuild_table;
2.创建分区表
create table REBUILD_PA_TABLE
(
STATIS_DATE NUMBER(8),
OWNER VARCHAR2(30),
OBJECT_NAME VARCHAR2(128),
SUBOBJECT_NAME VARCHAR2(30),
OBJECT_ID NUMBER not null,
DATA_OBJECT_ID NUMBER,
OBJECT_TYPE VARCHAR2(18),
CREATED DATE,
LAST_DDL_TIME DATE,
TIMESTAMP VARCHAR2(19),
STATUS VARCHAR2(7),
TEMPORARY VARCHAR2(1),
GENERATED VARCHAR2(1),
SECONDARY VARCHAR2(1)
)
partition by list (STATIS_DATE)
(
partition p20111031 values (20111031)
);
alter table REBUILD_PA_TABLE add partition p20111101 values(20111101);
;
alter table REBUILD_PA_TABLE
add constraint Pa_YYY primary key (OBJECT_ID)
;
-- 打开重定义
begin
dbms_redefinition.CAN_REDEF_TABLE('boc_rdm','REBUILD_TABLE');
end;
-- 开始重定义
begin
dbms_redefinition.START_REDEF_TABLE('boc_rdm','REBUILD_TABLE','REBUILD_PA_TABLE');
end;
-- 进行数据同步
begin
dbms_redefinition.sync_interim_table('BOC_RDM','REBUILD_TABLE','REBUILD_PA_TABLE');
end;
-- 重命名表,把非分区表转化为分区表
begin
dbms_redefinition.finish_redef_table('BOC_RDM','REBUILD_TABLE','REBUILD_PA_TABLE');
end;
select * from REBUILD_TABLE partition(p20111031)
(1)堆表与分区之间进行转换。(2)重建表以减少HWM。10g能shrink,9i如果用move tablespace and rebuild index在move的时候会锁表,如果想实现在线降低HWM,只能用这个了。
(3)在线更改表结构,如更改列的前后顺序,将column_a,column_b改成column_b,column_a
1.创建测试表
create table rebuild_table as select * from dba_objects;
insert into rebuild_table select * from rebuild_table;
insert into rebuild_table select * from rebuild_table;
insert into rebuild_table select * from rebuild_table;
commit;
delete from rebuild_table;
insert into rebuild_table select * from dba_objects;
insert into rebuild_table select * from rebuild_table;
insert into rebuild_table select * from rebuild_table;
commit;
update rebuild_table set object_id=rownum;
alter table rebuild_table add CONSTRAINT P_YY PRIMARY KEY (OBJECT_ID);
select * from rebuild_table;
2.创建分区表
create table REBUILD_PA_TABLE
(
STATIS_DATE NUMBER(8),
OWNER VARCHAR2(30),
OBJECT_NAME VARCHAR2(128),
SUBOBJECT_NAME VARCHAR2(30),
OBJECT_ID NUMBER not null,
DATA_OBJECT_ID NUMBER,
OBJECT_TYPE VARCHAR2(18),
CREATED DATE,
LAST_DDL_TIME DATE,
TIMESTAMP VARCHAR2(19),
STATUS VARCHAR2(7),
TEMPORARY VARCHAR2(1),
GENERATED VARCHAR2(1),
SECONDARY VARCHAR2(1)
)
partition by list (STATIS_DATE)
(
partition p20111031 values (20111031)
);
alter table REBUILD_PA_TABLE add partition p20111101 values(20111101);
;
alter table REBUILD_PA_TABLE
add constraint Pa_YYY primary key (OBJECT_ID)
;
-- 打开重定义
begin
dbms_redefinition.CAN_REDEF_TABLE('boc_rdm','REBUILD_TABLE');
end;
-- 开始重定义
begin
dbms_redefinition.START_REDEF_TABLE('boc_rdm','REBUILD_TABLE','REBUILD_PA_TABLE');
end;
-- 进行数据同步
begin
dbms_redefinition.sync_interim_table('BOC_RDM','REBUILD_TABLE','REBUILD_PA_TABLE');
end;
-- 重命名表,把非分区表转化为分区表
begin
dbms_redefinition.finish_redef_table('BOC_RDM','REBUILD_TABLE','REBUILD_PA_TABLE');
end;
select * from REBUILD_TABLE partition(p20111031)
相关推荐
zhangchaoming 2020-05-17
Carlos 2020-02-21
LuoXinLoves 2020-02-02
LuoXinLoves 2020-01-19
gaobudong 2019-03-27
HackAge 2013-11-01
EricRay 2011-08-09
xuanzi 2019-06-26
xuanzi 2018-02-18
社会主义好 2017-10-01
itisaamazing 2017-05-26
zipp 2017-02-27
dataminer 2016-11-15
我的DBA之路 2015-07-27
kiddwyl 2015-06-16
海内存知己 2015-05-06
joyleeLyhua 2015-04-03
蓦然回首择城终老 2015-04-03
qingfengfeixin 2015-03-02