1. 交换分区的操作步骤如下: 
  2.  
  3.  
  4. 1. 创建分区表t1,假设有2个分区,P1,P2. 
  5. 2. 创建基表t11存放P1规则的数据。 
  6. 3. 创建基表t12 存放P2规则的数据。 
  7. 4. 用基表t11和分区表T1的P1分区交换。 把表t11的数据放到到P1分区 
  8. 5. 用基表t12 和分区表T1p2 分区交换。 把表t12的数据存放到P2分区。 
  9.  
  10.  
  11. ----1.未分区表和分区表中一个分区交换 
  12.  
  13.  
  14. create table t1 
  15. sid int not null primary key
  16. sname  varchar2(50) 
  17. PARTITION BY range(sid) 
  18. ( PARTITION p1 VALUES LESS THAN (5000) tablespace test, 
  19.   PARTITION p2 VALUES LESS THAN (10000) tablespace test, 
  20.   PARTITION p3  VALUES LESS THAN (maxvalue) tablespace test 
  21. ) tablespace test; 
  22.  
  23.  
  24.  
  25.  
  26. SQL> select count(*) from t1; 
  27.  
  28.  
  29.   COUNT(*) 
  30. ---------- 
  31.          0 
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. create table t11 
  39. sid int not null primary key
  40. sname  varchar2(50) 
  41. ) tablespace test; 
  42.  
  43.  
  44.  
  45.  
  46. create table t12 
  47. sid int not null primary key
  48. sname  varchar2(50) 
  49. ) tablespace test; 
  50.  
  51.  
  52.  
  53.  
  54. create table t13 
  55. sid int not null primary key
  56. sname  varchar2(50) 
  57. ) tablespace test; 
  58.  
  59.  
  60. --循环导入数据 
  61. declare 
  62.         maxrecords constant int:=4999; 
  63.         i int :=1; 
  64.     begin 
  65.         for i in 1..maxrecords loop 
  66.           insert into t11 values(i,'ocpyang'); 
  67.         end loop; 
  68.     dbms_output.put_line(' 成功录入数据! '); 
  69.     commit
  70.     end;  
  71.  
  72.  
  73.  
  74.  
  75. declare 
  76.         maxrecords constant int:=9999; 
  77.         i int :=5000; 
  78.     begin 
  79.         for i in 5000..maxrecords loop 
  80.           insert into t12 values(i,'ocpyang'); 
  81.         end loop; 
  82.     dbms_output.put_line(' 成功录入数据! '); 
  83.     commit
  84.     end;  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. declare 
  92.         maxrecords constant int:=70000; 
  93.         i int :=10000; 
  94.     begin 
  95.         for i in 10000..maxrecords loop 
  96.           insert into t13 values(i,'ocpyang'); 
  97.         end loop; 
  98.     dbms_output.put_line(' 成功录入数据! '); 
  99.     commit
  100.     end;  
  101.  
  102.  
  103. commit
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. SQL> select count(*) from t11; 
  111.  
  112.  
  113.   COUNT(*) 
  114. ---------- 
  115.       4999 
  116.  
  117.  
  118. SQL> select count(*) from t12; 
  119.  
  120.  
  121.   COUNT(*) 
  122. ---------- 
  123.       5000 
  124.  
  125.  
  126. SQL> select count(*) from t13; 
  127.  
  128.  
  129.   COUNT(*) 
  130. ---------- 
  131.      60001 
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. --交换分区 
  139.  
  140.  
  141.  
  142.  
  143. alter table t1 exchange partition p1 with table t11; 
  144.  
  145.  
  146.  
  147.  
  148. SQL> select count(*) from t11;   --基表t11数据为0 
  149.  
  150.  
  151.   COUNT(*) 
  152. ---------- 
  153.          0 
  154.  
  155.  
  156. SQL> select count(*) from t1 partition (p1);  --分区表的P1分区数据位基表t11的数据  
  157.  
  158.  
  159.   COUNT(*) 
  160. ---------- 
  161.       4999 
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. alter table t1 exchange partition p2 with table t12; 
  171.  
  172.  
  173. select count(*) from t12;  
  174.  
  175.  
  176. select count(*) from t1 partition (p2);  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. alter table t1 exchange partition p3 with table t13; 
  186.  
  187.  
  188. select count(*) from t13;  
  189.  
  190.  
  191. select count(*) from t1 partition (p3);  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. -----2.分区表和分区表交换 
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. /* 
  210. EXCHANGE PARTITION WITH TABLE的方式不支持分区表与分区表的交换,只能通过中间表中转. 
  211. */ 
  212.  
  213.  
  214. --2.1源表 
  215.  
  216.  
  217.  
  218.  
  219. create tablespace jinrilog 
  220. datafile 'E:\APP\ADMINISTRATOR\ORADATA\ORCL\jinrilog01.DBF' 
  221. size 200M  autoextend on next 20M maxsize unlimited 
  222. extent management local autoallocate 
  223. segment space management auto 
  224.  
  225.  
  226.  
  227.  
  228. create tablespace jinrilogindex 
  229. datafile 'E:\APP\ADMINISTRATOR\ORADATA\ORCL\jinrilogindex01.DBF' 
  230. size 200M  autoextend on next 20M maxsize unlimited 
  231. extent management local autoallocate 
  232. segment space management auto 
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241. create table t1 
  242. sid int not null , 
  243. sname  varchar2(50) not null
  244. createtime date default sysdate   not null 
  245. PARTITION BY range(createtime) 
  246. (  
  247. PARTITION p1 VALUES LESS THAN ('2013-06-01 00:00:00') tablespace jinrilog, 
  248. PARTITION p2 VALUES LESS THAN ('2013-07-01 00:00:00') tablespace jinrilog, 
  249. PARTITION p3 VALUES LESS THAN ('2013-08-01 00:00:00') tablespace jinrilog, 
  250. PARTITION p4  VALUES LESS THAN (maxvalue) tablespace jinrilog 
  251. ) tablespace jinrilog; 
  252.  
  253.  
  254.  
  255.  
  256. create unique index un_t1_01 on t1(sid,createtime) 
  257. tablespace jinrilogindex 
  258. local
  259.  
  260.  
  261.  
  262.  
  263. alter table t1 add constraint pk_t1 primary key(sid,createtime); 
  264.  
  265.  
  266.  
  267.  
  268. create index index_t1_01 
  269. on t1 (sname  asc
  270. tablespace jinrilogindex 
  271. local 
  272. partition index_sname_01 tablespace jinrilogindex, 
  273. partition index_sname_02 tablespace jinrilogindex, 
  274. partition index_sname_03 tablespace jinrilogindex, 
  275. partition index_sname_04 tablespace jinrilogindex 
  276. ); 
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283. --循环导入数据 
  284. declare 
  285.         maxrecords constant int:=1000; 
  286.         i int :=1; 
  287.     begin 
  288.         for i in 1..maxrecords loop 
  289.           insert into t1 values(i,'ocpyang','2013-06-11 00:00:00'); 
  290.         end loop; 
  291.     dbms_output.put_line(' 成功录入数据! '); 
  292.     commit
  293.     end;  
  294.  
  295.  
  296.  
  297.  
  298. declare 
  299.         maxrecords constant int:=2000; 
  300.         i int :=1; 
  301.     begin 
  302.         for i in 1..maxrecords loop 
  303.           insert into t1 values(i,'ocpyang','2013-07-11 00:00:00'); 
  304.         end loop; 
  305.     dbms_output.put_line(' 成功录入数据! '); 
  306.     commit
  307.     end;  
  308.  
  309.  
  310. declare 
  311.         maxrecords constant int:=3000; 
  312.         i int :=1; 
  313.     begin 
  314.         for i in 1..maxrecords loop 
  315.           insert into t1 values(i,'ocpyang','2013-08-11 00:00:00'); 
  316.         end loop; 
  317.     dbms_output.put_line(' 成功录入数据! '); 
  318.     commit
  319.     end;  
  320.  
  321.  
  322.  
  323.  
  324. SQL> select count(*) from t1; 
  325.  
  326.  
  327.   COUNT(*) 
  328. ---------- 
  329.      6000 
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336. SQL> select count(*) from  t1 partition(p1) ; 
  337.  
  338.  
  339.   COUNT(*) 
  340. ---------- 
  341.          0 
  342.  
  343.  
  344. SQL> 
  345. SQL> select count(*) from  t1 partition(p2) ; 
  346.  
  347.  
  348.   COUNT(*) 
  349. ---------- 
  350.       1000 
  351.  
  352.  
  353.  
  354.  
  355. SQL> select count(*) from  t1 partition(p3) ; 
  356.  
  357.  
  358.   COUNT(*) 
  359. ---------- 
  360.       2000 
  361.  
  362.  
  363.  
  364.  
  365. SQL> select count(*) from  t1 partition(p4) ; 
  366.  
  367.  
  368.   COUNT(*) 
  369. ---------- 
  370.       3000 
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381. ---查看表数据分区情况 
  382.  
  383.  
  384. select utp.table_name,utp.partition_name,utp.tablespace_name from user_tab_partitions utp  
  385. where utp.table_name='T1'
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392. --查看分区索引分布情况 
  393.  
  394.  
  395. col index_name for a20 
  396. col partition_name for a20 
  397. col tablespace_name for a20 
  398. col status for a10 
  399. select index_name,null partition_name,tablespace_name,status 
  400. from user_indexes 
  401. where table_name='T1' 
  402. and partitioned='NO' 
  403. union  
  404. select index_name,partition_name,tablespace_name,status from user_ind_partitions 
  405. where index_name in 
  406. select index_name from user_indexes 
  407. where table_name='T1' 
  408. order by 1,2,3 
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417. --2.2 和中间表交换数据 
  418.  
  419.  
  420. create table t11 
  421. sid int not null , 
  422. sname  varchar2(50)  not null
  423. createtime date default sysdate   not null 
  424. )tablespace jason; 
  425.  
  426.  
  427.  
  428.  
  429. select count(*) from t11; 
  430.  
  431.  
  432. alter table t1 exchange partition p2 with table t11; 
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443. --查看无效的索引并重建 
  444.  
  445.  
  446. col index_name for a20 
  447. col partition_name for a20 
  448. col tablespace_name for a20 
  449. col status for a10 
  450. select index_name,null partition_name,status 
  451. from user_indexes 
  452. where table_name='T1' 
  453. and partitioned='NO' 
  454. union  
  455. select index_name,partition_name,status from user_ind_partitions 
  456. where index_name in 
  457. select index_name from user_indexes 
  458. where table_name='T1' 
  459. order by 1,2,3 
  460.  
  461.  
  462. INDEX_NAME                     PARTITION_NAME                 STATUS 
  463. ------------------------------ ------------------------------ -------- 
  464. INDEX_T1_01                    INDEX_SNAME_01                 USABLE 
  465. INDEX_T1_01                    INDEX_SNAME_02                 UNUSABLE 
  466. INDEX_T1_01                    INDEX_SNAME_03                 USABLE 
  467. INDEX_T1_01                    INDEX_SNAME_04                 USABLE 
  468. UN_T1_01                       P1                             USABLE 
  469. UN_T1_01                       P2                             UNUSABLE 
  470. UN_T1_01                       P3                             USABLE 
  471. UN_T1_01                       P4                             USABLE 
  472.  
  473.  
  474. alter index INDEX_T1_01  rebuild partition INDEX_SNAME_02; 
  475.  
  476.  
  477. alter index UN_T1_01  rebuild partition P2; 
  478.  
  479.  
  480.  
  481.  
  482. col index_name for a20 
  483. col partition_name for a20 
  484. col tablespace_name for a20 
  485. col status for a10 
  486. select index_name,null partition_name,status 
  487. from user_indexes 
  488. where table_name='T1' 
  489. and partitioned='NO' 
  490. union  
  491. select index_name,partition_name,status from user_ind_partitions 
  492. where index_name in 
  493. select index_name from user_indexes 
  494. where table_name='T1' 
  495. order by 1,2,3 
  496.  
  497.  
  498. INDEX_NAME                     PARTITION_NAME                 STATUS 
  499. ------------------------------ ------------------------------ -------- 
  500. INDEX_T1_01                    INDEX_SNAME_01                 USABLE 
  501. INDEX_T1_01                    INDEX_SNAME_02                 USABLE 
  502. INDEX_T1_01                    INDEX_SNAME_03                 USABLE 
  503. INDEX_T1_01                    INDEX_SNAME_04                 USABLE 
  504. UN_T1_01                       P1                             USABLE 
  505. UN_T1_01                       P2                             USABLE 
  506. UN_T1_01                       P3                             USABLE 
  507. UN_T1_01                       P4                             USABLE 
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518. select count(*) from t1 partition (p2); 
  519.  
  520.  
  521.   COUNT(*) 
  522. ---------- 
  523.          0 
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530. select count(*) from t11; 
  531.  
  532.  
  533.  COUNT(*) 
  534. --------- 
  535.      1000 
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544. --确定数据是否已经切换到新的表空间 
  545.  
  546.  
  547. SELECT TABLESPACE_NAME  
  548. FROM USER_TAB_PARTITIONS  
  549. WHERE TABLE_NAME='T1' AND PARTITION_NAME='P2'
  550.  
  551.  
  552. TABLESPACE_NAME 
  553. ------------------------------ 
  554. JASON 
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569. ---2.3中间表和归档表再次交换数据 
  570.  
  571.  
  572. create tablespace archive01 
  573. datafile 'E:\APP\ADMINISTRATOR\ORADATA\ORCL\archive01.DBF' 
  574. size 200M  autoextend on next 20M maxsize unlimited 
  575. extent management local autoallocate 
  576. segment space management auto 
  577.  
  578.  
  579.  
  580.  
  581. create tablespace archive02 
  582. datafile 'E:\APP\ADMINISTRATOR\ORADATA\ORCL\archive02.DBF' 
  583. size 200M  autoextend on next 20M maxsize unlimited 
  584. extent management local autoallocate 
  585. segment space management auto 
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594. create table t2 
  595. sid int not null , 
  596. sname  varchar2(50)  not null
  597. createtime date default sysdate   not null 
  598. PARTITION BY range(createtime) 
  599. (  
  600. PARTITION p1 VALUES LESS THAN ('2013-06-01 00:00:00') tablespace archive01, 
  601. PARTITION p2 VALUES LESS THAN ('2013-07-01 00:00:00') tablespace archive01, 
  602. PARTITION p3 VALUES LESS THAN ('2013-08-01 00:00:00') tablespace archive01, 
  603. PARTITION p4  VALUES LESS THAN (maxvalue) tablespace archive01 
  604. ) tablespace archive01; 
  605.  
  606.  
  607.  
  608.  
  609. create unique index un_t2_01 on t2(sid,createtime) 
  610. tablespace archive02 
  611. local
  612.  
  613.  
  614.  
  615.  
  616. alter table t2 add constraint pk_t2 primary key(sid,createtime); 
  617.  
  618.  
  619.  
  620.  
  621. select up.table_name,up.partition_name,up.tablespace_name from user_tab_partitions up  
  622. where up.table_name='T2'
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629. --查看分区索引分布情况 
  630.  
  631.  
  632.  
  633.  
  634. col index_name for a20 
  635. col partition_name for a20 
  636. col tablespace_name for a20 
  637. col status for a10 
  638. select index_name,null partition_name,tablespace_name,status 
  639. from user_indexes 
  640. where table_name='T2' 
  641. and partitioned='NO' 
  642. union  
  643. select index_name,partition_name,tablespace_name,status from user_ind_partitions 
  644. where index_name in 
  645. select index_name from user_indexes 
  646. where table_name='T2' 
  647. order by 1,2,3 
  648.  
  649.  
  650. INDEX_NAME           PARTITION_NAME       TABLESPACE_NAME      STATUS 
  651. -------------------- -------------------- -------------------- ---------- 
  652. UN_T2_01             P1                   ARCHIVE02            USABLE 
  653. UN_T2_01             P2                   ARCHIVE02            USABLE 
  654. UN_T2_01             P3                   ARCHIVE02            USABLE 
  655. UN_T2_01             P4                   ARCHIVE02            USABLE 
  656.  
  657.  
  658.  
  659.  
  660. select count(*) from t2; 
  661.  
  662.  
  663.  COUNT(*) 
  664. --------- 
  665.         0 
  666.  
  667.  
  668.  
  669.  
  670. --交换数据 
  671.  
  672.  
  673. alter table t2 exchange partition p2 with table t11 ; 
  674.  
  675.  
  676. select count(*) from t2; 
  677.  
  678.  
  679. select count(*) from t11;