oracle分區交換技術

来源:http://www.cnblogs.com/xuchenliang/archive/2017/05/12/6844024.html
-Advertisement-
Play Games

交換分區的操作步驟如下:1. 創建分區表t1,假設有2個分區,P1,P2.2. 創建基表t11存放P1規則的數據。3. 創建基表t12 存放P2規則的數據。4. 用基表t11和分區表T1的P1分區交換。 把表t11的數據放到到P1分區5. 用基表t12 和分區表T1p2 分區交換。 把表t12的數據 ...


交換分區的操作步驟如下:

1. 創建分區表t1,假設有2個分區,P1,P2.
2. 創建基表t11存放P1規則的數據。
3. 創建基表t12 存放P2規則的數據。
4. 用基表t11和分區表T1的P1分區交換。 把表t11的數據放到到P1分區
5. 用基表t12 和分區表T1p2 分區交換。 把表t12的數據存放到P2分區。

----1.未分區表和分區表中一個分區交換
create table t1
(
sid int not null primary key,
sname  varchar2(50)
)
PARTITION BY range(sid)
( PARTITION p1 VALUES LESS THAN (5000) tablespace test,
  PARTITION p2 VALUES LESS THAN (10000) tablespace test,
  PARTITION p3  VALUES LESS THAN (maxvalue) tablespace test
) tablespace test;

SQL> select count(*) from t1;

 

  COUNT(*)
----------
         0

create table t11
(
sid int not null primary key,
sname  varchar2(50)
) tablespace test;


create table t12
(
sid int not null primary key,
sname  varchar2(50)
) tablespace test;


create table t13
(
sid int not null primary key,
sname  varchar2(50)
) tablespace test;


--迴圈導入數據
declare
        maxrecords constant int:=4999;
        i int :=1;
    begin
        for i in 1..maxrecords loop
          insert into t11 values(i,'ocpyang');
        end loop;
    dbms_output.put_line(' 成功錄入數據! ');
    commit;
    end; 
/


declare
        maxrecords constant int:=9999;
        i int :=5000;
    begin
        for i in 5000..maxrecords loop
          insert into t12 values(i,'ocpyang');
        end loop;
    dbms_output.put_line(' 成功錄入數據! ');
    commit;
    end; 
/


declare
        maxrecords constant int:=70000;
        i int :=10000;
    begin
        for i in 10000..maxrecords loop
          insert into t13 values(i,'ocpyang');
        end loop;
    dbms_output.put_line(' 成功錄入數據! ');
    commit;
    end; 
/
commit;


SQL> select count(*) from t11;


  COUNT(*)
----------
      4999


SQL> select count(*) from t12;


  COUNT(*)
----------
      5000


SQL> select count(*) from t13;


  COUNT(*)
----------
     60001


--交換分區

alter table t1 exchange partition p1 with table t11;

SQL> select count(*) from t11;   --基表t11數據為0


  COUNT(*)
----------
         0


SQL> select count(*) from t1 partition (p1);  --分區表的P1分區數據位基表t11的數據 


  COUNT(*)
----------
      4999


alter table t1 exchange partition p2 with table t12;


select count(*) from t12; 


select count(*) from t1 partition (p2); 


alter table t1 exchange partition p3 with table t13;


select count(*) from t13; 


select count(*) from t1 partition (p3); 


-----2.分區表和分區表交換

/*
EXCHANGE PARTITION WITH TABLE的方式不支持分區表與分區表的交換,只能通過中間表中轉.
*/


--2.1源表


create tablespace jinrilog
datafile 'E:\APP\ADMINISTRATOR\ORADATA\ORCL\jinrilog01.DBF'
size 200M  autoextend on next 20M maxsize unlimited
extent management local autoallocate
segment space management auto
;


create tablespace jinrilogindex
datafile 'E:\APP\ADMINISTRATOR\ORADATA\ORCL\jinrilogindex01.DBF'
size 200M  autoextend on next 20M maxsize unlimited
extent management local autoallocate
segment space management auto
;


