创建一个包含百万条记录的备份表

时间:2018-04-30 06:13:21

标签: sql oracle oracle12c

我想将旧记录从TABLEA移到TABLEA_AUDIT。 TABLEA有大约150万条记录,它有两个嵌套表。

No of Records:1557951 大小:1024 MB

我试过了,

1.使用创建表作为select * from

CREATE TABLE
TABLEA_AUDIT
COLUMN TABLEA_STAGE NOT SUBSTITUTABLE AT ALL LEVELS
NESTED TABLE TABLEA_STAGES STORE AS AUDIT_TABLEA_STAGES,
NESTED TABLE TABLEA_MODELS STORE AS AUDIT_TABLEA_MODELS
AS
SELECT *
FROM
TABLEA
WHERE COULMN1 IS NOT NULL
AND TRUNC(UPDATED_DT) < '01-MAR-18';

结果:2小时的等待并没有得到任何结果

2.Tried CREATE TABLE AS没有记录下面的博客。 http://www.dba-oracle.com/t_fast_copy_data_oracle_table.htm

结果:2小时的等待并没有得到任何结果

3.开发了一个新程序来复制记录并创建了一个新的DBMS_JOB

EXECUTE IMMEDIATE 'CREATE TABLE TABLEA_AUDIT AS SELECT * FROM TABLEA';

结果:作业运行超过2小时且没有结果。

4.处理该表并开发了一个插入批量记录的程序

set serveroutput on size unlimited
set timing on
declare 
type audit_type is table of TABLEA%rowtype;
v_type audit_type;
CURSOR temp_cur is
select  *
FROM TABLEA a
WHERE COLUMN1 IS NOT NULL
AND TRUNC(UPDATED_DT) < '01-MAR-18';
BEGIN

    OPEN  temp_cur;
    / collect data in the collection /
    FETCH temp_cur BULK COLLECT INTO v_type;
    / close the pointer /
    CLOSE temp_cur;

    FORALL i in v_type.first .. v_type.last
    INSERT INTO TABLEA_AUDIT VALUES v_type(i);

    FORALL i in v_type.first .. v_type.last
    DELETE  FROM TABLEA WHERE PRIMARY_KEY_COL = v_type(i).PRIMARY_KEY_COL;               

    COMMIT;
END;
/

感谢。

2 个答案:

答案 0 :(得分:0)

兄弟试试这个

 PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 12314 notifs    20   0 5723112 2.101g    104 S 129.4 17.9 785:17.34 java                              
12582 notifs    20   0 5727148 2.082g      4 S 117.6 17.7 782:13.72 java                              
15509 notifs    20   0 3767700 1.009g    500 S  11.8  8.6  78:27.08 httpd                             
15511 notifs    20   0 3527984 984116    524 S  11.8  8.0  77:25.43 httpd                             
15510 notifs    20   0 3640620 964676    552 S  11.8  7.8  75:20.43 httpd                             
15504 notifs    20   0   77460    764    532 S   0.0  0.0   0:02.40 httpd  

答案 1 :(得分:0)

我建议Data Pump

首先,使用导出数据泵导出表。在下一步中,使用REMAP_TABLE参数导入它(使用导入数据泵 - 谁猜?)。

这是一个基于Scott的EMP表的简单示例(我删除了多余的行以便于阅读):

导出:

c:\Temp>expdp scott/tiger@xe tables=emp directory=ext_dir dumpfile=emp.dmp

Export: Release 11.2.0.2.0 - Production on Pon Tra 30 14:45:18 2018

Starting "SCOTT"."SYS_EXPORT_TABLE_01":  scott/********@xe tables=emp directory=ext_dir dumpfile=emp.dmp
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "SCOTT"."EMP"                               8.484 KB      12 rows
Master table "SCOTT"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SCOTT.SYS_EXPORT_TABLE_01 is:
  C:\TEMP\EMP.DMP
Job "SCOTT"."SYS_EXPORT_TABLE_01" successfully completed at 14:45:20


c:\Temp>

导入:

c:\Temp>impdp scott/tiger@xe remap_table=emp:emp_bkp directory=ext_dir dumpfile=emp.dmp exclude=constraint

Import: Release 11.2.0.2.0 - Production on Pon Tra 30 14:50:09 2018

Master table "SCOTT"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "SCOTT"."SYS_IMPORT_FULL_01":  scott/********@xe remap_table=emp:emp_bkp directory=ext_dir dumpfile=emp.dmp exc
lude=constraint
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT"."EMP_BKP"                           8.484 KB      12 rows
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
ORA-31684: Object type INDEX:"SCOTT"."PK_EMP" already exists
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
ORA-39111: Dependent object type INDEX_STATISTICS skipped, base object type INDEX:"SCOTT"."PK_EMP" already exists
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SCOTT"."SYS_IMPORT_FULL_01" completed with 2 error(s) at 14:50:11


c:\Temp>

请注意,主键约束创建失败,因为原始表中已存在具有该名称的约束。

结果:

c:\Temp>sqlplus scott/tiger@xe

SQL> select * From emp_Bkp;

     EMPNO ENAME      JOB              MGR HIREDATE          SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ---------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17.12.1980        800                    20
      7499 ALLEN      SALESMAN        7698 20.02.1981       1600        300         30
      7521 WARD       SALESMAN        7698 22.02.1981       1250        500         30
      7566 JONES      MANAGER         7839 02.04.1981       2975                    20
      7654 MARTIN     SALESMAN        7698 28.09.1981       1250       1400         30
      7698 BLAKE      MANAGER         7839 01.05.1981       2850                    30
      7782 CLARK      MANAGER         7839 09.06.1981       2450                    10
      7839 KING       PRESIDENT            17.11.1981       5000                    10
      7844 TURNER     SALESMAN        7698 08.09.1981       1500          0         30
      7900 JAMES      CLERK           7698 03.12.1981        950                    30
      7902 FORD       ANALYST         7566 03.12.1981       3000                    20
      7934 MILLER     CLERK           7782 23.01.1982       1300                    10

12 rows selected.

SQL>

再一次,你应该使用两个命令:

expdp scott/tiger@xe tables=emp directory=ext_dir dumpfile=emp.dmp

impdp scott/tiger@xe remap_table=emp:emp_bkp directory=ext_dir dumpfile=emp.dmp exclude=constraint

试一试,看看它是如何运作的。