数据库表空间整合方案1.问题描述目前,某服务系统数据库存在多个表空间,对数据库表的维护造成了一定的影响。
2.问题解决方案解决方案:采用脚本来转移数据库表、索引及特殊字段(lob字段类型)第一、表做表空间迁移时,首先使用查询语句把表迁移语句拼接起来,然后再执行结果中的语句,查询语句如下:select table_name,'alter table ' || table_name || ' move tablespace tablespaceNameA;' exewordsfrom all_tableswhere owner = '表拥有者' --表拥有者如:USERA(记住一定要大写)and tablespace_name <> ' tablespaceNameA' --不属于表空间 tablespaceNameA注释:请保留结果table_name列的结果,第三步要用到这些表,并执行exewords 列中产生的结果语句。
以下以UserA为例:Select table_name, 'alter table '||table_name||' move tablespace tablespaceNameA;' exewords from all_tables where owner='USERA' and tablespace_name<>' tablespaceNameA';结果语句略,执行exewords列中产生的语句。
第二、索引做表空间做迁移时,首先使用查询语句把索引迁移语句拼接起来,然后再执行结果中的语句,查询语句如下:select index_name,'alter index ' || index_name || ' rebuild tablespace tablespaceNameA;' exewordsfrom all_indexeswhere owner = '索引拥有者'--索引拥有者如:USERA(记住一定要大写)and tablespace_name <> 'tablespaceNameA'--表空间结果略,执行exewords列产生的结果。
以下以UserA为例:select index_name,'alter index ' || index_name || ' rebuild tablespace tablespaceNameA;' exewordsfrom all_indexeswhere owner = USERA'--数据库用户名and tablespace_name <> 'tablespaceNameA'--表空间(将要转移过去的表空间)结果略,执行exewords列产生的结果。
第三,转移lob字段,对于这种字段,在建立含有lob字段的表时,oracle会自动为lob字段建立两个单独的segment,一个用来存放lob数据,另一个用来存放lob索引,并且它们都会存储在对应表指定的表空间中。
但是当我们用alter table tb_name move tablespace tbs_name; 对表做表空间之间迁移时只能迁移非lob字段以外的segment,而如果要在移动表数据同时移动lob相关字段,就必需用如下的含有特殊参数的语句来完成:alter table tb_name move tablespace tbs_name lob (column_lob1,column_lob2) storeas(tablesapce tbs_name);具体步骤如下(以USERA为例):第(1)、select rs.exewords from(Select 'select ' || '''alter table ' || table_name ||' move tablespace tablespaceNameA lob(''||' || 'column_name' ||'||'') store as(tablespace tablespaceNameA);''exewords' ||' from user_tab_columns where table_name=''' || table_name ||''' and (data_type = ''BLOB'' or data_type = ''CLOB'' or data_type = ''NCLOB'') union ' exewordsfrom all_tableswhere owner = USERA'--数据库用户and tablespace_name <> 'tablespaceNameA'--转移后的表空间名称and table_name ='MYTEST') rs--第一个步骤保存的表名unionselect 'select ''temp'' exewords from dual where 1=2' exewords from dual;第(2)、从第(1)步中得到结果,并继续执行结果语句。
第(3)、从第(2)步中得到结果,并继续执行结果语句。
利用如下语句可以验证转移前和转移后的差别:select*from all_tables where owner= USERA'and tablespace_name<>'tablespaceNameA' select*from all_indexes where owner=' USERA'and tablespace_name<>'tablespaceNameA'例子:--=============================转移Blob字段例子================================================create table MyTest(id number,id2 number,myname varchar2(10),img1 blob,img2 clob)tablespace tablespaceNameBpctfree 10initrans 1maxtrans 255storage(initial 64Kminextents 1maxextents unlimited);alter table MyTestadd constraint PK_mytest primary key (ID)using indextablespace DEFCpctfree 10initrans 2maxtrans 255storage(initial 64Kminextents 1maxextents unlimited);create index ind_on_mytest_id2 on myTest (id2) tablespace tablespaceNameBpctfree 10initrans 2maxtrans 255storage(initial 64Kminextents 1maxextents unlimited);create index ind_on_mytest_myname on myTest (myname)tablespace tablespaceNameBpctfree 10initrans 2maxtrans 255storage(initial 64Kminextents 1maxextents unlimited);----select*from Mytest;-------第一步骤(执行如下语句,查询出需要转移的数据库表,并保留table_name列的结果,最后一步要用)select table_name,'alter table ' || table_name || ' move tablespace tablespaceNameA;' exewordsfrom all_tableswhere owner = 'USERA' --表拥有者and tablespace_name <> 'tablespaceNameA' --不属于表空间tablespaceNameA--第二步骤(执行上面生成的exewords列的结果)alter table MYTEST move tablespace tablespaceNameA;--第三步骤(执行如下语句,查出需要转移索引)select index_name,'alter index ' || index_name || ' rebuild tablespace tablespaceNameA;' exewordsfrom all_indexeswhere owner = 'USERA'--数据库用户名and tablespace_name <> 'tablespaceNameA'--表空间(将要转移过去的表空间)--第四步骤(执行上面生成的exewords列的结果)alter index PK_MYTEST rebuild tablespace tablespaceNameA;alter index IND_ON_MYTEST_ID2 rebuild tablespace tablespaceNameA;alter index IND_ON_MYTEST_MYNAME rebuild tablespace tablespaceNameA;--第五步骤(执行如下语句,查出大字段,并将其转移)--1)执行一下语句,拼出查询大字段的语句。
select rs.exewords from(Select 'select ' || '''alter table ' || table_name ||' move tablespace tablespaceNameA lob(''||' || 'column_name' ||'||'') store as(tablespace tablespaceNameA);''exewords' ||' from user_tab_columns where table_name=''' || table_name ||''' and (data_type = ''BLOB'' or data_type = ''CLOB'' ordata_type = ''NCLOB'') union ' exewordsfrom all_tableswhere owner = 'USERA'--数据库用户and tablespace_name <> 'tablespaceNameA'--转移后的表空间名称and table_name ='MYTEST') rs--第一个步骤保存的表名unionselect 'select ''temp'' exewords from dual where 1=2' exewords from dual; --2)执行以上语句执行后生成的exewords列的结果。