create table t1
(
sid int not null ,
sname  varchar2(50) not null,
createtime date default sysdate   not null
)
PARTITION BY range(createtime)

PARTITION p1 VALUES LESS THAN ('2013-06-01 00:00:00') tablespace jinrilog,
PARTITION p2 VALUES LESS THAN ('2013-07-01 00:00:00') tablespace jinrilog,
PARTITION p3 VALUES LESS THAN ('2013-08-01 00:00:00') tablespace jinrilog,
PARTITION p4  VALUES LESS THAN (maxvalue) tablespace jinrilog
) tablespace jinrilog;


create unique index un_t1_01 on t1(sid,createtime)
tablespace jinrilogindex
local;

alter table t1 add constraint pk_t1 primary key(sid,createtime);

create index index_t1_01
on t1 (sname  asc)
tablespace jinrilogindex
local
(
partition index_sname_01 tablespace jinrilogindex,
partition index_sname_02 tablespace jinrilogindex,
partition index_sname_03 tablespace jinrilogindex,
partition index_sname_04 tablespace jinrilogindex
);

--迴圈導入數據
declare
        maxrecords constant int:=1000;
        i int :=1;
    begin
        for i in 1..maxrecords loop
          insert into t1 values(i,'ocpyang','2013-06-11 00:00:00');
        end loop;
    dbms_output.put_line(' 成功錄入數據! ');
    commit;
    end; 
/

declare
        maxrecords constant int:=2000;
        i int :=1;
    begin
        for i in 1..maxrecords loop
          insert into t1 values(i,'ocpyang','2013-07-11 00:00:00');
        end loop;
    dbms_output.put_line(' 成功錄入數據! ');
    commit;
    end; 
/


declare
        maxrecords constant int:=3000;
        i int :=1;
    begin
        for i in 1..maxrecords loop
          insert into t1 values(i,'ocpyang','2013-08-11 00:00:00');
        end loop;
    dbms_output.put_line(' 成功錄入數據! ');
    commit;
    end; 
/

SQL> select count(*) from t1;


  COUNT(*)
----------
     6000


SQL> select count(*) from  t1 partition(p1) ;


  COUNT(*)
----------
         0

SQL>
SQL> select count(*) from  t1 partition(p2) ;


  COUNT(*)
----------
      1000


SQL> select count(*) from  t1 partition(p3) ;


  COUNT(*)
----------
      2000

SQL> select count(*) from  t1 partition(p4) ;


  COUNT(*)
----------
      3000

---查看表數據分區情況

select utp.table_name,utp.partition_name,utp.tablespace_name from user_tab_partitions utp 
where utp.table_name='T1';

--查看分區索引分佈情況

col index_name for a20
col partition_name for a20
col tablespace_name for a20
col status for a10
select index_name,null partition_name,tablespace_name,status
from user_indexes
where table_name='T1'
and partitioned='NO'
union 
select index_name,partition_name,tablespace_name,status from user_ind_partitions
where index_name in
(
select index_name from user_indexes
where table_name='T1'
)
order by 1,2,3
;
--2.2 和中間表交換數據

create table t11
(
sid int not null ,
sname  varchar2(50)  not null,
createtime date default sysdate   not null
)tablespace jason;

select count(*) from t11;


alter table t1 exchange partition p2 with table t11;

 

--查看無效的索引並重建


col index_name for a20
col partition_name for a20
col tablespace_name for a20
col status for a10
select index_name,null partition_name,status
from user_indexes
where table_name='T1'
and partitioned='NO'
union 
select index_name,partition_name,status from user_ind_partitions
where index_name in
(
select index_name from user_indexes
where table_name='T1'
)
order by 1,2,3
;


INDEX_NAME                     PARTITION_NAME                 STATUS
------------------------------ ------------------------------ --------
INDEX_T1_01                    INDEX_SNAME_01                 USABLE
INDEX_T1_01                    INDEX_SNAME_02                 UNUSABLE
INDEX_T1_01                    INDEX_SNAME_03                 USABLE
INDEX_T1_01                    INDEX_SNAME_04                 USABLE
UN_T1_01                       P1                             USABLE
UN_T1_01                       P2                             UNUSABLE
UN_T1_01                       P3                             USABLE
UN_T1_01                       P4                             USABLE


