码迷,mamicode.com
首页 > 其他好文 > 详细

快速进行表空间清理方案的编写和操作

时间:2019-07-11 18:46:59      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:run   用户   分区表   记录   分布   bst   空间使用率   data   复数   

一、查询数据库表空间使用率
select total.tablespace_name,
round(total.gb, 2) total_gb,
round(total.gb, 2) - round(nvl(free.gb, 0), 2) used_gb,
round(nvl(free.gb, 0), 2) free_gb,
round( 100 ( 1 - nvl( free.gb, 0 ) / total.gb ), 2 ) "USED RATE(%)",
round(nvl(free.gb, 0) / total.gb
100, 2) "FREE RATE(%)"
from (select tablespace_name, sum(bytes) / 1024 / 1024 / 1024 GB
from dba_free_space
group by tablespace_name) free,
(select tablespace_name, sum(bytes) / 1024 / 1024 / 1024 GB
from dba_data_files
group by tablespace_name) total
where total.tablespace_name = free.tablespace_name(+)
order by "FREE RATE(%)" ;

二、查询表空间T_SPACES占用空间比较大的对象
select *
from (select segment_name,
segment_type,
tablespace_name,
sum(bytes / 1024 / 1024 / 1024) used_gb
from dba_segments
where tablespace_name = ‘T_SPACES‘
group by segment_name, segment_type, tablespace_name )
order by 4 desc;

经分析:占用表空间T_SPACES使用率如上图所示。
注:写的比较久了,图不见了,根据上面语句可以查询出来占用空间最大的就是最前面的,因为使用了ORDER BY DESC倒序排列。
结论:我们需要清理上图表中数据,可将表空间T_SPACES使用率降低。
三、建议删除T_TAB1表数据(因为该笔与历史表T_TAB1_his有重复数据)
数据分布情况
1、T_TAB1分布情况如下:
select substr(tran_dt,1,6),count(*) from cpsser.T_TAB1 group by substr(tran_dt,1,6);
20170426日最新查询:

四、清理T_TAB1表数据(201611—201703)
1、查询数据记录数
select count(*) from cpsser.T_TAB1 partition (M20170101) ;--3800371

2、truncate分区表cpsser.T_TAB1数据
alter table T_TAB1 truncate partition M20170101 update global indexes;

五、查看索引是否失效(VALID有效),若失效重建索引
select status from dba_indexes where table_name=‘T_TAB1‘;
注:若有失效的索引,需要重建索引,重建索引语句如下:
alter index P_T_TAB1 rebuild online;

六、清理索引空间
在线重建索引(T_TAB1、T_TAB2)
1、T_TAB1
alter index INX1_T_TAB1 rebuild online;
alter index INX2_T_TAB1 rebuild online;
alter index PK_T_TAB1 rebuild online;

以上是非常简便的ORACLE数据库表空间清理方案,可以直接拿去使用。
小知识:
数据库分区索引的几个视图
dba_ind_partitions:分区索引的分区情况和统计信息。
dba_part_indexes:分区索引的概要统计信息,可以查看表中有那些分区索引,和分区索引的类型。
dba_indexes minus dba_part_indexes 可以得到每个表中哪些非分区索引
分区索引分为:本地索引和全局索引。
分区索引不能对整体重建,必须对每个分区重建。

--查询该用户下有哪些是分区表
select * from dba_part_tables where owner=‘SROOT‘
dba_part_tables是sys用户下的表

快速进行表空间清理方案的编写和操作

标签:run   用户   分区表   记录   分布   bst   空间使用率   data   复数   

原文地址:https://blog.51cto.com/7794482/2419447

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!