普通表转分区表和交换分区(oracle)

本文介绍了将普通表转换为分区表的四种方法,包括Export/Import方法、使用子查询的Insert方法、分区交换方法及DBMS_REDEFINITION方法,并详细展示了分区交换的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 普通表转分区表方法  
  2.    
  3.   
  4. 将普通表转换成分区表有4种方法:  
  5.        1. Export/import method  
  6.        2. Insert with a subquery method  
  7.        3. Partition exchange method  
  8.        4. DBMS_REDEFINITION  
  9.          
  10. select * from t_user_info_test;  
  11. --方法一  
  12. drop table t_phone_test purge;  
  13. create table t_phone_test(phone,part) nologging  partition by list(part)  
  14. (  
  15. partition p0 values('0'),  
  16. partition p1 values('1'),  
  17. partition p2 values('2'),  
  18. partition p3 values('3'),  
  19. partition p4 values('4'),  
  20. partition p5 values('5'),  
  21. partition p6 values('6'),  
  22. partition p7 values('7'),  
  23. partition p8 values('8'),  
  24. partition p9 values('9')  
  25. )   
  26. as   
  27. select user_mobile phone,substr(user_mobile,-1,1) part  
  28. from t_user_info_test;  
  29.   
  30.   
  31. select * from t_phone_test partition(p0);  
  32.   
  33. select * from t_phone_test where part='0';  
  34.   
  35. --方法二 交换分区  
  36.      这种方法只是对数据字典中分区和表的定义进行了修改,没有数据的修改或复制,效率最高。适用于包含大数据量的表转到分区表中的一个分区的操作。尽量在闲时进行操作。  
  37.   
  38. 交换分区的操作步骤如下:  
  39.      1. 创建分区表,假设有2个分区,P1,P2.  
  40.      2. 创建表A存放P1规则的数据。  
  41.      3. 创建表B 存放P2规则的数据。  
  42.      4. 用表A 和P1 分区交换。 把表A的数据放到到P1分区  
  43.      5. 用表B 和p2 分区交换。 把表B的数据存放到P2分区。  
  44.   
  45.   
  46.   
  47. create table t_phone_test_0 nologging  
  48. as   
  49. select user_mobile phone,substr(user_mobile,-1,1) part  
  50. from t_user_info_test where substr(user_mobile,-1,1)='0';  
  51.   
  52. select count(*) from t_phone_test where part='0';  
  53. --4410  
  54. select count(*) from t_user_info_test where substr(user_mobile,-1,1)='0';  
  55. --4410  
  56.   
  57. alter table t_phone_test exchange partition p0 with table t_phone_test_0;  
  58.   
  59.   
  60. delete from   t_phone_test_0;  
  61.   
  62. select count(*) from t_phone_test where part='0';  
  63. select count(*) from t_phone_test_0;  
  64.   
  65. insert into t_phone_test(phone,part) values('15267046070','0');  
  66.   
  67. --p0一条数据,t_phone_test_0里4410条数据,交换之后p0是4410,t_phone_test_0是1,再执行一次数据又换回来了。  
  68.   
  69. insert into t_phone_test_0(phone,part) values('15267046070','1');  
  70. alter table t_phone_test exchange partition p0 with table t_phone_test_0;  
  71. delete from t_phone_test_0 where part='1';  
  72.   
  73.   
  74. --合并分区  
  75. ----alter table tbname merge partitions/subpartitions pt1,pt2 into partition/subpartition pt3;  
  76.   
  77. alter table t_phone_test merge partitions p0,p1 into partition p0;  
  78.   
  79.   
  80. select count(*) from t_phone_test where part='0';  
  81. select count(*) from t_phone_test where part='1';  
  82.   
  83. select count(*)  from t_phone_test partition(p0);  
  84. select count(*)  from t_phone_test partition(p1);  
  85.   
  86.   
  87.   
  88.  alter table t_phone_test  add partition p10 values(default);  
  89.   
  90. insert into t_phone_test(phone,part) values('15267046010','10');  
  91. insert into t_phone_test(phone,part) values('15267046020','20');  
  92.   
  93. select * from   
  94.   
  95. --  
  96. alter table t_phone_test drop partition p10;  
  97.  alter table t_phone_test  add partition p10 values'10');  
  98.    
  99. alter table t_phone_test exchange partition p10 with table t_phone_test_10;  
  100. --ORA-14097: column type or size mismatch in ALTER TABLE EXCHANGE PARTITION  
  101. alter table T_PHONE_TEST_10 modify PART VARCHAR2(2);  
  102. alter table t_phone_test merge partitions p0,p10 into partition p0;  
  103.   
  104. --此时p0中有p0和p10的数据,但是p0的list不再是0而是0和10  
  105.   partition P0 values ('10''0')  
  106.     tablespace APP_DATAN  
  107.     pctfree 10  
  108.     initrans 1  
  109.     maxtrans 255  
  110.     storage  
  111.     (  
  112.       initial 1M  
  113.       next 1M  
  114.       minextents 1  
  115.       maxextents unlimited  
  116.       pctincrease 0  
  117.     ),  
  118.       
  119. alter table t_phone_test exchange partition p0 with table t_phone_test_10;     
  120. alter table t_phone_test drop partition p0;  
  121. alter table t_phone_test  add partition p0 values'0');      
  122.   
  123. alter table t_phone_test exchange partition p0 with table t_phone_test_10;     
  124.   
  125.   
  126. drop table t_phone_test_10 purge;  
  127. create table t_phone_test_10 nologging  
  128. as   
  129. select user_mobile phone,substr(user_mobile,-2,2) part  
  130. from t_user_info_test where substr(user_mobile,-2,2)='10';  
  131.   
  132. drop table t_phone_test_0 purge;  
  133. create table t_phone_test_0 nologging   
  134. as  
  135. select  phone,substr(phone,-1,1) part  
  136. from t_phone_test_10;  
  137.   
  138. alter table t_phone_test exchange partition p0 with table t_phone_test_0;  
  139.   
  140.   
  141. select * from t_phone_test_10;  
  142.   
  143.   
  144.   
  145. select count(*)  from t_phone_test partition(p0);  
  146. select count(*)  from t_phone_test partition(p10);  
  147. select count(*) from t_phone_test_10;  
  148. select count(*) from t_phone_test_0;  
  149.   
  150.   
  151.   
  152. select substr('123456',-1,1),substr('123456',-2,2),substr('123456',-3,2) from dual;  
  153.   
  154.   
  155. ---------------------------------------------------------  
  156. 1.创建分区表  
  157. drop table t_phone_test purge;  
  158. create table t_phone_test(phone,part) nologging  partition by list(part)  
  159. (  
  160. partition p0 values('0'),  
  161. partition p1 values('1'),  
  162. partition p2 values('2'),  
  163. partition p3 values('3'),  
  164. partition p4 values('4'),  
  165. partition p5 values('5'),  
  166. partition p6 values('6'),  
  167. partition p7 values('7'),  
  168. partition p8 values('8'),  
  169. partition p9 values('9')  
  170. )   
  171. as   
  172. select user_mobile phone,substr(user_mobile,-1,1) part  
  173. from t_user_info_test;  
  174.   
  175. select count(*)  from t_phone_test partition(p0);--4410  
  176. select count(*)  from t_phone_test partition(p10);  
  177. select count(*) from t_phone_test_10;  
  178. select count(*) from t_phone_test_0;  
  179.   
  180. 2.创建基表  
  181. drop table t_phone_test_10 purge;  
  182. create table t_phone_test_10 nologging  
  183. as  
  184. select  phone,substr(phone,-2,2) part  
  185. from t_phone_test where substr(phone,-2,2)='10';  
  186.   
  187. select count(*) from t_phone_test_10;--406  
  188.   
  189. --ORA-14097: column type or size mismatch in ALTER TABLE EXCHANGE PARTITION  
  190. alter table T_PHONE_TEST_10 modify PART VARCHAR2(2);  
  191.   
  192. 3.添加分区  
  193. alter table t_phone_test  add partition p10 values'10');      
  194. select count(*)  from t_phone_test partition(p10);--0  
  195. 4.交换分区  
  196. alter table t_phone_test exchange partition p10 with table t_phone_test_10;     
  197. select count(*)  from t_phone_test partition(p10);--406  
  198. 5.合并分区  
  199. alter table t_phone_test merge partitions p0,p10 into partition p0;  
  200. select count(*)  from t_phone_test partition(p0);--4816  
  201. --此时p0中有p0和p10的数据,但是p0的list不再是0而是0和10  
  202.   partition P0 values ('10''0')  
  203.     tablespace APP_DATAN  
  204.     pctfree 10  
  205.     initrans 1  
  206.     maxtrans 255  
  207.     storage  
  208.     (  
  209.       initial 1M  
  210.       next 1M  
  211.       minextents 1  
  212.       maxextents unlimited  
  213.       pctincrease 0  
  214.     ),  
  215.       
  216. 6.交换分区  
  217. alter table t_phone_test exchange partition p0 with table t_phone_test_10;    
  218.   
  219. select count(*)  from t_phone_test partition(p0);--0  
  220. select count(*) from t_phone_test_10;--4816  
  221.   
  222.   
  223. 6.删除分区 和添加分区  
  224. alter table t_phone_test  drop partition p0;  
  225. alter table t_phone_test  add partition p0 values('0');  
  226.   
  227. 7.筛选数据  
  228. drop table t_phone_test_0 purge;  
  229. create table t_phone_test_0 nologging  
  230. as  
  231. select  phone,substr(phone,-1,1) part  
  232. from t_phone_test_10 where substr(phone,-1,1)='0';  
  233.   
  234. select count(*) from t_phone_test_0;--4816  
  235.   
  236. 8.交换分区  
  237. alter table t_phone_test exchange partition p0 with table t_phone_test_0;    
  238.   
  239. select count(*)  from t_phone_test partition(p0);--4816  
  240. select count(*) from t_phone_test_0;--0  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值