当前位置: 代码网 > it编程>数据库>Oracle > Oracle 删除表中记录如何释放表及表空间大小(最新整理)

Oracle 删除表中记录如何释放表及表空间大小(最新整理)

2026年03月03日 Oracle 我要评论
查看表占有空间大小排序select owner, t.segment_name, t.segment_type, sum(t.bytes / 1024 / 1024

查看表占有空间大小排序

select owner,
       t.segment_name,
       t.segment_type,
       sum(t.bytes / 1024 / 1024) mb
  from dba_segments t
 where t.owner = 'appuser'
   and t.segment_type = 'table'
 group by owner, t.segment_name, t.segment_type
 order by mb desc

1.查看一个表所占的空间大小:

select bytes/1024/1024 ||'mb' table_size ,u.* from user_segments u where u.segment_name='jk_test';

2.查看一个表空间所占的实际大小:

select sum(bytes) / 1024 / 1024 ||'mb' from user_segments u  where tablespace_name = 'data01';

3.查看一个表空间对应的数据文件:

select * from dba_data_files d where d.tablespace_name = 'data01';

4.查看表空间的使用情况:

select a.tablespace_name,      
       filenum,   
       total "total (mb)",  
       f.free "free (mb)",
       to_char(round(free * 100 / total, 2), '990.00') "free%", 
       to_char(round((total - free) * 100 / total, 2), '990.00') "used%",    
       round(maxsizes, 2) "max (mb)"
  from (select tablespace_name,          
               count(file_id) filenum,        
               sum(bytes / (1024 * 1024)) total,          
               sum(maxbytes) / 1024 / 1024 maxsizes      
          from dba_data_files       
         group by tablespace_name) a,     
       (select tablespace_name, round(sum(bytes / (1024 * 1024))) free     
          from dba_free_space      
         group by tablespace_name) f
 where a.tablespace_name = f.tablespace_name

5.查看数据文件的实际使用情况:

select ceil(max_block * block_size / 1024)
  from (select max(block_id) max_block
          from dba_extents
         where file_id in (select file_id
                             from dba_data_files d
                            where d.tablespace_name = 'users')) m,
       (select value / 1024 block_size
          from v$parameter
         where name = 'db_block_size') b

一、创建一个有十万条记录的测试表jk_test ,查看其所占空间大小3873m
delete jk_test。再次查看大小不会变,此时执行select * from jk_test会发现速度超极慢,查询结果却是空,查看其cost,发现是10万多。很难理解吧,其实是其所占空间没有释放的缘故。
执行alter table jk_test move 或 alter table jk_test move storage(initial 64k)
或alter table jk_test deallocate unused或 alter table jk_test shrink space.
注意:因为alter table jk_test move 是通过消除行迁移,清除空间碎片,删除空闲空间,实现缩小所占的空间,但会导致此表上的索引无效(因为rowid变了,无法找到),所以执行 move 就需要重建索引。
找到表对应的索引。
select index_name,table_name,tablespace_name,index_type,status  from dba_indexes  where table_owner='scott' ;
根据status 的值,重建无效的就行了。
sql='alter index '||index_name||' rebuild'; 使用存储过程执行,稍微安慰。
还要注意alter table move过程中会产生锁,应该避免在业务高峰期操作!
再次查看其所占空间大小,发现已经很小了,再一次执行查询,很快了吧。
另外说明:truncate table jk_test 会执行的更快,而且其所占的空间也会释放,我想应该是truncate 语句执行后是不会进入oracle回收站(recylebin)的缘故。如果drop 一个表加上purge 也不会进回收站(在此里面的数据可以通过flashback找回)。
不管是delete还是truncate 相应数据文件的大小并不会改变,如果想改变数据文件所占空间大小可执行如下语句:alter database datafile 'filename' resize 8g重定义数据文件的大小(不能小于该数据文件已用空间的大小)。
另补充一些purge知识
purge操作:
1). purge tablespace tablespace_name : 用于清空表空间的recycle bin
2). purge tablespace tablespace_name user user_name: 清空指定表空间的recycle bin中指定用户的对象
3). purge recyclebin: 删除当前用户的recycle bin中的对象
4). purge dba_recyclebin: 删除所有用户的recycle bin中的对象,该命令要sysdba权限
5). drop table table_name purge:  删除对象并且不放在recycle bin中,即永久的删除,不能用flashback恢复。
6). purge index recycle_bin_object_name: 当想释放recycle bin的空间,又想能恢复表时,可以通过释放该对象的index所占用的空间来缓解空间压力。 因为索引是可以重建的。
二、如果某些表占用了数据文件的最后一些块,则需要先将该表导出或移动到其他的表空间中,然后删除表,再进行收缩。不过如果是移动到其他的表空间,需要重建其索引。

1)sql> alter table t_obj move tablespace t_tbs1;   ---移动表到其它表空间

也可以直接使用exp和imp来进行

2)sql>alter owner.index_name rebuild;     --重建索引

3)删除原来的表空间
三:对表分析之后也可以优化(本人没有试过)

analyze table ysgl_compile_reqsub 
compute statistics for all indexes; 
也要看情况,不是什么情况都可以优化,等下次有机会再测试一下。

到此这篇关于oracle 删除表中记录如何释放表及表空间大小的文章就介绍到这了,更多相关oracle 删除表中记录释放表空间内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com