当前位置:网站首页>Oracle数据文件收缩_最佳实践_超简单方法
Oracle数据文件收缩_最佳实践_超简单方法
2022-08-03 05:27:00 【谷凯Jump】
Oracle数据文件收缩_最佳实践_超简单方法
前言
之前翻阅过无数文档,实践证明Oracle高水位降低后,数据文件占用服务器空间不会减小。
只能通过重新建立表空间来实现
如下图,表空间内容大小4.92GB,数据文件却分配了144GB
使用我的SQL,99%可行有效!超级便捷!拼接SQL+执行 两步完成
查询出待执行脚本
--查询出创建新表空间的SQL
select 'CREATE TABLESPACE '|| :MY_TABLE_SPACE ||'_TEMP DATAFILE ' as sql_text from dual
union all
select * from (
select ''''||file_name||'_TEMP'' SIZE 100M AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED'||
(case when row_number() over(partition by tablespace_name order by file_name desc)=1 then '' else ',' end)
as sql from dba_data_files where tablespace_name=:MY_TABLE_SPACE order by file_name)
union all
select 'LOGGING EXTENT MANAGEMENT LOCAL AUTOALLOCATE BLOCKSIZE 8K SEGMENT SPACE MANAGEMENT AUTO FLASHBACK ON;' as sql_text from dual
union all
--查询出移动表空间的SQL--同时将重建索引的SQL查出
select movs.sql_text||
replace((case when movs.rn>1 then '' else
listagg((case when b.index_name like '%$$%' or b.index_name is null then '' else
'alter index '|| b.table_owner || '.' || b.index_name ||' rebuild;' end),';') within group (order by movs.table_name) end)
,';;',';') as sqltext2
from
(select mov.*,row_number() over (partition by table_name order by seq desc) as rn from
(
select 'alter table ' || owner || '.' || segment_name || ' move tablespace '|| :MY_TABLE_SPACE ||'_TEMP;' sql_text,owner,segment_name,segment_name as table_name,3 as seq from dba_segments where tablespace_name = :MY_TABLE_SPACE and segment_type='TABLE'
union all
select 'alter index ' || owner || '.' || segment_name || ' rebuild tablespace '|| :MY_TABLE_SPACE ||'_TEMP;' sql_text,owner,segment_name,segment_name as table_name,1 as seq from dba_segments where tablespace_name = :MY_TABLE_SPACE and segment_type='INDEX'
union all
select 'alter table ' || owner || '.' || table_name || ' move lob(' || column_name || ') store as(tablespace '|| :MY_TABLE_SPACE ||'_TEMP);' sql_text,owner,column_name as segment_name,table_name,2 as seq from dba_lobs where tablespace_name = :MY_TABLE_SPACE
) mov order by owner,table_name,segment_name,seq
) movs,user_indexes b where movs.owner=b.table_owner(+) and movs.table_name=b.table_name(+)
group by movs.owner,movs.table_name,movs.segment_name,movs.sql_text,movs.rn
union all
--查询出删除原表空间的SQL
select 'DROP tablespace ' || :MY_TABLE_SPACE || ' INCLUDING CONTENTS AND DATAFILES;' as sql_text from dual
union all
--查询出重建原表空间的SQL
select 'CREATE TABLESPACE ' || :MY_TABLE_SPACE || ' DATAFILE ' as sql_text from dual
union all
select sql_text from (
select ''''||file_name||''' SIZE 100M AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED'||
(case when row_number() over(partition by tablespace_name order by file_name desc)=1 then '' else ',' end)
as sql_text from dba_data_files where tablespace_name=:MY_TABLE_SPACE order by file_name)
union all
select 'LOGGING EXTENT MANAGEMENT LOCAL AUTOALLOCATE BLOCKSIZE 8K SEGMENT SPACE MANAGEMENT AUTO FLASHBACK ON;' as sql_text from dual
union all
--查询出移动回去表空间的SQL--同时将重建索引的SQL查出
select movs.sql_text||
replace((case when movs.rn>1 then '' else
listagg((case when b.index_name like '%$$%' or b.index_name is null then '' else
'alter index '|| b.table_owner || '.' || b.index_name ||' rebuild;' end),';') within group (order by movs.table_name) end)
,';;',';') as sqltext2
from
(select mov.*,row_number() over (partition by table_name order by seq desc) as rn from
(
select 'alter table ' || owner || '.' || segment_name || ' move tablespace '|| :MY_TABLE_SPACE ||';' sql_text,owner,segment_name,segment_name as table_name,3 as seq from dba_segments where tablespace_name = :MY_TABLE_SPACE and segment_type='TABLE'
union all
select 'alter index ' || owner || '.' || segment_name || ' rebuild tablespace '|| :MY_TABLE_SPACE ||';' sql_text,owner,segment_name,segment_name as table_name,1 as seq from dba_segments where tablespace_name = :MY_TABLE_SPACE and segment_type='INDEX'
union all
select 'alter table ' || owner || '.' || table_name || ' move lob(' || column_name || ') store as(tablespace '|| :MY_TABLE_SPACE ||');' sql_text,owner,column_name as segment_name,table_name,2 as seq from dba_lobs where tablespace_name = :MY_TABLE_SPACE
) mov order by owner,table_name,segment_name,seq
) movs,user_indexes b where movs.owner=b.table_owner(+) and movs.table_name=b.table_name(+)
group by movs.owner,movs.table_name,movs.segment_name,movs.sql_text,movs.rn
union all
--查询出删除临时辅助表空间的SQL
select 'DROP tablespace ' || :MY_TABLE_SPACE || '_TEMP INCLUDING CONTENTS AND DATAFILES;' as sql_text from dual
开始执行
CREATE TABLESPACE MESDATA_L1_TEMP DATAFILE
'+DATA/mesdb/datafile/mesdata_l1_01.dbf_TEMP' SIZE 100M AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED,
'+DATA/mesdb/datafile/mesdata_l1_02.dbf_TEMP' SIZE 100M AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED,
'+DATA/mesdb/datafile/mesdata_l1_03.dbf_TEMP' SIZE 100M AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED,
'+DATA/mesdb/datafile/mesdata_l1_04.dbf_TEMP' SIZE 100M AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED
LOGGING EXTENT MANAGEMENT LOCAL AUTOALLOCATE BLOCKSIZE 8K SEGMENT SPACE MANAGEMENT AUTO FLASHBACK ON;
alter table MES.XXXXX move lob(SQLTEXT) store as(tablespace MESDATA_L1_TEMP);
alter table MES.XXXXX move tablespace MESDATA_L1_TEMP;alter index MES.I_XXXXX_01 rebuild;alter index MES.I_XXXXX_02 rebuild;alter index MES.PK_XXXXX rebuild;
alter table MES.YYYYY move lob(MAPFILE) store as(tablespace MESDATA_L1_TEMP);
alter table MES.ZZZZZ move lob(XLSBODY) store as(tablespace MESDATA_L1_TEMP);alter index MES.PK_ZZZZZ rebuild;
DROP tablespace MESDATA_L1 INCLUDING CONTENTS AND DATAFILES;
CREATE TABLESPACE MESDATA_L1 DATAFILE
'+DATA/mesdb/datafile/mesdata_l1_01.dbf' SIZE 100M AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED,
'+DATA/mesdb/datafile/mesdata_l1_02.dbf' SIZE 100M AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED,
'+DATA/mesdb/datafile/mesdata_l1_03.dbf' SIZE 100M AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED,
'+DATA/mesdb/datafile/mesdata_l1_04.dbf' SIZE 100M AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED
LOGGING EXTENT MANAGEMENT LOCAL AUTOALLOCATE BLOCKSIZE 8K SEGMENT SPACE MANAGEMENT AUTO FLASHBACK ON;
alter table MES.XXXXX move lob(SQLTEXT) store as(tablespace MESDATA_L1);
alter table MES.XXXXX move tablespace MESDATA_L1;alter index MES.I_XXXXX_01 rebuild;alter index MES.I_XXXXX_02 rebuild;alter index MES.PK_XXXXX rebuild;
alter table MES.YYYYY move lob(MAPFILE) store as(tablespace MESDATA_L1);
alter table MES.ZZZZZ move lob(XLSBODY) store as(tablespace MESDATA_L1);alter index MES.PK_ZZZZZ rebuild;
DROP tablespace MESDATA_L1_TEMP INCLUDING CONTENTS AND DATAFILES;
后续
如果数据为归档模式的话,移动表空间时会产生归档日志,注意处理一下。
<数据量比较小的话,有些用户会发现datafile是变小了,但是系统空间未变小,是因为有归档产生,则可以通过清理归档来释放>
某些情况下,有index失效的话,通过下面的SQL查询出失效的索引进行rebuild
select index_name,'alter index '|| index_name ||' rebuild;' as to_do_script from user_indexes where status <> 'VALID';
如果查询出来的SQL中有BIN$开头的对象,可先使用如下指令清空回收站后再处理。
purge recyclebin ;
数据库中不能使用LONG类型,否则会报错;这些表可以先通过删表重建的方式手动处理
ORA-00997: 非法使用 LONG 数据类型
边栏推荐
猜你喜欢
随机推荐
ZEMAX | 绘图分辨率结果对光线追迹的影响
使用JSP实现简单的登录注册功能,并且使用Session跟踪用户登录信息
802.1AS的BMCA(最佳主时钟选举)理解
2. What is the difference between Exception and Error?
次世代建模到底需要哪些美术基础
域名怎么管理,域名管理注意事项有哪些?
SSL证书验证怎么做?有几种方式?
ZEMAX | 如何使用渐晕系数
servlet learning (7) ServletContext
【面试准备】游戏开发中的几个岗位分别做什么&考察侧重点
servlet学习(七)ServletContext
使用Blender和ZBrush制作武器模型
【C语言】输出100~200之间的素数/质数(3种方法)
MATLAB给多组条形图添加误差棒
SSL证书过期后怎么办?
【随笔】平常心
数组与字符串11-反转字符串
ZEMAX | 如何围绕空间中的任何点旋转任何元素
树——前序
【3D建模制作技巧分享】ZBrush快捷键如何设置