alter index INDEX_T1_01  rebuild partition INDEX_SNAME_02;


alter index UN_T1_01  rebuild partition P2;

col index_name for a20
col partition_name for a20
col tablespace_name for a20
col status for a10
select index_name,null partition_name,status
from user_indexes
where table_name='T1'
and partitioned='NO'
union 
select index_name,partition_name,status from user_ind_partitions
where index_name in
(
select index_name from user_indexes
where table_name='T1'
)
order by 1,2,3
;


INDEX_NAME                     PARTITION_NAME                 STATUS
------------------------------ ------------------------------ --------
INDEX_T1_01                    INDEX_SNAME_01                 USABLE
INDEX_T1_01                    INDEX_SNAME_02                 USABLE
INDEX_T1_01                    INDEX_SNAME_03                 USABLE
INDEX_T1_01                    INDEX_SNAME_04                 USABLE
UN_T1_01                       P1                             USABLE
UN_T1_01                       P2                             USABLE
UN_T1_01                       P3                             USABLE
UN_T1_01                       P4                             USABLE

select count(*) from t1 partition (p2);

  COUNT(*)
----------
         0

select count(*) from t11;


 COUNT(*)
---------
     1000

--確定數據是否已經切換到新的表空間


SELECT TABLESPACE_NAME 
FROM USER_TAB_PARTITIONS 
WHERE TABLE_NAME='T1' AND PARTITION_NAME='P2';


TABLESPACE_NAME
------------------------------
JASON

---2.3中間表和歸檔表再次交換數據


create tablespace archive01
datafile 'E:\APP\ADMINISTRATOR\ORADATA\ORCL\archive01.DBF'
size 200M  autoextend on next 20M maxsize unlimited
extent management local autoallocate
segment space management auto
;

create tablespace archive02
datafile 'E:\APP\ADMINISTRATOR\ORADATA\ORCL\archive02.DBF'
size 200M  autoextend on next 20M maxsize unlimited
extent management local autoallocate
segment space management auto
;

create table t2
(
sid int not null ,
sname  varchar2(50)  not null,
createtime date default sysdate   not null
)
PARTITION BY range(createtime)

PARTITION p1 VALUES LESS THAN ('2013-06-01 00:00:00') tablespace archive01,
PARTITION p2 VALUES LESS THAN ('2013-07-01 00:00:00') tablespace archive01,
PARTITION p3 VALUES LESS THAN ('2013-08-01 00:00:00') tablespace archive01,
PARTITION p4  VALUES LESS THAN (maxvalue) tablespace archive01
) tablespace archive01;


create unique index un_t2_01 on t2(sid,createtime)
tablespace archive02
local;

alter table t2 add constraint pk_t2 primary key(sid,createtime);

select up.table_name,up.partition_name,up.tablespace_name from user_tab_partitions up 
where up.table_name='T2';

--查看分區索引分佈情況

col index_name for a20
col partition_name for a20
col tablespace_name for a20
col status for a10
select index_name,null partition_name,tablespace_name,status
from user_indexes
where table_name='T2'
and partitioned='NO'
union 
select index_name,partition_name,tablespace_name,status from user_ind_partitions
where index_name in
(
select index_name from user_indexes
where table_name='T2'
)
order by 1,2,3
;


INDEX_NAME           PARTITION_NAME       TABLESPACE_NAME      STATUS
-------------------- -------------------- -------------------- ----------
UN_T2_01             P1                   ARCHIVE02            USABLE
UN_T2_01             P2                   ARCHIVE02            USABLE
UN_T2_01             P3                   ARCHIVE02            USABLE
UN_T2_01             P4                   ARCHIVE02            USABLE

select count(*) from t2;


 COUNT(*)
---------
        0

--交換數據


alter table t2 exchange partition p2 with table t11 ;

select count(*) from t2;

select count(*) from t11;

 

以上內容轉自http://blog.csdn.NET/yangzhawen/article/details/8768943


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...