Temporary tablespace cleaning in Oracle and Oracle tablespace cleaning
Author: iamlaosong
Oracle temporary tablespace is mainly used to query and store some buffer data. Temporary tablespace consumption is mainly caused by sorting the intermediate query results. The main functions of temporary tablespace:
Index create or rebuild
Order by or group
Distinct operation
Union, intersect, or minus
Sort-merge joins
Analyze
Restarting the database can release temporary tablespace. If the instance cannot be restarted, the temp tablespace will continue to grow as the problematic SQL statement is executed. Even if the temporary tablespace is rebuilt, the usage of the temporary tablespace reaches 99% after a period of time, and then the tablespace begins to grow until the hard disk space is exhausted. As shown in, the temporary tablespace is 8 GB at the time of creation and is now 32 GB:
To prevent the unlimited increase of the temporary tablespace, I used the method of recreating the temporary tablespace after a period of time. For convenience, I kept two sets of statements and executed them in turn, assume that the temporary tablespace name is temp. Create a New tempa tablespace and delete the temp tablespace as follows:
Create temporary tablespace tempa tempfile '/opt/app/oracle/oradata/orcl/tempa01.dbf' SIZE 8192 m reuse autoextend on next 1024 k maxsize unlimited; -- create temporary tablespace for transit
Alter database default temporary tablespace tempa; -- change the default temporary tablespace
Drop tablespace temp including contents and datafiles; -- delete the original temporary tablespace
Shows the new temporary tablespace:
After a while, when the temporary tablespace increases to a certain extent, create a new temp tablespace and delete the tempa tablespace, that is:
Create temporary tablespace temp tempfile '/opt/app/oracle/oradata/orcl/temp01.dbf' SIZE 8192 m reuse autoextend on next 1024 k maxsize unlimited; -- create temporary tablespace for transit
Alter database default temporary tablespace temp; -- change the default temporary tablespace
Drop tablespace tempa including contents and datafiles; -- delete the original temporary tablespace
This ensures that the temporary tablespace is not too large and prevents excessive disk space occupation.
========================================================== ==================
The following SQL statement can be used to view the space used by the current temporary tablespace and the occupied space:
Select sess. SID, segtype, blocks * 8/1000 "MB", SQL _text
From v $ sort_usage sort, v $ session sess, v $ SQL
Where sort. SESSION_ADDR = sess. SADDR
And SQL. ADDRESS = sess. SQL _ADDRESS
Order by blocks desc;
The following statement queries the idle degree of the temporary tablespace:
Select 'the '| name | 'temp tablespace' | tablespace_name |
'Idle' |
Round (100-(s. tot_used_blocks/s. total_blocks) * 100, 3) |
'% At' | to_char (sysdate, 'yyyymmddhh24mis ')
From (select d. tablespace_name,
Nvl (sum (used_blocks), 0) tot_used_blocks,
Sum (blocks) total_blocks
From v $ sort_segment v, dba_temp_files d
Where d. tablespace_name = v. tablespace_name (+)
Group by d. tablespace_name) s,
V $ database;