当前位置:网站首页>Complete solution instance of Oracle shrink table space
Complete solution instance of Oracle shrink table space
2022-07-05 17:21:00 【1024 questions】
remarks :
One . demand
Two . Solution
2.1 Clean up expired data
2.2 Shrink the table space
2.3 Clean up table fragments
2.4 Directly put the relevant table drop fall
2.5 Move other tables under this table space out of this table space
summary
remarks :Oracle 11.2.0.4
One . demandThere is a log library recently , It takes up a lot of space , Need to clean up history , Then shrink the space .
As shown in the figure below ,4T We've almost run out of space .

The first thought is to clean up the data for more than half a year , then resize Table space .
2.1 Clean up expired dataBecause the table of business is tablename_yearmonth Format , for example log_202204, A watch every month , So go straight to truncate that will do .
Large table found :
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 The big table :
select 'truncate table '|| t.TABLE_NAME ||';' from user_tables t where t.TABLE_NAME like 'LOG%';2.2 Shrink the table space 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
Take the next step alter datafile Copy the statement and execute :
Some report errors :
ORA-03297: file contains used data beyond requested RESIZE value

Because I use truncate, Theoretically, it will not be affected by high water level , I found several blogs on the Internet , It also means to lower the high water level of the meter , Clean up table fragments .
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%';After cleaning up the debris , Re execution , Is still an error .
2.4 Directly put the relevant table drop fallselect 'drop table '|| t.TABLE_NAME ||'purge;' from user_tables t where t.TABLE_NAME like 'LOG%';drop After dropping the meter , Re execution , Is still an error .
2.5 Move other tables under this table space out of this table spaceOmnipotent itpub There is a blog on :
Truncate table perhaps drop table Shrink data files , I often meet ORA-03297: file contains used data beyond requested RESIZE value Inquire about dba_free_space There is also free space . After inquiry MOS(Doc ID 1029252.6) hear
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.
It means if you are free extent If it's in the middle of the file , Cannot proceed at this time resize , The tail must be object drop Then rebuild Again resize datafile. The following is my test ;
[[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 Is a continuous
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 23Have original tab1 Of 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 valueUnable to proceed resize
The following tab1 drop Retest
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 valueStill wrong
then truncate tab2 Retest
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 valueAt this time, it can only shrink tab2 Space But not shrink tab1 Space
And then again 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.Can shrink tab1 Space
note:
Shrinking data files is related to two factors
1 Lower the high water level
2 free extent stay datafile Tail of
This article directly explains the second
If you are free extent If it's in the middle of the file , Cannot proceed at this time resize , The tail must be object drop Then rebuild Again resize datafile.
That is to say, the user writes other tables in the same period , Also under this data file , Then you can't resize.
Move other tables to users Table space :
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_%';Run compressed space again , success

2.6 View compressed space
You can see that there are more 2.1T Space

Shrinking space, running speed is good ,50 Multiple data files , Compress in a few minutes .
summaryThis is about Oracle This is the end of the article on shrinking table space , More about Oracle Please search the previous articles of SDN or continue to browse the relevant articles below. I hope you will support SDN more in the future !
边栏推荐
- 【Web攻防】WAF检测技术图谱
- Thoughtworks 全球CTO:按需求构建架构,过度工程只会“劳民伤财”
- CMake教程Step5(添加系统自检)
- Judge whether a number is a prime number (prime number)
- Error in composer installation: no composer lock file present.
- 通过proc接口调试内核代码
- [first lecture on robot coordinate system]
- The second day of learning C language for Asian people
- Deeply cultivate 5g, and smart core continues to promote 5g applications
- It is forbidden to copy content JS code on the website page
猜你喜欢
随机推荐
thinkphp3.2.3
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
Use of ThinkPHP template
SQL删除重复数据的实例教程
ClickHouse(03)ClickHouse怎么安装和部署
About JSON parsing function JSON in MySQL_ EXTRACT
What is ROM
Matery主题自定义(一)黑夜模式
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
C (WinForm) the current thread is not in a single threaded unit, so ActiveX controls cannot be instantiated
The first lesson of EasyX learning
What else do you not know about new map()
goto Statement
33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
ECU简介
【剑指 Offer】62. 圆圈中最后剩下的数字
IDC报告:腾讯云数据库稳居关系型数据库市场TOP 2!
张平安:加快云上数字创新,共建产业智慧生态
C how TCP restricts the access traffic of a single client
Error in composer installation: no composer lock file present.



![[first lecture on robot coordinate system]](/img/3c/af056f0fe68b3244a3dc491ceb291d.png)

