數據交換表面看上去是兩個段裏面的數據進行交換,其實就是數據字典的交換,但是表結構必須一樣
下面一個例子交換分區和索引
創建分區
create table Part_index_example
(
x number ,
y number,
data varchar2(20)
)
partition by range(x)
(
partition Part_index_part1 values less than(5),
partition Part_index_part2 values less than(10)
);
創建索引
create index local_prefixed on Part_index_example(x,y) local;
插入數據
insert into Part_index_example(x,y,Data) select mod(level ,10),level,level from dual connect by level<1000;
創建表
create table less5
(
x number ,
y number,
data varchar2(20)
);
索引
create index less_ind on less5(x,y);
插入數據
insert into less5 select mod(level,5),level,level from dual connect by level<1000;
SQL> select count(*) from less5;
COUNT(*)
----------
999
SQL> select count(*) from Part_index_example partition(Part_index_part1);
COUNT(*)
----------
504
SQL> begin
2 dbms_stats.gather_table_stats(ownname => user ,tabname => upper('less5'),cascade => true);
3 dbms_stats.gather_table_stats(ownname => user ,tabname => upper('Part_index_example'),cascade => true);
4 end;
5 /
PL/SQL 過程已成功完成。
alter table Part_index_example exchange partition Part_index_part1 with table less5 including indexes without validation;
這個操作非常快。
SQL> select count(*) from Part_index_example partition(Part_index_part1);
COUNT(*)
----------
999
SQL> select count(*) from less5;
COUNT(*)
----------
504
通過交換,其實可以用一張空表與一個分區進行交換,從而使得區間數據清除,並且又能將數據歸檔
可以將分區邊滿表,空表變分區,從而可以將滿表導出數據庫。
注意:
這個操作會導致全局索引分區失效
全局分區索引每一個分區索引可能指向任何表分區,而我們取走了一個分區,導致索引失敗。其中有些條目指向我們已經刪除數據,而新增的數據不再該索引條目中。使得在查詢時無法使用全局索引分區
在對分區進行維護時,基本會導致全局索引分區失效,
alter table Part_index_example drop partition Part_index_part2 ;
alter table Part_index_example add partition Part_index_part3 values less than(20);
alter table Part_index_example exchange partition Part_index_part1 with table less5 including indexes without validation ;
都會導致全局索引分區失效,但是如果在這些ddl語句後面加 update global indexes;
alter table Part_index_example drop partition Part_index_part2 update global indexes ;
alter table Part_index_example add partition Part_index_part3 values less than(20) update global indexes;
alter table Part_index_example exchange partition Part_index_part1 with table less5 including indexes without validation update global indexes;