删除或重建Oracle 临时表空间(官方文档)

How to resize (or recreate) the TEMP tablespace ?( Version 8.1.5.0 to 11.2.0.0)

 

SOLUTION

1) Overview

2) Dropping / Recreating Temporary Tablespace Method

3) Drop Tempfile Command Method - (Oracle9i and higher)

 

Overview

 

In many database configurations, the DBA will choose to allow their temporary tablespace (actually the tempfile(s) for the temporary tablespace) to autoextend.

A runaway query or sort can easily chew up valuable space on the disk as the tempfiles(s) extends to accommodate the request for space.

If the increase in size of the temporary tablespace (the tempfiles) gets exceedingly large because of a particular anomaly, the DBA will often want to resize the temporary tablespace to a more reasonable size in order to reclaim that extra space. The obvious action would be to resize the tempfiles using the following statement:

 

SQL> alter database tempfile '/u02/oradata/TESTDB/temp01.dbf' resize 250M;

alter database tempfile '/u02/oradata/TESTDB/temp01.dbf' resize 250M

*

ERROR at line 1:

ORA-03297: file contains used data beyond requested RESIZE value

 

You then restart the database and attempt the same statement only to be greeted with the same error. Several methods exist to reclaim the used space used for a larger than normal temporary tablespace depending on which release of Oracle you are running. The method that exists for all releases of Oracle is to simply drop and recreate the temporary tablespace back to its original (or another reasonable) size. If you are using Oracle9i or higher, you can apply another method which is to drop the large tempfile (which will drop the tempfile from the data dictionary AND the O/S file system) using the alter database tempfile '<tempfile name>' drop including datafiles; command. Each method is explained below.

 

Dropping / Recreating Temporary Tablespace Method

 

SQL> CREATE TEMPORARY TABLESPACE temp2

2 TEMPFILE '/u02/oradata/TESTDB/temp2_01.dbf' SIZE 5M REUSE

3 AUTOEXTEND ON NEXT 1M MAXSIZE unlimited

4 EXTENT MANAGEMENT LOCAL UNIFORM SIZE 1M;

Tablespace created.

 

SQL> ALTER DATABASE DEFAULT TEMPORARY TABLESPACE temp2;

Database altered.

 

SQL> DROP TABLESPACE temp INCLUDING CONTENTS AND DATAFILES;

Tablespace dropped.

 

SQL> CREATE TEMPORARY TABLESPACE temp

2 TEMPFILE '/u02/oradata/TESTDB/temp01.dbf' SIZE 500M REUSE

3 AUTOEXTEND ON NEXT 100M MAXSIZE unlimited

4 EXTENT MANAGEMENT LOCAL UNIFORM SIZE 1M;

Tablespace created.

 

SQL> ALTER DATABASE DEFAULT TEMPORARY TABLESPACE temp;

Database altered.

 

SQL> DROP TABLESPACE temp2 INCLUDING CONTENTS AND DATAFILES;

Tablespace dropped.

 

Drop Tempfile Command Method - (Oracle9i and higher):

 

If you are using Oracle9i or higher, another method exists that allows you to simply drop a tempfile. As with the above method, this should be performed during off hours with no users logged on performing work. The first step is to obtain the name of the tempfile to drop. For this example, my temporary tablespace name is TEMP and the name of the tempfile is /u02/oradata/TESTDB/temp2_01.dbf:

 

SQL> SELECT tablespace_name, file_name, bytes FROM dba_temp_files WHERE tablespace_name = 'TEMP';

TABLESPACE_NAME FILE_NAME BYTES

----------------- -------------------------------- --------------

TEMP /u02/oradata/TESTDB/temp01.dbf 13,107,200,000

 

The normal size of my temporary tablespace is 512MB which has always been more than adequate for this environment. A bad query, however, increased the size of this tablespace (my tempfile) to over 13GB and I would like to reclaim that space. In the example below, I simply drop and recreate the tempfile:

 

SQL> ALTER DATABASE TEMPFILE '/u02/oradata/TESTDB/temp01.dbf' DROP INCLUDING DATAFILES;

Database altered.

 

SQL> ALTER TABLESPACE temp ADD TEMPFILE '/u02/oradata/TESTDB/temp01.dbf' SIZE 512m

2 AUTOEXTEND ON NEXT 250m MAXSIZE UNLIMITED;

Tablespace altered.

 

On some platforms (i.e. Windows 2000), it is possible for the tempfile to be deleted from DBA_TEMP_FILES but not from the hard drive of the server.

If this occurs, simply delete the file using regular O/S commands.

 

SQL> SELECT tablespace_name, file_name, bytes

2       FROM dba_temp_files WHERE tablespace_name = 'TEMP';

TABLESPACE_NAME FILE_NAME BYTES

----------------- -------------------------------- --------------

TEMP /u02/oradata/TESTDB/temp01.dbf 536,870,912

 

If users are currently accessing the tempfile that you are attempting to drop, you may receive the following error:

 

SQL> ALTER DATABASE TEMPFILE '/u02/oradata/TESTDB/temp01.dbf' DROP INCLUDING DATAFILES;

ALTER DATABASE TEMPFILE '/u02/oradata/TESTDB/temp01.dbf' DROP INCLUDING DATAFILES

*

ERROR at line 1:

ORA-25152: TEMPFILE cannot be dropped at this time

 

As for the users who were using the tempfile, their transaction will end and will be greeted with the following error message:

 

SQL> @testTemp.sql

join dba_extents c on (b.segment_name = c.segment_name)

*

ERROR at line 4:

ORA-00372: file 601 cannot be modified at this time

ORA-01110: data file 601: '/u02/oradata/TESTDB/temp01.dbf'

ORA-00372: file 601 cannot be modified at this time

ORA-01110: data file 601: '/u02/oradata/TESTDB/temp01.dbf'

If this happens, you should attempt to drop the tempfile again so the operation is successful:

 

SQL> ALTER DATABASE TEMPFILE '/u02/oradata/TESTDB/temp01.dbf' DROP INCLUDING DATAFILES;

Database altered.

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值