您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

使用导出导入(datapump)方式将普通表切换为分区表

2024/4/27 0:26:23发布6次查看
随着数据库数据量的不断增长,有些表需要由普通的堆表转换为分区表的模式。有几种不同的方法来对此进行操作,诸如导出表数据,然后创建分区表再导入数据到分区表;使用exchange partition方式来转换为分区表以及使用dbms_redefinition来在线重定义分区表。本
随着数据库数据量的不断增长,有些表需要由普通的堆表转换为分区表的模式。有几种不同的方法来对此进行操作,诸如导出表数据,然后创建分区表再导入数据到分区表;使用exchange partition方式来转换为分区表以及使用dbms_redefinition来在线重定义分区表。本文描述的是使用导出导入方式来实现,下面是具体的操作示例。
有关具体的dbms_redefinition在线重定义表的原理及步骤可参考:基于 dbms_redefinition 在线重定义表
1、主要步骤
2、准备环境
--创建用户sql> create user leshami identified by xxx;sql> grant dba to leshami;--创建演示需要用到的表空间sql> create tablespace tbs_tmp datafile '/u02/database/sybo2/oradata/tbs_tmp.dbf' size 10m autoextend on;sql> alter user leshami default tablespace tbs_tmp;sql> create tablespace tbs1 datafile '/u02/database/sybo2/oradata/tbs1.dbf' size 10m autoextend on;sql> create tablespace tbs2 datafile '/u02/database/sybo2/oradata/tbs2.dbf' size 10m autoextend on;sql> create tablespace tbs3 datafile '/u02/database/sybo2/oradata/tbs3.dbf' size 10m autoextend on;sql> conn leshami/xxx-- 创建一个lookup表create table lookup ( id number(10), description varchar2(50));--添加主键约束alter table lookup add ( constraint lookup_pk primary key (id));--插入数据insert into lookup (id, description) values (1, 'one');insert into lookup (id, description) values (2, 'two');insert into lookup (id, description) values (3, 'three');commit;--创建一个用于切换到分区的大表create table big_table ( id number(10), created_date date, lookup_id number(10), data varchar2(50));--填充数据到大表declare l_lookup_id lookup.id%type; l_create_date date;begin for i in 1 .. 10000 loop if mod(i, 3) = 0 then l_create_date := add_months(sysdate, -24); l_lookup_id := 2; elsif mod(i, 2) = 0 then l_create_date := add_months(sysdate, -12); l_lookup_id := 1; else l_create_date := sysdate; l_lookup_id := 3; end if; insert into big_table (id, created_date, lookup_id, data) values (i, l_create_date, l_lookup_id, 'this is some data for ' || i); end loop; commit;end;/--为大表添加主、外键约束,索引,以及添加触发器等.alter table big_table add ( constraint big_table_pk primary key (id));create index bita_created_date_i on big_table(created_date);create index bita_look_fk_i on big_table(lookup_id);alter table big_table add ( constraint bita_look_fk foreign key (lookup_id) references lookup(id));create or replace trigger tr_bf_big_table before update of created_date on big_table for each rowbegin :new.created_date := to_char (sysdate, 'yyyymmdd hh24:mi:ss');end tr_bf_big_table;/--收集统计信息exec dbms_stats.gather_table_stats('leshami', 'lookup', cascade => true);exec dbms_stats.gather_table_stats('leshami', 'big_table', cascade => true);
3、创建分区表
create table big_table2 ( id number(10), created_date date, lookup_id number(10), data varchar2(50))partition by range (created_date)(partition big_table_2012 values less than (to_date('01/01/2013', 'dd/mm/yyyy')) tablespace tbs1, partition big_table_2013 values less than (to_date('01/01/2014', 'dd/mm/yyyy')) tablespace tbs2, partition big_table_2014 values less than (maxvalue)) tablespace tbs3;--可以直接使用insert方式来填充数据到分区表,如下insert into big_table2 select * from big_table;
4、通过datapump方式导出导入数据到分区表
--该方式主要用于从不同的数据库迁移数据,比如源库源表为普通表,而目标库为分区表 $ expdp leshami/xxx directory=db_dump_dir dumpfile=big_table.dmp logfile=exp_big_tb.log tables=big_table content=data_onlysql> rename big_table to big_table_old;table renamed.sql> rename big_table2 to big_table;table renamed.$ impdp leshami/xxx directory=db_dump_dir dumpfile=big_table.dmp logfile=imp__big_tb.log tables=big_tableexec dbms_stats.gather_table_stats('leshami', 'big_table', cascade => true);--下面是导入数据之后的结果sql> select table_name, partition_name,high_value,num_rows 2 from user_tab_partitions where table_name='big_table';table_name partition_name high_value num_rows------------------------------ ------------------------------ --------------------- ----------big_table2 big_table_2012 to_date(' 2013-01-01 3333big_table2 big_table_2013 to_date(' 2014-01-01 3334big_table2 big_table_2014 maxvalue 3333--如果数据无异常可以删除源表以便为分区表添加相应索引及约束,如果未删除源表,需要使用单独的索引,约束名等sql> drop table big_table;table dropped.alter table big_table add ( constraint big_table_pk primary key (id));create index bita_created_date_i on big_table(created_date) local;create index bita_look_fk_i on big_table(lookup_id) local;alter table big_table add ( constraint bita_look_fk foreign key (lookup_id) references lookup(id));--触发器也需要单独添加到分区表create or replace trigger tr_bf_big_table before update of created_date on big_table for each rowbegin :new.created_date := to_char (sysdate, 'yyyymmdd hh24:mi:ss');end tr_bf_big_table2;/5、后记
更多参考
有关oracle rac请参考
有关oracle 网络配置相关基础以及概念性的问题请参考:
有关基于用户管理的备份和备份恢复的概念请参考
有关rman的备份恢复与管理请参考
有关oracle体系结构请参考
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录