oracle大批量数据删除解决方案

这段时间系统由于大量历史数据造成应用响应缓慢,需要对大量历史数据做数据迁移,原本计划将原表备份后Create table as 一个新表,量表换名重建索引等解决,但由于服务层的特殊结构,这些表无法做分区,而且这种方式直接会将服务整挂,所以想采用delete掉数据重新压缩表空间和索引方式解决。
预计删除数据量为12个大表,每个表总量为3000W-3亿数据,需要清除2/3的数据,采取步骤如下,以供日后整理

  1. 备份数据

备份数据量需要表空间空余500G,现workflow空余表空间只有90G,需要扩展至少500G
Step.1备份故障和投诉主工单
表名 备注
T367 故障主工单T表(WF:BMCC_EOMS_ITDealFault)
H367 故障主工单H表(WF:BMCC_EOMS_ITDealFault)
T633 投诉主工单T表(WF:BJ_EOMS_Complaint)
H633 投诉主工单H表(WF:BJ_EOMS_Complaint)
远程dblink备份语句示例
Create table T367 as select * from [email protected]_product;
Step.2备份工单辅助表
表名 备注
T117 工单状态表(WF:App_Base_Infor)
T118 工单通知表(WF:App_Base_Notice)
T136 工单环节表(WF:App_DealProcess)
T135 工单流程线表(WF:App_DealLink)
T134 工单开始结束状态表(WF:App_DealAssistantProcess)
T141 工单流程判断表(WF:App_DealVerdict)
T363 工单流转线表(WF:App_DealGoLine)
T114 工单字段修改记录表(WF:App_Base_FieldModifyLog)

  1. 查询数据量

1.故障T表

select count(1) from t367 WHERE 1=1
 AND c3<1380513600

2.投诉T表

select count(1) from t633 WHERE 1=1
 AND c3<1380513600

2.其它辅助表

select count(1) from t136  WHERE 1=1
AND (C700020002 = ‘WF:BMCC_EOMS_ITDealFault‘ OR C700020002=‘WF:BJ_EOMS_COMPLAINT‘)
AND c3<1380513600
select  count(1) from T135 WHERE 1=1 AND (C700020502 = ‘WF:BMCC_EOMS_ITDealFault‘ OR C700020502=‘WF:BJ_EOMS_COMPLAINT‘)  AND c3<1380513600 ;
select  count(1) from T134 WHERE 1=1 AND (C700020802 = ‘WF:BMCC_EOMS_ITDealFault‘ OR C700020802=‘WF:BJ_EOMS_COMPLAINT‘)  AND c3<1380513600 ;
select  count(1) from T141 WHERE 1=1 AND (C700020602 = ‘WF:BMCC_EOMS_ITDealFault‘ OR C700020602=‘WF:BJ_EOMS_COMPLAINT‘)  AND c3<1380513600 ;
select  count(1) from T114 WHERE 1=1 AND (C700021002 = ‘WF:BMCC_EOMS_ITDealFault‘ OR C700021002=‘WF:BJ_EOMS_COMPLAINT‘)  AND c3<1380513600 ;
select count(1) from T118 WHERE 1=1 AND (C700050002 = ‘WF:BMCC_EOMS_ITDealFault‘ OR C700050002=‘WF:BJ_EOMS_COMPLAINT‘)  AND c3>1380513600;
  1. 新建临时表

新建临时表(存要删数据的rowid)
语句:

CREATE TABLE ROWID_T633 AS SELECT ROWID ID,C1 FROM T633 WHERE C3 < 1380513600
CREATE TABLE ROWID_T367 AS SELECT ROWID ID,C1 FROM T633 WHERE C3 < 1380513600
CREATE TABLE ROWID_T117 AS SELECT ROWID AS ID FROM T117 WHERE 1=1
 AND (C700020002 = ‘WF:BMCC_EOMS_ITDealFault‘ OR C700020002=‘WF:BJ_EOMS_COMPLAINT‘)
 AND c3<1380513600
CREATE TABLE ROWID_T135 AS SELECT ROWID AS ID FROM T135 WHERE 1=1 AND (C700020502 = ‘WF:BMCC_EOMS_ITDealFault‘ OR                   C700020502=‘WF:BJ_EOMS_COMPLAINT‘)  AND c3<1380513600 ;
CREATE TABLE ROWID_T134 AS SELECT ROWID AS ID FROM T134 WHERE 1=1 AND (C700020802 = ‘WF:BMCC_EOMS_ITDealFault‘ OR     C700020802=‘WF:BJ_EOMS_COMPLAINT‘)  AND c3<1380513600 ;
CREATE TABLE ROWID_T141 AS SELECT ROWID AS ID FROM T141 WHERE 1=1 AND (C700020602 = ‘WF:BMCC_EOMS_ITDealFault‘ OR     C700020602=‘WF:BJ_EOMS_COMPLAINT‘)  AND c3<1380513600 ;
CREATE TABLE ROWID_T114 AS SELECT ROWID AS ID FROM T114 WHERE 1=1 AND (C700021002 = ‘WF:BMCC_EOMS_ITDealFault‘ OR C700021002=‘WF:BJ_EOMS_COMPLAINT‘)  AND c3<1380513600 ;
CREATE TABLE ROWID_T136 AS SELECT ROWID AS ID FROM T136 WHERE 1=1
 AND (C700020002 = ‘WF:BMCC_EOMS_ITDealFault‘ OR C700020002=‘WF:BJ_EOMS_COMPLAINT‘)
 AND c3<1380513600;
