首页 > 代码库 > Oracle清理大表,降水位

Oracle清理大表,降水位

背景:一张表的清理机制存在问题,导致该表的数据一直在增加,该表水位已很高,需要对该表的数据进行清理并降水位。

1.1 迁移前准备

步骤一、新建表 p_transaction_bak。

oracle@linux-nxv6:~/orcale > sqlplus test/test

SQL> create table p_transaction_bak
as
select * from p_transaction where 1 = 0;
SQL> alter table p_transaction_bak modify OPERATIONDATE DEFAULT sysdate;
SQL> alter table p_transaction_bak modify INVALID DEFAULT 0;

步骤二、有需要的有效数据插入新表中。

SQL> alter session enable parallel dml;

insert /*+ append parallel(p,8) */ into p_transaction_bak p select /*+ parallel(n,8) */ * from p_transaction n
where to_char(n.operationdate, ‘yyyy-mm-dd‘) between
to_char(to_date(‘2016-06-27‘, ‘yyyy-mm-dd‘), ‘yyyy-mm-dd‘) and
to_char(to_date(‘2016-07-04‘, ‘yyyy-mm-dd‘), ‘yyyy-mm-dd‘);

commit;

步骤三、为新表 p_transaction

table创建主键和索引
SQL> create unique index PK_PO_TRANSACTION_NEW on p_transaction_bak(STREAMINGID) tablespace portaloneindx parallel 8 online;
SQL> alter table p_transaction_bak add constraint PK_PO_TRANSACTION_NEW primary key (STREAMINGID);
SQL> alter index PK_PO_TRANSACTION_NEW noparallel;
SQL> create index ix_transaction_operationdate on p_transaction_bak(operationdate) tablespace portaloneindx parallel 8 online;
SQL> alter index ix_transaction_operationdate noparallel;

----End

1.2 执行数据迁移

步骤一、停应用

步骤二、以SYSDBA用户登录数据库。

oracle@linux-nxv6:~/orcale > sqlplus / as sysdba

步骤三、检查数据表空间和索引表空间使用率。

SQL>select total.tablespace_name,
round(total.MB, 2) as Total_MB,
round(total.MB - free.MB, 2) as Used_MB,
round((1 - free.MB / total.MB) * 100, 2) || ‘%‘ as Used_Pct
from (select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_free_space
group by tablespace_name) free,
(select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_data_files
group by tablespace_name) total
where free.tablespace_name = total.tablespace_name;

步骤四、并行将数据插入新表。

SQL> alter session enable parallel dml;

insert /*+ append parallel(p,8) */ into p_transaction_bak p select /*+ parallel(n,8) */ * from p_transaction n
where to_char(n.operationdate, ‘yyyy-mm-dd‘) =
to_char(to_date(‘2016-07-05‘, ‘yyyy-mm-dd‘), ‘yyyy-mm-dd‘);

commit;

步骤五、备份旧表。

SQL> rename p_transaction to p_transaction_old;

步骤六、重命名新表。

SQL> rename p_transaction_bak to p_transaction;

----End

1.3 回滚以上操作 oracle@linux-nxv6:~/ orcale > sqlplus test/test

SQL>rename p_transaction to p_transaction_new;
SQL>rename p_transaction_old to p_transaction;
SQL>truncate table p_transaction_new;
SQL>drop table p_transaction_new;

----End

 

Oracle清理大表,降水位