背景介绍:
环境:linux 5.5 + oracle 10.2.0.4
某普通表t,由于前期设计不当没有分区,如今几年来的数据量已达9亿+, 空间占用大约350g,在线重定义为分区表不现实,故采取申请时间窗口停此表应用,改造为分区表。
若t表数据量适当,可选用在线重定义操作时,,可参考:
1.创建分区表
-- create table 创建分区表t_part,分区从14年6月开始。
create table t_part
(
……
)
partition by range(time_stamp)(
partition p20140601 values less than (to_date(' 2014-06-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss', 'nls_calendar=gregorian'))
tablespace dbs_d_jingyu
);
使用分区添加工具添加到15年6月份。
2.设置新建分区表为nologging, 重命名原表t为t_old
alter table t_part nologging;
rename t to t_old;
3.并行直接路径插入
alter session enable parallel dml;
insert /*+ append parallel(p,10) */ into t_part p select /*+ parallel(n,10) */ * from t_old n;
commit;
查看下insert的执行计划,确定都能用到并行度。
explain plan for insert /*+ append parallel(p,10) */ into t_part p select /*+ parallel(n,10) */ * from t_old n;
执行插入脚本
sql> @/home/oracle/insert
~~~~~~~~~~~~~~~~~~~~~~~~~
已创建908792694行。
已用时间: 02: 09: 37.94
提交完成。
已用时间: 00: 08: 13.76
4.为分区表建立索引
4.1 重命名历史表的索引名
alter index pk_t rename to pk_t_bak;
alter table t_old rename constraint pk_t to pk_t_bak;
alter index idx_t_2 rename to idx_t_2_bak;
alter index idx_t_3 rename to idx_t_3_bak;
4.2 给新分区表t_part创建主键及索引
create unique index pk_t on t_part(oid, time_stamp, serial_no, city_id) local tablespace dbs_i_jingyu nologging parallel 32;
索引已创建。
已用时间: 04: 39: 53.10
alter table t_part add constraint pk_t primary key (oid, time_stamp, serial_no, city_id);
表已更改。
已用时间: 00: 00: 00.43
create index idx_t_2 on t_part (time_stamp, serial_no, city_id) local tablespace dbs_i_jingyu nologging parallel 32;
索引已创建。
已用时间: 02: 27: 49.92
create index idx_t_3 on t_part (time_stamp, city_id) local tablespace dbs_i_jingyu nologging parallel 32;
索引已创建。
已用时间: 02: 19: 06.74
4.3 修改索引和表为logging,noparallel
alter index pk_t logging noparallel;
alter index idx_t_2 logging noparallel;
alter index idx_t_3 logging noparallel;
alter table t_part logging;
4.4 遇到的问题
建立唯一性索引时报错:
sql> create unique index pk_t on t_part(oid, time_stamp, serial_no, city_id) local tablespace dbs_i_jingyu nologging parallel 32;
create unique index pk_t on t_part(oid, time_stamp, serial_no, city_id) local tablespace dbs_i_jingyu nologging parallel 32
ora-12801: 并行查询服务器 p000 中发出错误信号
ora-01652: 无法通过 128 (在表空间 tmp 中) 扩展 temp 段
解决方式:增加临时表空间大小
alter tablespace tmp add tempfile '/usr3/oradata2/sysdata/tmp02.dbf' size 30g;
alter tablespace tmp add tempfile '/usr3/oradata2/sysdata/tmp03.dbf' size 30g;
alter tablespace tmp add tempfile '/usr3/oradata2/sysdata/tmp04.dbf' size 30g;
5.rename表,恢复t表的相关应用
rename t_part为t,恢复t表应用。
rename t_part to t;
根据实际情况决定是否彻底drop掉t_old,释放空间。
drop table t_old purge;
