oracle 11.2 普通錶轉換爲分區表的常見方法

1、模擬大表
--新建測試表
create tablespace test
datafile 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\test01.DBF'
size 2000m
autoextend on
next 100m maxsize unlimited
extent management local autoallocate
segment   space management auto;
create table t1
(
tid number not null primary key,
sname varchar2(10)
)
tablespace test;
--循環導入數據
declare
        maxrecords constant int:=200000;
        i int :=1;
    begin
        for i in 1..maxrecords loop
          insert into t1 values(i,'ocpyang');
        end loop;
    dbms_output.put_line(' 成功錄入數據! ');
    commit;
    end;
/
-------方法1:在線重定義
建立中間表(分區表,你想得到的格式)
CREATE TABLE t11
(
tid number not null primary key,
sname varchar2(10)
)
PARTITION BY RANGE (tid)
(
PARTITION p1 VALUES LESS THAN (50000) tablespace test,
PARTITION p2 VALUES LESS THAN (100000) tablespace test,
PARTITION p3 VALUES LESS THAN (150000) tablespace test,
PARTITION p4 VALUES LESS THAN (300000) tablespace test
)
;
驗證原表是否可以在線遷移
 exec dbms_redefinition.can_redef_table('SCOTT','T1');
新開啓一個會話驗證當前會話沒有REDO產生
select t3.sid,t2.NAME,t1.VALUE from v$sesstat t1 ,
v$statname t2 ,
(select SID from v$mystat where rownum<=1) t3
where t1.SID=t3.sid and t1.STATISTIC#=t2.STATISTIC#
and   t2.name like '%redo%' and t1.VALUE<>0 ;
   
   
       SID NAME                                                    VALUE
---------- -------------------------------------------------- ----------
       200 redo entries                                           407915
       200 redo size                                           106889084
       200 redo size for direct writes                           2729848
       200 redo buffer allocation retries                              3
       200 redo log space requests                                     5
       200 redo log space wait time                                   23
       200 redo ordering marks                                      1031
       200 redo subscn max counts                                     17
       200 redo synch time                                             1
       200 redo synch time (usec)                                   6903
       200 redo synch writes                                           5
開始聯機重定義:
SQL> exec dbms_redefinition.start_redef_table('SCOTT','T1','T11');
   
同步數據:
   
SQL> exec dbms_redefinition.sync_interim_table('SCOTT','T1','T11');
   
   
結束聯機重定義:
SQL> exec dbms_redefinition.finish_redef_table('SCOTT','T1','T11');
   
select t3.sid,t2.NAME,t1.VALUE from v$sesstat t1 ,
v$statname t2 ,
(select SID from v$mystat where rownum<=1) t3
where t1.SID=t3.sid and t1.STATISTIC#=t2.STATISTIC#
and   t2.name like '%redo%' and t1.VALUE<>0 ;
        
       SID NAME                                                    VALUE
---------- -------------------------------------------------- ----------
       200 redo entries                                           419232
       200 redo size                                           130386624
       200 redo size for direct writes                          11235576
       200 redo buffer allocation retries                              3
       200 redo log space requests                                     5
       200 redo log space wait time                                   23
       200 redo ordering marks                                      1993
       200 redo subscn max counts                                     41
       200 redo synch time                                            25
       200 redo synch time (usec)                                 243493
       200 redo synch writes                                          33
查看數據已經移動完成,索引已經遷移.
如果分區表並沒有主鍵,我們建立主鍵
alter table t11 add constraint pk_t11 primary key (tid);
---方法2
使用NOLOGGING方法
CREATE TABLE t12
(
tid number not null primary key,
sname varchar2(10)
)
PARTITION BY RANGE (tid)
(
PARTITION p1 VALUES LESS THAN (50000) tablespace test nologging ,
PARTITION p2 VALUES LESS THAN (100000) tablespace test nologging,
PARTITION p3 VALUES LESS THAN (150000) tablespace test nologging,
PARTITION p4 VALUES LESS THAN (300000) tablespace test nologging
)
;
/****或對分區表修改
alter table t12 modify partition p1 nologging;
***/
select t3.sid,t2.NAME,t1.VALUE from v$sesstat t1 ,
v$statname t2 ,
(select SID from v$mystat where rownum<=1) t3
where t1.SID=t3.sid and t1.STATISTIC#=t2.STATISTIC#
and   t2.name like '%redo%' and t1.VALUE<>0 ;
       SID NAME                                                    VALUE
