1

快速复制一张大表讨论

 2 years ago
source link: https://blogread.cn/it/article/100?f=hot1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

快速复制一张大表讨论

浏览:2864次  出处信息

    有这样的一类需求,快速复制一张表,而这张表的数据量又非常的大。比方说本来是分区表,要把它重新变成非分区表,怎么能够快速的完成这样的操作呢?我下面给出2种大致的方法:

    第一,就是利用CTAS方式来创建一张新表,当然要想加快速度,在数据库不是force logging的前提下,可以使用nologging方式来创建表
SQL> create table auction_auctions_bak
  2  tablespace tbs_taobao
  3  nologging
  4  as
  5  select * from auction_auctions;

    Table created.

    SQL> select count(*) from auction_auctions;

     COUNT(*)
----------
   4179779

    SQL> select count(*) from auction_auctions_bak;

     COUNT(*)
----------
   4179779

    在建立完成表以后,要注意将表的属性重新变成logging:
SQL> select logging from tabs where table_name=\'AUCTION_AUCTIONS_BAK\';

    LOG
---
NO

    SQL> alter table auction_auctions_bak logging;

    Table altered.

    当然也可以这样:CREATE TABLE ... AS SELECT .. WHERE 1=2;然后使用 INSERT /*+ APPEND */ INTO .. SELECT ...。

    最后将表名更改过来,建立一下新的索引,然后就可以了。

    SQL> drop table auction_auctions;

    Table dropped.

    SQL> rename auction_auctions_bak to auction_auctions;

    Table renamed.

    第二,在原理上,它其实和第一种方式差不多,就是阻止数据库记录日志来加快速度,大概过程如下:
1、exp出原表(建议compress=n)
2、drop 原表
3、以nologging的方式,新建与原表同名的空表
4、imp原表至空表中,注意要把ignore设置为y
5、更改新表的属性为logging

    我这次分别都对两种方式做了测试,结果第二种方式远远快于第一种方式,大概是其10倍的速度。注意以上两种方式都是在数据库没有force logging的前提下完成的。如果数据库被force logging了,那么怎样才能加快复制速度,这个大家可以说说。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK