当前位置:网站首页> Oracle缩表空间的完整解决实例
Oracle缩表空间的完整解决实例
2022-07-05 16:43:00 【1024问】
备注:
一. 需求
二. 解决方案
2.1 清理过期数据
2.2 收缩表空间
2.3 清理表碎片
2.4 直接把相关的表drop掉
2.5 把该表空间下其它的表移出此表空间
总结
备注:Oracle 11.2.0.4
一. 需求近期有一个日志库,占用了比较多的空间,需要将历史的清理,然后收缩空间。
如下图所示,4T的空间已经差不多用完。

首先想到的是清理掉超过半年的数据,然后resize 表空间。
2.1 清理过期数据因为业务的表是 tablename_yearmonth格式,例如 log_202204,每个月一个表,所以直接进行truncate即可。
找到大表:
select t.segment_name,t.BYTES/1024/1024/1024 GB,t.segment_typefrom user_segments twhere t.segment_type in ('TABLE','TABLE PARTITION')order by nvl(t.BYTES/1024/1024/1024,0) desc;
truncate 大表:
select 'truncate table '|| t.TABLE_NAME ||';' from user_tables t where t.TABLE_NAME like 'LOG%';2.2 收缩表空间select a.tablespace_name,a.file_name,a.totalsize as totalsize_MB,b.freesize as freesize_MB,'ALTER DATABASE DATAFILE ''' || a.file_name || ''' RESIZE ' ||round((a.totalsize - b.freesize) + 200) || 'M;' as "alter datafile"from (select a.file_name,a.file_id,a.tablespace_name,a.bytes / 1024 / 1024 as totalsizefrom dba_data_files a) a,(select b.tablespace_name,b.file_id,sum(b.bytes / 1024 / 1024) as freesizefrom dba_free_space bgroup by b.tablespace_name, b.file_id) bwhere a.file_id = b.file_idand b.freesize > 100and a.tablespace_name in ('TBS_LOG_DATA')order by a.tablespace_name
将上一步的 alter datafile语句拷贝出来执行:
有部分报错:
ORA-03297: file contains used data beyond requested RESIZE value

因为我使用的是truncate,理论上不会受高水位的影响,在网上找了几个博客,也是说要降低表的高水位,清理表碎片。
select 'alter table '||t.TABLE_NAME||' enable row movement;', 'alter table '||t.TABLE_NAME||' shrink space cascade;' from user_tables t where t.TABLE_NAME like 'LOG%';清理完碎片之后,重新执行,依旧报错。
2.4 直接把相关的表drop掉select 'drop table '|| t.TABLE_NAME ||'purge;' from user_tables t where t.TABLE_NAME like 'LOG%';drop掉表之后,重新执行,依旧报错。
2.5 把该表空间下其它的表移出此表空间万能的itpub上有个博客:
Truncate table 或者 drop table 收缩数据文件,经常遇到ORA-03297: file contains used data beyond requested RESIZE value 查询dba_free_space 也有空闲空间。经过查询MOS(Doc ID 1029252.6)得知
If you have a large extent in the middle of a datafile, and some object taking up room at the end of the datafile, you can use the query FINDEXT.SQL below to find this object. If you export this object, then drop it, you should then free up contiguous space at the end of your datafile so you will be able to resize it smaller.
Make sure you leave enough room in the datafile for importing the object back into the tablespace.
意思是说如果空闲的extent如果在文件的中间,此时无法进行resize ,必须把尾部的object drop 然后重建 再resize datafile。以下是本人做的测试;
[[email protected] ~]$ sqlplus / as sysdbaSQL*Plus: Release 10.2.0.1.0 - Production on Wed Jul 31 11:10:41 2013Copyright (c) 1982, 2005, Oracle. All rights reserved.Connected to:Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit ProductionWith the Partitioning, OLAP and Data Mining optionsSQL> create tablespace test2 datafile '/u01/app/oracle/oradata/orcl/test2.dbf' size 10M autoextend on next 1M;Tablespace created.SQL> create table tab1 tablespace test2 as select * from dba_objects;Table created.SQL> select file#,name,bytes/1024/1024 bytes from v$datafile where name like '%test2%';FILE# NAME BYTES----- ------------------------------------------------------------ ----- 23 /u01/app/oracle/oradata/orcl/test2.dbf 11SQL> create table tab2 tablespace test2 as select * from dba_objects;Table created.SQL> select file#,name,bytes/1024/1024 bytes from v$datafile where name like '%test2%';FILE# NAME BYTES----- ------------------------------------------------------------ ----- 23 /u01/app/oracle/oradata/orcl/test2.dbf 21SQL> select SEGMENT_NAME,FILE_ID,EXTENT_ID,BLOCK_ID,blocks from dba_extents where file_id=23 order by BLOCK_ID;SEGMENT_NA FILE_ID EXTENT_ID BLOCK_ID BLOCKS---------- ---------- ---------- ---------- ----------TAB1 23 0 9 8TAB1 23 1 17 8TAB1 23 2 25 8TAB1 23 3 33 8TAB1 23 4 41 8TAB1 23 5 49 8TAB1 23 6 57 8TAB1 23 7 65 8TAB1 23 8 73 8TAB1 23 9 81 8TAB1 23 10 89 8SEGMENT_NA FILE_ID EXTENT_ID BLOCK_ID BLOCKS---------- ---------- ---------- ---------- ----------TAB1 23 11 97 8TAB1 23 12 105 8TAB1 23 13 113 8TAB1 23 14 121 8TAB1 23 15 129 8TAB1 23 16 137 128TAB1 23 17 265 128TAB1 23 18 393 128TAB1 23 19 521 128TAB1 23 20 649 128TAB1 23 21 777 128SEGMENT_NA FILE_ID EXTENT_ID BLOCK_ID BLOCKS---------- ---------- ---------- ---------- ----------TAB1 23 22 905 128TAB1 23 23 1033 128TAB1 23 24 1161 128TAB2 23 0 1289 8TAB2 23 1 1297 8TAB2 23 2 1305 8TAB2 23 3 1313 8TAB2 23 4 1321 8TAB2 23 5 1329 8TAB2 23 6 1337 8TAB2 23 7 1345 8SEGMENT_NA FILE_ID EXTENT_ID BLOCK_ID BLOCKS---------- ---------- ---------- ---------- ----------TAB2 23 8 1353 8TAB2 23 9 1361 8TAB2 23 10 1369 8TAB2 23 11 1377 8TAB2 23 12 1385 8TAB2 23 13 1393 8TAB2 23 14 1401 8TAB2 23 15 1409 8TAB2 23 16 1417 128TAB2 23 17 1545 128TAB2 23 18 1673 128SEGMENT_NA FILE_ID EXTENT_ID BLOCK_ID BLOCKS---------- ---------- ---------- ---------- ----------TAB2 23 19 1801 128TAB2 23 20 1929 128TAB2 23 21 2057 128TAB2 23 22 2185 128TAB2 23 23 2313 128TAB2 23 24 2441 12850 rows selected.
Block_id 是连续的
SQL> truncate table tab1 2 ;Table truncated.SQL> select * from dba_free_space where file_id=23;TABLESPACE_NAME FILE_ID BLOCK_ID BYTES BLOCKS RELATIVE_FNO-------------------- ---------- ---------- ---------- ---------- ------------TEST2 23 17 ########## 1272 23TEST2 23 2569 ########## 120 23有原来tab1 的free blocks 1272
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/test2.dbf' resize 12M;alter database datafile '/u01/app/oracle/oradata/orcl/test2.dbf' resize 12M*ERROR at line 1:ORA-03297: file contains used data beyond requested RESIZE value无法进行resize
下面把tab1 drop 再测试
SQL> drop table tab1 purge;Table dropped.SQL> alter database datafile '/u01/app/oracle/oradata/orcl/test2.dbf' resize 12M;alter database datafile '/u01/app/oracle/oradata/orcl/test2.dbf' resize 12M*ERROR at line 1:ORA-03297: file contains used data beyond requested RESIZE value依然报错
然后truncate tab2 再进行测试
SQL> truncate table tab2;Table truncated.SQL> select * from dba_free_space where file_id=23;TABLESPACE_NAME FILE_ID BLOCK_ID BYTES BLOCKS RELATIVE_FNO-------------------- ---------- ---------- ---------- ---------- ------------TEST2 23 9 ########## 1280 23TEST2 23 1297 ########## 1392 23SQL> alter database datafile '/u01/app/oracle/oradata/orcl/test2.dbf' resize 12M;Database altered.SQL> alter database datafile '/u01/app/oracle/oradata/orcl/test2.dbf' resize 6M;alter database datafile '/u01/app/oracle/oradata/orcl/test2.dbf' resize 6M*ERROR at line 1:ORA-03297: file contains used data beyond requested RESIZE value此时只能收缩 tab2 的空间 但是不能收缩 tab1的空间
然后再drop tab2
SQL> drop table tab2 purge 2 ;Table dropped.SQL> alter database datafile '/u01/app/oracle/oradata/orcl/test2.dbf' resize 6M;Database altered.SQL> alter database datafile '/u01/app/oracle/oradata/orcl/test2.dbf' resize 1M;Database altered.可以收缩tab1的空间
note:
收缩数据文件和两个因素有关
1 降低高水位
2 free extent在datafile 的尾部
本篇文章直接解释了第二个
如果空闲的extent如果在文件的中间,此时无法进行resize ,必须把尾部的object drop 然后重建 再resize datafile。
也就是说同时期该用户下其它表的写入,也在这个数据文件下,那么就不能进行resize。
把其它表移动到users表空间:
select 'alter index '||index_NAME||' rebuild tablespace users;' from user_indexes where TABLE_NAME not like 'LOG_%';select 'alter table '||TABLE_NAME||' move tablespace users;' from user_tables where TABLE_NAME not like 'LOG_%';再次运行压缩空间,成功

2.6 查看压缩的空间
可以看到一下子多出了2.1T 的空间

收缩空间运行速度还不错,50多个数据文件,几分钟就压缩完成。
总结到此这篇关于Oracle缩表空间的文章就介绍到这了,更多相关Oracle缩表空间内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- 33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)
- 【7.7直播预告】《SaaS云原生应用典型架构》大咖讲师教你轻松构建云原生SaaS化应用,难题一一击破,更有华为周边好礼等你领!
- 叩富网开期货账户安全可靠吗?怎么分辨平台是否安全?
- Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
- Use JDBC technology and MySQL database management system to realize the function of course management, including adding, modifying, querying and deleting course information.
- Use of ThinkPHP template
- 外盘期货平台如何辨别正规安全?
- WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
- Is it safe for qiniu business school to open a stock account? Is it reliable?
- [7.7 live broadcast preview] the lecturer of "typical architecture of SaaS cloud native applications" teaches you to easily build cloud native SaaS applications. Once the problem is solved, Huawei's s
猜你喜欢

Using C language to realize palindrome number

机器学习01:绪论

WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响

Embedded -arm (bare board development) -1

高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积

The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%

【729. 我的日程安排錶 I】
SQL删除重复数据的实例教程

High number | summary of calculation methods of volume of rotating body, double integral calculation of volume of rotating body

Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
随机推荐
IDC报告:腾讯云数据库稳居关系型数据库市场TOP 2!
网站页面禁止复制内容 JS代码
Application of threshold homomorphic encryption in privacy Computing: Interpretation
The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
Embedded -arm (bare board development) -1
时间戳strtotime前一天或后一天的日期
浏览器渲染原理以及重排与重绘
WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
通过proc接口调试内核代码
33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)
项目引入jar从私服Nexus 拉去遇到的一个问题
Learnopongl notes (II) - Lighting
ECU introduction
云安全日报220705:红帽PHP解释器发现执行任意代码漏洞,需要尽快升级
Function sub file writing
【testlink】TestLink1.9.18常见问题解决方法
拷贝方式之DMA
[729. My schedule I]
一文了解Go语言中的函数与方法的用法
It is forbidden to copy content JS code on the website page