---------- -------------------------------------------------- ----------
       200 redo entries                                           419448
       200 redo size                                           130464436
       200 redo size for direct writes                          11252032
       200 redo buffer allocation retries                              3
       200 redo log space requests                                     5
       200 redo log space wait time                                   23
       200 redo ordering marks                                      1998
       200 redo subscn max counts                                     41
       200 redo synch time                                            25
       200 redo synch time (usec)                                 244362
       200 redo synch writes                                          37
然後開啓新會話進行INSERT APPEND
insert /* +append */ into t12 select * from t1;
select t3.sid,t2.NAME,t1.VALUE from v$sesstat t1 ,
v$statname t2 ,
(select SID from v$mystat where rownum<=1) t3
where t1.SID=t3.sid and t1.STATISTIC#=t2.STATISTIC#
and   t2.name like '%redo%' and t1.VALUE<>0 ;
       SID NAME                                                    VALUE
---------- -------------------------------------------------- ----------
       200 redo entries                                           434385
       200 redo size                                           155328372
       200 redo size for direct writes                          11260260
       200 redo buffer allocation retries                              5
       200 redo log space requests                                     7
       200 redo log space wait time                                   29
       200 redo ordering marks                                      2775
       200 redo subscn max counts                                     50
       200 redo synch time                                            28
       200 redo synch time (usec)                                 273343
       200 redo synch writes                                          39
alter table t12 rename to t1;  --修改分區表爲T1的表名即可
---方法3
直接使用CREATE TABLE NOLOGGING方式
select t3.sid,t2.NAME,t1.VALUE from v$sesstat t1 ,
v$statname t2 ,
(select SID from v$mystat where rownum<=1) t3
where t1.SID=t3.sid and t1.STATISTIC#=t2.STATISTIC#
and   t2.name like '%redo%' and t1.VALUE<>0 ;
       SID NAME                                                    VALUE
---------- -------------------------------------------------- ----------
       200 redo entries                                           434385
       200 redo size                                           155328372
       200 redo size for direct writes                          11260260
       200 redo buffer allocation retries                              5
       200 redo log space requests                                     7
       200 redo log space wait time                                   29
       200 redo ordering marks                                      2775
       200 redo subscn max counts                                     50
       200 redo synch time                                            28
       200 redo synch time (usec)                                 273343
       200 redo synch writes                                          39
CREATE TABLE t13
PARTITION BY RANGE (tid)
(
PARTITION p1 VALUES LESS THAN (50000) tablespace test nologging ,
PARTITION p2 VALUES LESS THAN (100000) tablespace test nologging,
PARTITION p3 VALUES LESS THAN (150000) tablespace test nologging,
PARTITION p4 VALUES LESS THAN (300000) tablespace test nologging
)
as
select * from t1;
   
select t3.sid,t2.NAME,t1.VALUE from v$sesstat t1 ,
v$statname t2 ,
(select SID from v$mystat where rownum<=1) t3
where t1.SID=t3.sid and t1.STATISTIC#=t2.STATISTIC#
and   t2.name like '%redo%' and t1.VALUE<>0 ;
       SID NAME                                                    VALUE
---------- -------------------------------------------------- ----------
       200 redo entries                                           434699
       200 redo size                                           155392656
       200 redo size for direct writes                          11262224
       200 redo buffer allocation retries                              5
       200 redo log space requests                                     7
       200 redo log space wait time                                   29
       200 redo ordering marks                                      2782
       200 redo subscn max counts                                     50
       200 redo synch time                                            28
       200 redo synch time (usec)                                 273797
       200 redo synch writes                                          40
alter table t13 rename to t1;  ----修改分區表爲T1的表名即可
   
-------方法4:交換分區
SQL> select constraint_name from user_constraints where table_name='T1';
CONSTRAINT_NAME
------------------------------
SYS_C0025179
SYS_C0025180
ALTER TABLE t1 DROP PRIMARY KEY;
CREATE TABLE t14
(
tid number not null,
sname varchar2(10)
)
PARTITION BY RANGE (tid)
(
PARTITION p1 VALUES LESS THAN (300000) tablespace test nologging ,
PARTITION p2 VALUES LESS THAN (400000) tablespace test nologging,
PARTITION p3 VALUES LESS THAN (500000) tablespace test nologging,
PARTITION p4 VALUES LESS THAN (600000) tablespace test nologging
)
;
select count(*) from t1;
  COUNT(*)
----------
    200000
select count(*) from t14 partition (p1);
  COUNT(*)
----------
         0
alter table t14 exchange partition p1 with table t1;
alter table t14
add constraint pk_t14 primary key(tid);
 alter table t14 rename to t1;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章