如果对于不停机的数据归档,需要考虑数据在不停写入。
1. 修改待归档表索引名
alter index PS_PSACCESSLOG rename to PS_PSACCESSLOG2;
2. 创建新表
-- Create table
create table SYSADM.PSACCESSLOG_1
(
OPRID VARCHAR2(30) not null,
LOGIPADDRESS VARCHAR2(40) not null,
LOGINDTTM TIMESTAMP(6),
LOGOUTDTTM TIMESTAMP(6),
PT_SIGNON_TYPE VARCHAR2(3) not null
)
tablespace PTTBL
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 40K
next 104K
minextents 1
maxextents unlimited
);
3. 在新表上创建原名索引(如果停机归档,可在写入数据后创建索引)
-- Create/Recreate indexes
create index SYSADM.PS_PSACCESSLOG on SYSADM.PSACCESSLOG_1 (OPRID, LOGIPADDRESS, LOGINDTTM)
tablespace PSINDEX
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 40K
next 104K
minextents 1
maxextents unlimited
);
4. 将新表依原表 赋权
GRANT DELETE ON PSACCESSLOG_1 TO R_EP_DML ;
GRANT INSERT ON PSACCESSLOG_1 TO R_EP_DML ;
GRANT SELECT ON PSACCESSLOG_1 TO R_EP_DML ;
GRANT UPDATE ON PSACCESSLOG_1 TO R_EP_DML ;
5. 重命名表名(将待归档表重命名归档表名,新表重命名原表名)此处对于业务上会有一定短时的影响
RENAME PSACCESSLOG TO PSACCESSLOG_20170717;
RENAME PSACCESSLOG_1 TO PSACCESSLOG;
6. 循环写入新表归档时间点后的数据
begin
for cur in (select a.* from PSACCESSLOG_20170717 a where logindttm>=to_date('20170101','yyyymmdd')) loop
insert into PSACCESSLOG (oprid,logipaddress,Logindttm,logoutdttm,Pt_Signon_Type) values(cur.oprid,cur.logipaddress,cur.Logindttm,cur.logoutdttm,cur.Pt_Signon_Type);
commit;
end loop;
end;
/
7. 搜集新表统计信息
EXEC dbms_stats.gather_table_stats(ownname => 'SYSADM',tabname => 'PSACCESSLOG',estimate_percent => dbms_stats.AUTO_SAMPLE_SIZE,cascade => true,method_opt => 'FOR ALL COLUMNS SIZE AUTO');