简体   繁体   English

如何减少Oracle数据库存储?

[英]How can I reduce oracle database storage?

I have a problem with hard disk space lack. 我有硬盘空间不足的问题。 so I delete all records from a big table in my oracle database. 所以我从oracle数据库的一个大表中删除所有记录。 But my hard disk space is not changed. 但是我的硬盘空间没有改变。 How can I fix it? 我该如何解决?

Try ALTER TABLE table_name SHRINK SPACE CASCADE; 试试ALTER TABLE table_name SHRINK SPACE CASCADE; and rebuild your indexes. 并重建索引。

Deleting all rows from a table does not reset the high water mark and does not free up any space. 从表中删除所有行不会重置高水位线,也不会释放任何空间。 This is because with DML operations Oracle assumes the space will be re-used later and doesn't bother to release it. 这是因为通过DML操作,Oracle假定该空间将在以后重新使用,并且不会费心释放它。

You probably want to run a TRUNCATE statement instead. 您可能想改为运行TRUNCATE语句。 It's also much faster than a DELETE as it doesn't generate any REDO or UNDO. 它也比DELETE快得多,因为它不会生成任何REDO或UNDO。 But beware that truncating a table is a DDL statement that cannot be rolled back. 但是请注意,截断表是无法回滚的DDL语句。

TRUNCATE ing tables releases space to the tablespace and that space is available to other objects. TRUNCATE表将空间释放到表空间,而该空间可用于其他对象。 But it still doesn't reduce the amount of operating system space used. 但是它仍然不会减少所使用的操作系统空间。 Lowering disk space requires shrinking datafiles. 减少磁盘空间需要缩小数据文件。

Shrinking datafiles can be tricky. 缩小数据文件可能很棘手。 Datafiles can only be shrunk to the last extent. 数据文件只能缩小到最后程度。 It's possible for a datafile to have a ton of empty space but not be shrinkable, if a single block of data is at the end of the file. 如果单个数据块位于文件末尾,则数据文件可能有大量的空白空间,但不可收缩。 In that case the datafile must be defragmented. 在这种情况下,必须对数据文件进行碎片整理。 That can be done by Oracle Enterprise Manager and you can find many other scripts to do it. 这可以由Oracle企业管理器完成,您可以找到许多其他脚本来完成。 They usually involve moving or rebuilding all the objects. 它们通常涉及移动或重建所有对象。

The simplest way to save some space is to try to shrink every file. 节省空间的最简单方法是尝试缩小每个文件。 This won't free up all available space, but in practice most of the free space is is usually after the last extent and can be reclaimed. 这不会释放所有可用空间,但是实际上,大多数可用空间通常是在最后一个扩展区之后,并且可以回收。

Run the below script, it will generate a bunch of alter statements to try shrink the datafiles by a large amount. 运行以下脚本,它将生成一堆alter语句,以尝试大量缩小数据文件。 Most of the statements will fail but that's OK. 大多数语句将失败,但是可以。 Cut the number in half and try again, repeatedly. 将数字减半,然后重试。 This is a "dumb" way to do it, but it can usually free up some space quickly. 这是一种“愚蠢”的方法,但是通常可以快速释放一些空间。

with decrease_size as (select 64 gb from dual)
select 'alter database datafile '''||file_name||''' resize '||to_char(round(bytes/1024/1024/1024)-(select gb from decrease_size))||'g;'
from dba_data_files
where bytes > (select gb from decrease_size) * 1024*1024*1024
order by 1 desc;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM