当前位置:网站首页>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 128
50 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 23
Have 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 value
Unable 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 value
Still 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 value
At 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 !
边栏推荐
- Embedded-c Language-3
- What is ROM
- Matery主题自定义(一)黑夜模式
- 33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)
- 浏览器渲染原理以及重排与重绘
- Application of threshold homomorphic encryption in privacy Computing: Interpretation
- C (WinForm) the current thread is not in a single threaded unit, so ActiveX controls cannot be instantiated
- Browser rendering principle and rearrangement and redrawing
- CMake教程Step6(添加自定义命令和生成文件)
- Learn about MySQL transaction isolation level
猜你喜欢
【Web攻防】WAF检测技术图谱
Three traversal methods of binary tree
Embedded -arm (bare board development) -2
Embedded UC (UNIX System Advanced Programming) -1
Embedded UC (UNIX System Advanced Programming) -3
Use of ThinkPHP template
Learn about MySQL transaction isolation level
WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
【性能测试】jmeter+Grafana+influxdb部署实战
Deeply cultivate 5g, and smart core continues to promote 5g applications
随机推荐
MySQL queries the latest qualified data rows
Application of threshold homomorphic encryption in privacy Computing: Interpretation
33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)
composer安装报错:No composer.lock file present.
How to write a full score project document | acquisition technology
C language to get program running time
Matery主题自定义(一)黑夜模式
Embedded-c Language-5
【beanshell】数据写入本地多种方法
Embedded UC (UNIX System Advanced Programming) -1
Example tutorial of SQL deduplication
flask解决CORS ERR 问题
浏览器渲染原理以及重排与重绘
mysql5.6解析JSON字符串方式(支持复杂的嵌套格式)
调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
[first lecture on robot coordinate system]
Embedded UC (UNIX System Advanced Programming) -3
ClickHouse(03)ClickHouse怎么安装和部署
CMake教程Step5(添加系统自检)
First day of learning C language