oracle数据文件收缩,Oracle数据文件收缩

随着数据库的使用,数据文件越来越大,有没有办法将标记为free的block挤掉

相关表:

DBA_DATA_FILES

DBA_DATA_FILES describes database files.

Column

Datatype

NULL

Description

FILE_NAME

VARCHAR2(513)

Name of the database file

FILE_ID

NUMBER

NOT NULL

File identifier number of the database file

TABLESPACE_NAME

VARCHAR2(30)

NOT NULL

Name of the tablespace to which the file belongs

BYTES

NUMBER

Size of the file in bytes

BLOCKS

NUMBER

NOT NULL

Size of the file in Oracle blocks

STATUS

VARCHAR2(9)

File status: AVAILABLE or INVALID (INVALID means that the file number is not in use, for example, a file in a tablespace that was dropped)

RELATIVE_FNO

NUMBER

Relative file number

AUTOEXTENSIBLE

VARCHAR2(3)

Autoextensible indicator

MAXBYTES

NUMBER

Maximum file size in bytes

MAXBLOCKS

NUMBER

Maximum file size in blocks

INCREMENT_BY

NUMBER

Number of Oracle blocks used as autoextension increment

USER_BYTES

NUMBER

The size of the file available for user data. The actual size of the file minus the USER_BYTES value is used to store file related metadata.

USER_BLOCKS

NUMBER

Number of blocks which can be used by the data

ONLINE_STATUS

VARCHAR2(7)

Online status of the file:

SYSOFF

SYSTEM

OFFLINE

ONLINE

RECOVER

DBA_FREE_SPACE

DBA_FREE_SPACE describes the free extents in all tablespaces in the database.

Note that if a datafile (or entire tablespace) is offline in a locally managed tablespace, you will not see any extent information. If an object has extents in an online file of the tablespace, you will see extent information about the offline datafile. However, if the object is entirely in the offline file, a query of this view will not return any records.

Related View

USER_FREE_SPACE describes the free extents in the tablespaces accessible to the current user.

Column

Datatype

NULL

Description

TABLESPACE_NAME

VARCHAR2(30)

Name of the tablespace containing the extent

FILE_ID

NUMBER

File identifier number of the file containing the extent

BLOCK_ID

NUMBER

Starting block number of the extent

BYTES

NUMBER

Size of the extent (in bytes) --free bytes

BLOCKS

NUMBER

Size of the extent (in Oracle blocks)  --free block

RELATIVE_FNO

NUMBER

Relative file number of the file containing the extent

DBA_EXTENTS

DBA_EXTENTS describes the extents comprising the segments in all tablespaces in the database.

Note that if a datafile (or entire tablespace) is offline in a locally managed tablespace, you will not see any extent information. If an object has extents in an online file of the tablespace, you will see extent information about the offline datafile. However, if the object is entirely in the offline file, a query of this view will not return any records.

Related View

USER_EXTENTS describes the extents comprising the segments owned by the current user's objects. This view does not display the OWNER, FILE_ID, BLOCK_ID, orRELATIVE_FNO columns.

Column

Datatype

NULL

Description

OWNER

VARCHAR2(30)

Owner of the segment associated with the extent

SEGMENT_NAME

VARCHAR2(81)

Name of the segment associated with the extent

PARTITION_NAME

VARCHAR2(30)

Object Partition Name (Set to NULL for non-partitioned objects)

SEGMENT_TYPE

VARCHAR2(18)

Type of the segment: INDEX PARTITION, TABLE PARTITION

TABLESPACE_NAME

VARCHAR2(30)

Name of the tablespace containing the extent

EXTENT_ID

NUMBER

Extent number in the segment

FILE_ID

NUMBER

File identifier number of the file containing the extent

BLOCK_ID

NUMBER

Starting block number of the extent

BYTES

NUMBER

Size of the extent in bytes

BLOCKS

NUMBER

Size of the extent in Oracle blocks

RELATIVE_FNO

NUMBER

Relative file number of the first extent block

SELECT 'alter database datafile '

||chr(39)

||file_name

||chr(39)

|| ' resize '

|| ceil(hwmsize*1.2)

||'M;'

FROM

(SELECT   a.file_id,

a.file_name,

a.filesize,

b.freesize,

(a.filesize-b.freesize) usedsize,

c.hwmsize,

c.hwmsize - (a.filesize-b.freesize) unsedsize_belowhwm,

a.filesize - c.hwmsize canshrinksize

FROM   (   select file_id,file_name,ROUND(bytes/1024/1024) filesize

FROM dba_data_files   ) a,

(   select file_id,ROUND(SUM(dfs.bytes)/1024/1024) freesize

FROM dba_free_space dfs   group BY file_id   ) b,

(   select file_id,ROUND(MAX(block_id)*8/1024) HWMsize

FROM dba_extents   group BY file_id) c   where a.file_id = b.file_id   and a.file_id = c.file_id   order BY unsedsize_belowhwm DESC

)

alter database datafile 'F:\ORACLE\ORADATA\PURPLE\USERS_NEW' resize 5755M;

alter database datafile 'F:\ORACLE\ORADATA\PURPLE\SYSTEM01.DBF' resize 4574M;

alter database datafile 'F:\ORACLE\ORADATA\PURPLE\INDEX_NEW' resize 825M;

alter database datafile 'F:\ORACLE\ORADATA\PURPLE\USERS01.DBF' resize 17980M;

alter database datafile 'F:\ORACLE\ORADATA\PURPLE\SYSAUX01.DBF' resize 670M;

alter database datafile 'F:\ORACLE\ORADATA\PURPLE\EXAMPLE01.DBF' resize 90M;

alter database datafile 'F:\ORACLE\ORADATA\PURPLE\UNDOTBS01.DBF' resize 72M; 总共释放出7G的空间出来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 当数据库表中的数据被删除时,所占用的存储空间并不会立即释放,只有当表空间中的数据块被再次使用时才会释放。 要释放表空间中的空闲空间,可以使用以下几种方法之一: 1. 使用 `SHRINK SPACE` 语句: ``` SHRINK SPACE [compact] tablename; ``` 这将释放表中的空闲空间,并将数据块聚集在一起,使得可以重新分配给其他对象。 2. 使用 `TRUNCATE` 语句: ``` TRUNCATE TABLE tablename; ``` 这将清空表中的所有数据,并释放表空间中的所有空闲空间。 3. 使用 `DROP` 和 `CREATE` 语句: ``` DROP TABLE tablename; CREATE TABLE tablename AS SELECT * FROM old_tablename WHERE 1=0; ``` 这将删除表并创建一个新表,新表中不包含任何数据。这样就可以释放表空间中的所有空闲空间。 4. 使用 `DBMS_SPACE.SPACE_USAGE` 包的 `DROP_UNUSED_SPACE` 函数: ``` EXECUTE DBMS_SPACE.DROP_UNUSED_SPACE( table_name => 'tablename', estimate_percent => 50 ); ``` 这将释放表空间中大约 50% 的空闲空间。 需要注意的是,上述方法均需要足够的磁盘空间来存储重新排列的数据。 ### 回答2: Oracle数据库中释放空间通常有以下几种方法: 1. 删除数据:可以使用DELETE或者TRUNCATE语句删除不再需要的数据。DELETE语句会将数据标记为已删除,但不会立即释放空间;而TRUNCATE语句会立即释放所有空间。 2. 清理回滚段:当事务回滚或者数据库发生异常时,会产生大量的回滚数据,占用大量的空间。可以使用ROLLBACK语句回滚事务或者手动清理回滚段来释放空间。 3. 重新构建表:通过执行ALTER TABLE语句,可以对表进行重建,从而释放空间。重建表会创建一个新的表,在新表上重建索引、约束等,然后将源表数据导入到新表中,最后删除原表。 4. 压缩表:Oracle提供了多种表压缩技术,如基于列存储的压缩、基于行存储的压缩等。可以通过执行ALTER TABLE语句来启用表压缩,从而减小表的存储空间。 5. 重建索引:索引在数据库中占据一定空间,可以通过DROP和CREATE语句来删除索引,然后重新创建索引,从而释放空间。 6. 数据库表空间管理:可以通过调整表空间的大小来释放空间。可以使用ALTER TABLESPACE语句来扩大、缩小表空间的大小。 需要注意的是,释放空间操作可能对数据库的性能产生一定的影响,因此在进行这些操作之前,应该先进行相关的性能测试,并在非高负载时段进行操作,以避免影响到正常的数据库运行。另外,释放空间之前应该做好数据备份工作,以防数据丢失。 ### 回答3: Oracle数据库中如何释放空间取决于具体的情况,主要有以下几种方法: 1. 通过删除数据:使用DELETE或TRUNCATE语句删除不再需要的数据。DELETE语句删除记录但仍保留空间,而TRUNCATE语句会将表空间重置为初始大小。删除后,可以通过执行ALTER TABLE语句收缩表空间,从而释放所占用的空间。 2. 通过重建索引:使用ALTER INDEX REBUILD语句重建索引,将索引的碎片和空间浪费去除,从而释放空间。在重建索引期间,Oracle会在新的表空间中创建一个新的索引,并将数据逐步移动到新索引中,当移动完成后,旧的索引将被删除,释放空间。 3. 通过压缩表:使用ALTER TABLE语句执行表压缩,将表中的数据压缩成更紧凑的格式,从而减小表的大小。压缩表操作需要占用较大的临时表空间,因此在执行之前需要确保临时表空间有足够的空间。 4. 通过重建表空间:如果表空间内有大量不再使用的空间,可以通过创建一个新的表空间,并将表和索引移动到新的表空间中来进行压缩。然后,删除旧的表空间,并回收其所占用的空间。 5. 通过重建数据库对象:如果以上方法都不能满足释放空间的需求,可以尝试重建数据库对象。通过创建一个新的数据库对象,然后将数据从旧对象中导入到新对象中,再删除旧对象,释放空间。 需要注意的是,在执行任何释放空间的操作之前,应该先备份数据库,以防止数据丢失。此外,根据具体的情况选择合适的方法,以确保数据的完整性和数据库的稳定性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值