简体   繁体   English

创建具有百万条记录的备份表

[英]create a backup table with million records

I want to move old records from TABLEA to TABLEA_AUDIT. 我想将旧记录从TABLEA移到TABLEA_AUDIT。 TABLEA have around 1.5 million records and it have two nested tables. TABLEA大约有150万条记录,并且有两个嵌套表。

No of Records : 1557951 Size : 1024 MB 记录数:1557951大小:1024 MB

I tried it by, 我尝试过

1.using Create table as select * from 1.使用Create table作为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';

Result : 2hours of waiting and didnt get any result 结果:2小时的等待,没有得到任何结果

2.Tried CREATE TABLE AS with no logging by follwoing below blog. 2.通过在博客下面关注来尝试创建CREATE TABLE AS,而无需记录日志。 http://www.dba-oracle.com/t_fast_copy_data_oracle_table.htm http://www.dba-oracle.com/t_fast_copy_data_oracle_table.htm

Result : 2hours of waiting and didnt get any result 结果:2小时的等待,没有得到任何结果

3.Developed a new procedure to copy the records and created a new DBMS_JOB 3.开发了一个新过程来复制记录并创建了一个新的DBMS_JOB

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

Result : Job is running for more than 2hours and no result. 结果:作业运行超过2小时,没有结果。

4.Created the table and developed a procedure to insert bulk records 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;
/

Thanks. 谢谢。

Bro try this 兄弟试试这个

--MSSQL
    select * into TABLEA_AUDIT from TABLEA

--FOR oracle (large amount of data )
Create table TABLEA_AUDIT
As
Select * 
from TABLEA 
where 1=2;

Insert into TABLEA_AUDIT
Select * from TABLEA;

I'd suggest Data Pump . 我建议数据泵

First, using the Export data pump export the table. 首先,使用导出数据泵导出表。 In the next step, import it (using Import data pump - who'd guess?), using the REMAP_TABLE parameter. 在下一步中,使用REMAP_TABLE参数导入(使用“导入数据泵-谁猜?”)。

Here's a simple example based on Scott's EMP table (I've removed superfluous lines to make it easier to read): 这是一个基于Scott的EMP表的简单示例(为了方便阅读,我删除了多余的行):

Export: 出口:

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>

Import: 进口:

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>

Note that the primary key constraint creation failed, as the constraint with that name already exists in the original table. 请注意,主键约束创建失败,因为具有该名称的约束已存在于原始表中。

The result: 结果:

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>

Once again, the two commands you should use: 再次,您应该使用两个命令:

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

Try it, see how it works. 试试看,看看它是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM