运维过程中数据库表空间使用率是经常需要关注的一个指标经常使用。表空间用完响现网业务,后果比较严重。下面总结下查看表空间使用率、扩展表空间等常用操作
查表空间使用率(非asm方式表空间):
select a.tablespace_name,a.ALLFREE/1024 AllFreeKB,c.USED_BYTES/1024 AllUsedKB ,b.ALLSIZE/1024 AllSizeKB,( a.ALLFREE / b.ALLSIZE * 100 ) FREE_PRECENT ,a.MAXFREEBYTES/1024 MaxFreeKB,a.FREENUM
from ( select tablespace_name,sum(bytes) ALLFREE, max(bytes) MAXFREEBYTES,count(*) FREENUM
from dba_free_space group by tablespace_name ) a,
( select tablespace_name,sum(bytes) ALLSIZE from dba_data_files group by tablespace_name ) b,
( select tablespace_name, sum(bytes) USED_BYTES from dba_extents group by tablespace_name ) c
where a.tablespace_name = b.tablespace_name and a.tablespace_name = c.tablespace_name
order by tablespace_name;
查表空间使用率(asm方式表空间):
select name,allocation_unit_size,total_mb,free_mb from v$asm_diskgroup;
查看各表数据表大小,查询后清理不太重要的大表数据:
select a.segment_name,a.tablespace_name,a.PARTITION_NAME,b.table_name,a.bytes/1024/1024 mbytes,a.blocks
from user_segments a, user_tables b
where a.segment_name = b.table_name
and a.tablespace_name='DAT01'
order by a.bytes/1024/1024 desc;
查看各索引大小,查询后清理不太重要的索引:
select a.segment_name,a.tablespace_name,b.table_name,a.bytes/1024/1024 mbytes,a.blocks
from user_segments a, user_indexes b
where a.segment_name = b.index_name
and a.segment_type = 'INDEX'
and a.tablespace_name='IND02'
order by a.bytes/1024/1024 desc;
增加数据库表空间(非asm方式表空间):
ALTER TABLESPACE HAIP_DAT01 ADD DATAFILE '/oradata/data/DAT01_66.dbf' SIZE 8192M;
增加数据库表空间(asm方式表空间):
alter tablespace HAIP_IND02 add datafile '+HADG1' size 16G;
修改数据文件大小:
alter database datafile 'D:\ORACLE\PRODUCT\10.2.0\ORADATA\SUREDD\UNTOTBS_NEW_01.DBF' resize 2048M;