CREATE TABLE ROWID_H633 AS SELECT ROWID ID,Entryid FROM H633 WHERE T0 < 1380513600
CREATE TABLE ROWID_H367 AS SELECT ROWID ID,Entryid FROM H367 WHERE T0 < 1380513600
CREATE TABLE ROWID_T118 AS SELECT ROWID AS ID FROM T118 WHERE 1=1 AND (C700050002 = ‘WF:BMCC_EOMS_ITDealFault‘ OR     C700050002=‘WF:BJ_EOMS_COMPLAINT‘)  AND c3>1380513600;
  1. 写脚本删除

    T117

    DECLARE n NUMBER :=0;
    BEGIN
    FOR tr IN (SELECT ID FROM ROWID_T117) LOOP
    DELETE FROM T117 WHERE ROWID=tr.id;
    n:=n+1;
    IF MOD(n,5000)=0 THEN
    COMMIT;
    END IF;
    END LOOP;
    COMMIT;
    END;

T367

DECLARE n NUMBER :=0;
BEGIN
FOR tr IN (SELECT ID FROM ROWID_T367) LOOP
DELETE FROM T367 WHERE ROWID=tr.id;
n:=n+1;
IF MOD(n,5000)=0 THEN
COMMIT;
END IF;
END LOOP;
COMMIT;
END;

T633
DECLARE n NUMBER :=0;
BEGIN
FOR tr IN (SELECT ID FROM ROWID_T633) LOOP
DELETE FROM T633 WHERE ROWID=tr.id;

n:=n+1;
IF MOD(n,5000)=0 THEN
COMMIT;
END IF;

END LOOP;
COMMIT;
END;

T134
DECLARE n NUMBER :=0;
BEGIN
FOR tr IN (SELECT ID FROM ROWID_T134) LOOP
DELETE FROM T134 WHERE ROWID=tr.id;

n:=n+1;
IF MOD(n,5000)=0 THEN
COMMIT;
END IF;

END LOOP;
COMMIT;
END;

  1. 剩余表迁移数据工作步骤
    重建T表(T135、T136、T114、T141)的临时表

    CREATE TABLE ROWID_T135 AS SELECT ROWID AS ID FROM T135 WHERE 1=1 AND (C700020502 = ‘WF:BMCC_EOMS_ITDealFault‘ OR C700020502=‘WF:BJ_EOMS_COMPLAINT‘) AND c3<1380513600 ;
    CREATE TABLE ROWID_T141 AS SELECT ROWID AS ID FROM T141 WHERE 1=1 AND (C700020602 = ‘WF:BMCC_EOMS_ITDealFault‘ OR C700020602=‘WF:BJ_EOMS_COMPLAINT‘) AND c3<1380513600 ;
    CREATE TABLE ROWID_T114 AS SELECT ROWID AS ID FROM T114 WHERE 1=1 AND (C700021002 = ‘WF:BMCC_EOMS_ITDealFault‘ OR C700021002=‘WF:BJ_EOMS_COMPLAINT‘) AND c3<1380513600 ;
    CREATE TABLE ROWID_T136 AS SELECT ROWID AS ID FROM T136 WHERE 1=1 AND (C700020002 = ‘WF:BMCC_EOMS_ITDealFault‘ OR C700020002=‘WF:BJ_EOMS_COMPLAINT‘) AND c3<1380513600 ;

    执行脚本删除数据

    DECLARE n NUMBER :=0;
    BEGIN
    FOR tr IN (SELECT ID FROM ROWID_T135) LOOP
    DELETE FROM T135 WHERE ROWID=tr.id;
    n:=n+1;
    IF MOD(n,5000)=0 THEN
    COMMIT;
    END IF;
    END LOOP;
    COMMIT;
    END;
    DECLARE n NUMBER :=0;
    BEGIN
    FOR tr IN (SELECT ID FROM ROWID_T136) LOOP
    DELETE FROM T136 WHERE ROWID=tr.id;
    n:=n+1;
    IF MOD(n,5000)=0 THEN
    COMMIT;
    END IF;
    END LOOP;
    COMMIT;
    END;

DECLARE n NUMBER :=0;
BEGIN
FOR tr IN (SELECT ID FROM ROWID_T114) LOOP
DELETE FROM T114 WHERE ROWID=tr.id;

n:=n+1;
IF MOD(n,5000)=0 THEN
COMMIT;
END IF;

