Oracle表空间整合例子
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
数据库表空间整合方案
1.问题描述
目前,某服务系统数据库存在多个表空间,对数据库表的维护造成了一定的
影响。
2.问题解决方案
解决方案:采用脚本来转移数据库表、索引及特殊字段
(lob字段类型)
第一、表做表空间迁移时,首先使用查询语句把表迁移语句拼接起来,然后再执行结果中的语句,查询语句如下:
select table_name,
'alter table ' || table_name || ' move tablespace tablespaceNameA;' exewords
from all_tables
where 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;' exewords
from all_indexes
where owner = '索引拥有者'--索引拥有者如:USERA(记住一定要大写)
and tablespace_name <> 'tablespaceNameA'--表空间
结果略,执行exewords列产生的结果。
以下以UserA为例:
select index_name,
'alter index ' || index_name || ' rebuild tablespace tablespaceNameA;' exewords
from all_indexes
where 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) store
as(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 ' exewords
from all_tables
where owner = USERA'--数据库用户
and tablespace_name <> 'tablespaceNameA'--转移后的表空间名称
and table_name ='MYTEST') rs--第一个步骤保存的表名
union
select '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 tablespaceNameB
pctfree 10
initrans 1
maxtrans 255