普通表转分区表4方法

简介:

将普通表转换成分区表有4种方法:
  1. Export/import method 
  2. Insert with a subquery method 
  3. Partition exchange method 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
Export/import method
*/
创建一个非分区表
test@CISCOSYS>  create  table  p1(
2     id  int ,
3      name  varchar2(32)
4  );
表已创建。
test@CISCOSYS>  select  count (*)  from  p1;
COUNT (*)
----------
10000
drop  table  p1;
C:\Documents  and  Settings\Administrator>exp test/test file=d:/p1.dmp tables=(p1);
Export: Release 10.2.0.1.0 - Production  on  星期三 1月 15 15:04:21 2014
Copyright (c) 1982, 2005, Oracle.   All  rights reserved.
连接到: Oracle  Database  10g Enterprise Edition Release 10.2.0.1.0 - Production
With  the Partitioning, OLAP  and  Data Mining options
已导出 ZHS16GBK 字符集和 AL16UTF16  NCHAR  字符集
即将导出指定的表通过常规路径...
. . 正在导出表                              P1导出了       10000 行
EXP-00011: TEST.; 不存在
导出成功终止, 但出现警告。
create  table  p1(
id  int ,
name  varchar2(32)
)
partition  by  range(id)
(
partition p_r_1000  values  less than(1000),
partition p_r_2000  values  less than(2000),
partition p_r_3000  values  less than(maxvalue)
);
imp test/test file=d:/p1.dmp tables=(p1)  ignore =y;
test@CISCOSYS>  select  count (*)  from  p1 partition(p_r_1000);
COUNT (*)
----------
999
test@CISCOSYS>  select  count (*)  from  p1 partition(p_r_2000);
COUNT (*)
----------
1000
test@CISCOSYS>  select  count (*)  from  p1 partition(p_r_3000);
COUNT (*)
----------
8001
转换成功
/*
Insert  with  a subquery method
*/
create  table  p2
partition  by  range(id)
(
partition p_r_1000  values  less than(1000),
partition p_r_2000  values  less than(2000),
partition p_r_3000  values  less than(maxvalue)
as  select  from  p1;
test@CISCOSYS>   select  count (*)  from  p2 partition(p_r_3000);
COUNT (*)
----------
8001
test@CISCOSYS>  select  count (*)  from  p2 partition(p_r_2000);
COUNT (*)
----------
1000
/**
3交换分区:Partition exchange method
*/
test@CISCOSYS>   create  table  pe_old  as  select   from  p2;
表已创建。
注:并没将原分区结构复制过来。
test@CISCOSYS>  select  count (*)  from  pe_old;
COUNT (*)
----------
10000
创建分区表
test@CISCOSYS>  create  table  pe_new(
2     id  int ,
3      name  varchar2(32)
4  )
5  partition  by  range(id)
6  (
7     partition pe_r_5000  values  less than(5000),
8     partition pe_r_10000  values  less than(maxvalue)
9  );
表已创建。
下面将pe_old 中的数据 转移到pe_new数据表中
test@CISCOSYS>  create  table  pe1  as  select  from  pe_old  where  id<5000;
表已创建。
test@CISCOSYS>  create  table  pe2  as  select  from  pe_old  where  id>=5000;
表已创建。
test@CISCOSYS>  select  count (*)  from  pe1;
COUNT (*)
----------
4999
test@CISCOSYS>  select  count (*)  from  pe2;
COUNT (*)
----------
5001
test@CISCOSYS>  alter  table  pe_new  exchange partition pe_r_5000  with  table  pe1;
表已更改。
test@CISCOSYS>  alter  table  pe_new  exchange partition pe_r_10000  with  table  pe2;
表已更改。
test@CISCOSYS>  select  count (*)  from  pe_new;
COUNT (*)
----------
10000
test@CISCOSYS>  select  count (*)  from  pe1;
COUNT (*)
----------
0
test@CISCOSYS>  select  count (*)  from  pe2;
COUNT (*)
----------
0
test@CISCOSYS>  select  count (*)  from  pe_new partition(pe_r_5000);
COUNT (*)
----------
4999
/**
4.使用在线重定义转换
http://blog.csdn.net/tianlesoftware/article/details/6218693
*/
创建基本表
test@CISCOSYS>  create  table  pl_plain  as  select  from  pe_new;
表已创建。
test@CISCOSYS>  create  index  pl_plain_idx  on  pl_plain(id);
索引已创建。
创建临时的分区表
test@CISCOSYS>    create  table  pl_part2(
id  int ,
name  varchar2(32)
)
partition  by  range(id)
(
partition pl2_r1_1000  values  less than(1000),
partition pl2_r2_3000  values  less than(3000),
partition pl2_r3_3000  values  less than(maxvalue)
);
表已创建。
test@CISCOSYS>  select  count (*)  from  pl_plain;
COUNT (*)
----------
10000
test@CISCOSYS>  exec  dbms_stats.gather_table_stats( 'test' 'pl_plain' cascade  =>  true );
PL/SQL 过程已成功完成。
检测合理性
test@CISCOSYS>  alter  table  pl_plain  add  constraint  pk_plain  primary  key (id);
表已更改。
test@CISCOSYS>  exec  dbms_redefinition.can_redef_table( 'test' 'pl_plain' );
PL/SQL 过程已成功完成。
开始重定义
test@CISCOSYS>  BEGIN
DBMS_REDEFINITION.start_redef_table(
uname =>  'TEST' ,
orig_table =>  'pl_plain' ,
int_table =>  'pl_part2' ,
DBMS_REDEFINITION.CONS_USE_PK);
END ;
/
PL/SQL 过程已成功完成。
同步新表
test@CISCOSYS>   BEGIN
2     dbms_redefinition.sync_interim_table(
3     uname =>  'TEST' ,
4     orig_table =>  'pl_plain' ,
5     int_table =>  'pl_part' );
6      END ;
7     /
PL/SQL 过程已成功完成。
test@CISCOSYS>  select  count (*)  from  pl_part;
COUNT (*)
----------
10000
此时分区表中数据已经同步过来了。
结束重定义 (此时基表与临时表进行了交换)
SQL>  BEGIN
2    dbms_redefinition.finish_redef_table(
3    uname =>  'TEST' ,
4    orig_table =>  'pl_plain' ,
5   int_table =>  'pl_part' );
6     END ;
7    /
test@CISCOSYS>  select  segment_name,segment_type,PARTITION_NAME  from  dba_segments  where  segment_name  like  '%PLAIN%' ;
SEGMENT_NAME         SEGMENT_TYPE       PARTITION_NAME
-------------------- ------------------ ------------------------------
PL_PLAIN_IDX          INDEX
PL_PLAIN              TABLE  PARTITION    PL_R3_3000
PL_PLAIN              TABLE  PARTITION    PL_R2_3000
PL_PLAIN              TABLE  PARTITION    PL_R1_1000

 



本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/1351990,如需转载请自行联系原作者

相关文章
|
OLAP 数据库 索引
分区表
分区表
157 0
|
关系型数据库 MySQL
|
SQL 测试技术 Go
|
SQL 索引 数据库
|
索引 SQL
对已存在的表进行分区时遇到的坑
在网上能够找到很多关于表分区的资料,可是大部分都是在介绍如何给一个新表创建表分区,而对已存在的表如何做分区的文章相对比较少,因此一些坑没有被“挖掘”出来或者“曝光率”比较低。
1539 0
|
测试技术 索引
非分区表是否可以创建分区索引?
有同事问一个问题, 一张非分区表,是否可以创建分区索引? 答案是可以,但分区索引的类型有限制。 MOS这篇文章给出了答案,以及一些例子,What Is The Global Partitioned Index On Non Partitioned Table? (文档 ID 1612359.1)。
1138 0
|
存储 索引
|
SQL 存储 索引