移動一個表到另一個schema的方法

可以有以下幾種常用的辦法:
1、expdp/impdp

創(chuàng)新互聯(lián)建站專業(yè)IDC數(shù)據(jù)服務(wù)器托管提供商,專業(yè)提供成都服務(wù)器托管,服務(wù)器租用,服務(wù)器托管德陽,服務(wù)器托管德陽,成都多線服務(wù)器托管等服務(wù)器托管服務(wù)。

2、ctas + parallel + nologin

     第二種方法要注意主鍵在新表是沒有創(chuàng)建的

NOT NULL constraints that were implicitly created by Oracle Database on columns of the selected table (for example, for primary keys) are not carried over to the new table.

http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_7002.htm

3、exchange partition

以下針對第三種方法進行測試:
創(chuàng)建big_table腳本來自O(shè)racle Database 9i10g11g編程藝術(shù)深入數(shù)據(jù)庫體系結(jié)構(gòu)(第2版),轉(zhuǎn)換方式:普通表A.A->分區(qū)表A.A_TEMP->普通表B.B
1.創(chuàng)建測試表:

info@PROD> create table big_table
  2  as
  3  select rownum id, a.OWNER, a.OBJECT_NAME, a.SUBOBJECT_NAME, a.O  3  
  3  select rownum id, a.OWNER, a.OBJECT_NAME, a.SUBOBJECT_NAME, a.OBJECT_ID, a.DATA_OBJECT_ID
  4    from all_objects a
  5   where 1=0
  6  /
Table created.
Elapsed: 00:00:00.09
info@PROD> alter table big_table nologging;
Table altered.
Elapsed: 00:00:00.01
info@PROD> declare
  2      l_cnt number;
  3      l_rows number := &1;
  4  begin
  5      insert /*+ append */
  6      into big_table
  7      select rownum, a.OWNER, a.OBJECT_NAME, a.SUBOBJECT_NAME, a.OBJECT_ID, a.DATA_OBJECT_ID
  8        from all_objects a
  9   where rownum <= &1;
 10  
 11      l_cnt := sql%rowcount;
 12  
 13      commit;
 14  
 15      while (l_cnt < l_rows)
 16      loop
 17          insert /*+ APPEND */ into big_table
 18          select rownum+l_cnt, 
 19                 OWNER, OBJECT_NAME, SUBOBJECT_NAME, OBJECT_ID, DATA_OBJECT_ID
 20            from big_table
 21           where rownum <= l_rows-l_cnt;
 22          l_cnt := l_cnt + sql%rowcount;
 23          commit;
 24      end loop;
 25  end;
 26  /
Enter value for 1: 8000000
old   3:     l_rows number := &1;
new   3:     l_rows number := 8000000;
Enter value for 1: 8000000
old   9:  where rownum <= &1;
new   9:  where rownum <= 8000000;
PL/SQL procedure successfully completed.
Elapsed: 00:00:07.73
info@PROD> select count(*) from big_table;
  COUNT(*)
----------
   8000000
Elapsed: 00:00:01.86
info@PROD> alter table big_table add constraint big_table_pk primary key(id);
Table altered.
Elapsed: 00:00:38.63
info@PROD> info@PROD> exec dbms_stats.gather_table_stats( user, 'BIG_TABLE', estimate_percent=> 1);
PL/SQL procedure successfully completed.

創(chuàng)建中間表:

info@PROD> CREATE TABLE big_table_temp
   2    PARTITION BY RANGE (id)
   3   (PARTITION id_1 VALUES LESS THAN (MAXVALUE))
   4    AS
   5     SELECT *
   6       FROM big_table
   7      WHERE ROWNUM <= 0;
info@PROD> alter table big_table_temp add constraint pk_big_table_temp_id primary key(id);

為pinfo用戶授權(quán):

info@PROD>  grant ALL on big_table to "PINFO";
info@PROD>  grant ALL on big_table_temp to "PINFO";

登錄pinfo,創(chuàng)建info同名表:

info@PROD> conn pinfo/admin
Connected.
pinfo@PROD> CREATE TABLE pinfo.big_table
  2  AS
  3     SELECT *
  4       FROM info.big_table
  5      WHERE ROWNUM <= 0;

登錄info,將big_table交換至big_table_temp:

pinfo@PROD> conn info/admin
info@PROD> ALTER TABLE big_table_temp EXCHANGE PARTITION id_1 WITH TABLE big_table EXCLUDING INDEXES WITHOUT VALIDATION;
Table altered.
Elapsed: 00:00:00.02
#此處使用了excludeing選項,否則會報 ORA-14098: index mismatch for tables in ALTER TABLE EXCHANGE PARTITION,可以在交換完成以后手動創(chuàng)建索引
info@PROD> select count(*) from big_table;
  COUNT(*)
----------
   0
info@PROD> select count(*) from  info.big_table_temp;
  COUNT(*)
----------
  8000000

登錄pinfo,將big_table_temp交換至big_table:

pinfo@PROD> ALTER TABLE info.big_table_temp EXCHANGE PARTITION id_1 WITH TABLE pinfo.big_table EXCLUDING INDEXES WITHOUT VALIDATION;
Table altered.
Elapsed: 00:00:00.01
pinfo@PROD> select count(*) from big_table;
  COUNT(*)
----------
   8000000
Elapsed: 00:00:02.91
pinfo@PROD> select count(*) from  info.big_table_temp;
  COUNT(*)
----------
         0

完成交換幾乎是毫秒級的。

也可以反向交換回去:

pinfo@PROD> ALTER TABLE info.big_table_temp EXCHANGE PARTITION id_1 WITH TABLE pinfo.big_table EXCLUDING INDEXES WITHOUT VALIDATION;
info@PROD> conn info/admin
info@PROD> ALTER TABLE big_table_temp EXCHANGE PARTITION id_1 WITH TABLE big_table exCLUDING INDEXES WITHOUT VALIDATION;

以下內(nèi)容來自asktom,轉(zhuǎn)換方式:普通表A.A->分區(qū)表B.B

參考:https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:752030266230

To quickly move big tables between schemas  use EXCHANGE PARTITION feature of Oracle 8i.
for example:
SQL> connect as user "A"
SQL> create table large_table
     (
       a number,
       b char,
       c date
     )
-- just for this example only. :)
SQL> grant ALL on large_table to "B";
SQL> connect as user "B"
SQL> create table large_table 
     (
       a number,  
       b char,    
       c date
     )
     partition by range (a)
     (
       partition dummy values less than (maxvalue)
     )
Then you can use the following command to quickly move 
"A.large_table" to "B.large_table"
SQL> connect as user "B";
SQL> alter table large_table exchange partition dummy
     with table A.large_table;
And return it back to schema A:
SQL> alter table large_table exchange partition dummy
     with table A.large_table;
-- of course, it is the same SQL command

本文標題:移動一個表到另一個schema的方法
當前鏈接:http://www.muchs.cn/article46/iheghg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、商城網(wǎng)站、動態(tài)網(wǎng)站網(wǎng)站營銷、手機網(wǎng)站建設(shè)網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作