END LOOP;
COMMIT;
END;
DECLARE n NUMBER :=0;
BEGIN
FOR tr IN (SELECT ID FROM ROWID_T141) LOOP
DELETE FROM T141 WHERE ROWID=tr.id;

n:=n+1;
IF MOD(n,5000)=0 THEN
COMMIT;
END IF;

END LOOP;
COMMIT;
END;

  1. 释放表空间

    –打开行锁
    alter table T118 enable row MOVEMENT
    –整理数据和索引
    alter TABLE T118 shrink space COMPACT CASCADE
    –释放表空间
    alter TABLE T118 shrink SPACE CASCADE

时间: 2024-11-10 07:07:43

oracle大批量数据删除解决方案的相关文章

一次oracle大量数据删除经历

oracle有个数据表现在已经有2500万条数据了,软件用到这个表的数据时就变的特别慢,所以准备把一个月以前的数据全部清除. 我的步骤是(下边操作都是在plsql中运行的) 1.首先 将这个月的数据导出到一个临时表中(这些数据是自己希望保留的) create table tempTable as select id,name from table1 where sj>to_date('2013-7-31 23:59:59','yyyy-mm-dd hh24:mi:ss'); 2.然后将2500万

Oracle重复数据删除

在Oracle数据库中,删除重复数据,大都会使用如下方法: delete from tbl a where rowid<>(select max(b.rowid) from tbl b where a.col1=b.col1 and a.col2 = b.col2);

应用系统数据删除解决方案

需求: 由于公司一些旧系统在开发上线时没有考虑到数据需要备份删除的问题, 导致系统使用了十年之后数据库过大, 需要进行紧急的实时备份删除数据 1. 备份数据到备份库 2. 删除数据 3. 收缩数据库数据文件 因为需要尽量减少对系统的影响, 一般会按一下步骤操作 1. 如为系统不需要实时查询的表(如系统日志表), 则创建新表代替, 把旧表改成TableName_Old然后再执行后续步骤 2. 如表有自增列ID, 曾以自增列ID为查找值进行删除 3. 每次删除固定行数, 以免引起长时间锁表(无论表级

oracle 数据库数据迁移解决方案

大部分系统由于平台和版本的原因,做的是逻辑迁移,少部分做的是物理迁移,接下来把心得与大家分享一下 去年年底做了不少系统的数据迁移,大部分系统由于平台和版本的原因,做的是逻辑迁移,少部分做的是物理迁移,有一些心得体会,与大家分享. 首先说说迁移流程,在迁移之前,写好方案,特别是实施的方案步骤一定要写清楚,然后进行完整的测试.我们在迁移时,有的系统测试了四五次,通过测试来完善方案和流程. 针对物理迁移,也即通过RMAN备份来进行还原并应用归档的方式(这里不讨论通过dd方式进行的冷迁移),虽然注意的是

oracle 同样数据删除(仅仅留一条)

DELETE FROM reg_user t1 WHERE user_name='9527008' and rowid > ( SELECT min(rowid) FROM location t2 WHERE t1.user_name= t2.user_name);

使用sqoop1.4.4从oracle导入数据到hive中错误记录及解决方案

在使用命令导数据过程中,出现如下错误 sqoop import --hive-import --connect jdbc:oracle:thin:@192.168.29.16:1521/testdb --username NAME --passord PASS --verbose -m 1 --table T_USERINFO 错误1:File does not exist: hdfs://opt/sqoop-1.4.4/lib/commons-io-1.4.jar FileNotFoundEx

ODP方式,大批量数据写入ORACLE数据库

项目中在同步数据的时候,需要把获得的数据DataTable,写入oracle数据库 因为System.Data.OracleClient写入方式写入大批量数据特别慢,改用Oracle.DataAccess写入方式(上代码): ODP工具类: 需要引入命名空间: using Oracle.DataAccess;using Oracle.DataAccess.Client;using Oracle.DataAccess.Types; 1 ODP_Inserter 2 { 3 /// <summary

oracle恢复已经删除的数据

insert into tablerestore select * from tablerestore as of timestamp to_Date('2014-8-8 15:00:00','yyyy-mm-dd hh24:mi:ss')     参数解释:tablerestore 要恢复数据的表 后面的时间是恢复截止到某个点的数据 oracle恢复已经删除的数据,布布扣,bubuko.com

Oracle使用游标删除所有用户数据表中的所有记录脚本

应用场景:因为数据库中的数据涉及机密信息,希望一次性能删除掉所有数据,只保留数据表结构,供新项目开发程序用 测试结果:经查询已删除所有数据 存在问题:数据表如果存在外键的话下面脚本可能执行不成功,请自行删除或者过滤掉该表,见下图 操作办法:直接将下面的脚本内容复制到PQSQL中执行即可 --Oracle使用游标删除所有用户数据表中的所有记录脚本 declare mystring NVARCHAR2(1000):=''; --定义要输出的字符串变量 cursor mycursor is --定义游