网上有很多例子以时间来作为划分的,对于此大家可以自己去查找相关方法,本文针对的是采用 number型id进行分区,用这个的原因主要是当前表中日期字段好多空值,这导致后面的在线重定义有问题,遂采用id来作为间隔。
首先进入命令窗口
1.创建测试表ZPGZZDB_NEWW_TB
SQL> create table ZPGZZDB_NEWW_TB(ID NUMBER not null,SCDDID NUMBER,ZDRQ DATE, ZDR NUMBER,BMID NUMBER,DYBZ VARCHAR2(6));
Table created
2.给测试表导入数据
SQL> insert into ZPGZZDB_NEWW_TB select id,scddid,zdrq,zdr,bmid,dybz from ZPGZZDB_TB;
27715 rows inserted
SQL> alter table ZPGZZDB_NEWW_TB add constraint pk_t primary key(id);
Table altered
SQL> EXEC DBMS_REDEFINITION.CAN_REDEF_TABLE('smbf', 'ZPGZZDB_NEWW_TB',DBMS_REDEFINITION.CONS_USE_PK);
PL/SQL procedure successfully completed
这一步可能会出现如下错误:
ERROR at line 1:
ORA-06550: line 1, column 53:
PLS-00201: identifier 'DBMS_REDEFINITION' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
这一步出错的原因是由于没有给‘DBMS_REDEFINITION’执行权限,只要授权一下,就不会出现这个错误: smbf为数据库用户名
GRANT ALL ON SYS.DBMS_REDEFINITION TO smbf
5.建立中间表及分区
SQL> CREATE TABLE ZPGZZDB_NEE_TB (ID NUMBER PRIMARY KEY,SCDDID NUMBER,ZDRQ DATE,ZDR NUMBER, BMID NUMBER,DYBZ VARCHAR2(6)) PARTITION BY RANGE (ID)
2 (PARTITION p1 VALUES LESS THAN (10001) ,
3 PARTITION p2 VALUES LESS THAN (20001) ,
4 PARTITION p3 VALUES LESS THAN (30001) ,
5 PARTITION p4 VALUES LESS THAN (40001),
6 PARTITION p5 VALUES LESS THAN (50001),
7 PARTITION p6 VALUES LESS THAN (60001),
8 PARTITION p7 VALUES LESS THAN (70001),
9 PARTITION p8 VALUES LESS THAN (80001),
10 PARTITION p9 VALUES LESS THAN (90001),
11 PARTITION p10 VALUES LESS THAN (100001),
12 PARTITION p11 VALUES LESS THAN (110001),
13 PARTITION p12 VALUES LESS THAN (120001),
14 PARTITION p13 VALUES LESS THAN (130001))
15 ;
这里建立了13个分区
6.查询下建立的分区
SQL> select partition_name from user_tab_partitions where table_name='ZPGZZDB_NEE_TB';
7、在线重定义
SQL> EXEC DBMS_REDEFINITION.START_REDEF_TABLE('smbf', ' ZPGZZDB_NEWW_TB', 'ZPGZZDB_NEE_TB ');
PL/SQL procedure successfully completed
注意:
分区一定要包括表里的所有数据,因为我表里有120000条数据,所以我建立了13个分区。如果出现
ERROR at line 1:
ORA-12091: cannot online redefine table "test"."T" with
materialized views
ORA-06512: at "SYS.DBMS_REDEFINITION", line 50
ORA-06512: at "SYS.DBMS_REDEFINITION", line 1343
ORA-06512: at line 1
这个原因就是分区表建立时,有的值没被包括在里面,所以切记最后包含的数据长度要大于当前表里的数据长度。
8、结束在线重定义
SQL> EXEC DBMS_REDEFINITION.FINISH_REDEF_TABLE('smbf', 'ZPGZZDB_NEWW_TB', ' ZPGZZDB_NEE_TB');
PL/SQL procedure successfully completed
9、查询分区数据
select count(*) from ZPGZZDB_NEWW_TB partition(P1);
10.7.校验完数据.清空中间表
truncate table ZPGZZDB_NEE_TB;
drop table ZPGZZDB_NEE_TB;