你好,游客 登录 注册 搜索
背景:
阅读新闻

用Partition Exchange(分区交换)卸载数据

[日期:2012-07-05] 来源:Linux社区  作者:coast_lichao [字体:大 中 小]

就这个操作,每次的select都要扫描单分区,几千万的数据,以上过程中每50000条提交一次,后面还有大量的delete。
SELECT COUNT(1)
        INTO v_count
        FROM T_MS_MEDIA_TASK T
        WHERE T.MONTHDAY = v_monthday;
处于各方面的考虑,我们没有允许这样的操作在数据库中正式上线,最后和他们经过沟通之后,确定了如下的方案:
采用partition exchange来达到目的,大致思路如下:
1,源表是list分区表,表上无global index,全部是local index。
2,创建一张中间表,结构和源表,目标端的hist表一致。
3,在中间表上创建与源表一致的索引。
4,hist(分区表)中的结构索引和源表中一致。
5,先把源分区交换到中间表,在把中间表交换到历史表中。

我的测试代码如下:


 

  1. SQL> create table test1  
  2.   2  (vid number ,  
  3.   3  v_date number,  
  4.   4  v_data varchar2(100))  
  5.   5  partition by range(v_date)  
  6.   6  (partition day_1 values less than (2 ),  
  7.   7  partition day_2 values less than (3 ),  
  8.   8  partition day_3 values less than (4 ),  
  9.   9  partition day_4 values less than (5 ),  
  10.  10  partition day_5 values less than (6 ),  
  11.  11  partition day_6 values less than (7 ),  
  12.  12  partition day_7 values less than (8 ),  
  13.  13  partition day_8 values less than (9 ),  
  14.  14  partition day_9 values less than (10),  
  15.  15  partition day_10 values less than (11),  
  16.  16  partition day_11 values less than (12),  
  17.  17  partition day_12 values less than (13),  
  18.  18  partition day_13 values less than (14),  
  19.  19  partition day_14 values less than (15),  
  20.  20  partition day_15 values less than (16),  
  21.  21  partition day_16 values less than (17),  
  22.  22  partition day_17 values less than (18),  
  23.  23  partition day_18 values less than (19),  
  24.  24  partition day_19 values less than (20),  
  25.  25  partition day_20 values less than (21),  
  26.  26  partition day_21 values less than (22),  
  27.  27  partition day_22 values less than (23),  
  28.  28  partition day_23 values less than (24),  
  29.  29  partition day_24 values less than (25),  
  30.  30  partition day_25 values less than (26),  
  31.  31  partition day_26 values less than (27),  
  32.  32  partition day_27 values less than (28),  
  33.  33  partition day_28 values less than (29),  
  34.  34  partition day_29 values less than (30),  
  35.  35  partition day_30 values less than (31),  
  36.  36  partition day_31 values less than (32),   
  37.  37  partition day_max values less than (maxvalue)  
  38.  38 );     
  39.   
  40. Table created.  
  41.   
  42. SQL> SQL> insert into test1   
  43.   2  (vid,  
  44.   3  v_date,  
  45.   4  v_data)  
  46.   5  select   
  47.   6  level ,  
  48.   7  mod(level,31)+1,  
  49.   8  lpad(level,60,'0')  
  50.   9  from dual   
  51.  10  connect by level<=100000;  
  52.   
  53. 100000 rows created.  
  54.   
  55. SQL>commit;   
  56. Commit complete.  
  57.   
  58. SQL> create index idx_1 on test1(vid) local;  
  59. SQL>create index idx_2 on test1(vid,v_date) local;  
  60.   
  61. SQL> create table test2 as select*from test1 where 1=2;  
  62. SQL> create index idx_3 on test2(vid,v_date);  
  63.   
  64. SQL> create table test3  
  65.   2  (vid number ,  
  66.   3  v_date number,  
  67.   4  v_data varchar2(100))  
  68.   5  partition by range(v_date)  
  69.   6  (partition day_1 values less than (2 ),  
  70.   7  partition day_2 values less than (3 ),  
  71.   8  partition day_3 values less than (4 ),  
  72.   9  partition day_4 values less than (5 ),  
  73.  10  partition day_5 values less than (6 ),  
  74.  11  partition day_6 values less than (7 ),  
  75.  12  partition day_7 values less than (8 ),  
  76.  13  partition day_8 values less than (9 ),  
  77.  14  partition day_9 values less than (10),  
  78.  15  partition day_10 values less than (11),  
  79.  16  partition day_11 values less than (12),  
  80.  17  partition day_12 values less than (13),  
  81.  18  partition day_13 values less than (14),  
  82.  19  partition day_14 values less than (15),  
  83.  20  partition day_15 values less than (16),  
  84.  21  partition day_16 values less than (17),  
  85.  22  partition day_17 values less than (18),  
  86.  23  partition day_18 values less than (19),  
  87.  24  partition day_19 values less than (20),  
  88.  25  partition day_20 values less than (21),  
  89.  26  partition day_21 values less than (22),  
  90.  27  partition day_22 values less than (23),  
  91.  28  partition day_23 values less than (24),  
  92.  29  partition day_24 values less than (25),  
  93.  30  partition day_25 values less than (26),  
  94.  31  partition day_26 values less than (27),  
  95.  32  partition day_27 values less than (28),  
  96.  33  partition day_28 values less than (29),  
  97.  34  partition day_29 values less than (30),  
  98.  35  partition day_30 values less than (31),  
  99.  36  partition day_31 values less than (32),   
  100.  37  partition day_max values less than (maxvalue)  
  101.  38  );  
  102.   
  103. Table created.  
  104.   
  105. SQL>  alter table test1 exchange partition day_1 with table test2 including indexes;  
  106.  alter table test1 exchange partition day_1 with table test2 including indexes  
  107.                                                        *  
  108. ERROR at line 1:  
  109. ORA-14098: index mismatch for tables in ALTER TABLE EXCHANGE PARTITION  
  110.   
  111.   
  112. SQL> !oerr ora 14098  
  113. 14098, 00000, "index mismatch for tables in ALTER TABLE EXCHANGE PARTITION"  
  114. // *Cause:  The two tables specified in the EXCHANGE have indexes which are  
  115. //          not equivalent  
  116. // *Action: Ensure that the indexes for the two tables have indexes which  
  117. //          follow this rule  
  118. //          For every non partitioned index for the non partitioned table,  
  119. //          there has to be an identical LOCAL index on the partitioned   
  120. //          table and vice versa. By identical, the column position, type  
  121. //          and size have to be the same.  
  122.   
  123. SQL> create index idx_4 on test2(vid);  
  124.   
  125. Index created.  
  126.   
  127. SQL> alter table test1 exchange partition day_1 with table test2 including indexes;  
  128.   
  129. Table altered.  
  130.   
  131. SQL> select DISTINCT index_name,status   
  132.   2  From dba_ind_partitions   
  133.   3  where index_name IN('IDX_1','IDX_2','IDX_3','IDX_4');  
  134.   
  135. INDEX_NAME                     STATUS  
  136. ------------------------------ --------  
  137. IDX_1                          USABLE  
  138. IDX_2                          USABLE  
  139.   
  140. SQL> select INDEX_NAME,STATUS   
  141.   2  from dba_indexes   
  142.   3  where index_name IN('IDX_1','IDX_2','IDX_3','IDX_4');  
  143.   
  144. INDEX_NAME                     STATUS  
  145. ------------------------------ --------  
  146. IDX_4                          VALID  
  147. IDX_1                          N/A  
  148. IDX_2                          N/A  
  149. IDX_3                          VALID  
  150.   
  151. SQL> create index idx_5 on test3(vid) local;  
  152.   
  153. Index created.  
  154.   
  155. SQL> create index idx_6 on test3(vid,v_date) local;  
  156.   
  157. Index created.  
  158.   
  159. SQL> alter table test3 exchange partition day_1 with table test2 including indexes;  
  160.   
  161. Table altered.  
  162.   
  163. SQL> select DISTINCT index_name,status   
  164.   2  From dba_ind_partitions   
  165.   3  where index_name IN('IDX_1','IDX_2','IDX_3','IDX_4','IDX_5','IDX_6');  
  166.   
  167. INDEX_NAME                     STATUS  
  168. ------------------------------ --------  
  169. IDX_1                          USABLE  
  170. IDX_2                          USABLE  
  171. IDX_5                          USABLE  
  172. IDX_6                          USABLE  
  173.   
  174. SQL> select INDEX_NAME,STATUS   
  175.   2  from dba_indexes   
  176.   3  where index_name IN('IDX_1','IDX_2','IDX_3','IDX_4','IDX_5','IDX_6');  
  177.   
  178. INDEX_NAME                     STATUS  
  179. ------------------------------ --------  
  180. IDX_6                          N/A  
  181. IDX_4                          VALID  
  182. IDX_5                          N/A  
  183. IDX_1                          N/A  
  184. IDX_2                          N/A  
  185. IDX_3                          VALID  
linux
相关资讯       Partition Exchange 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款