--1. 查看表空间剩余总量
SELECT dbf.tablespace_name,
dbf.totalspace/1024 "总量(G)",
dbf.totalblocks AS 总块数,
dfs.freespace/1024 "剩余总量(G)",
dfs.freeblocks "剩余块数",
(dfs.freespace / dbf.totalspace) * 100 "空闲比例"
FROM (SELECT t.tablespace_name,
SUM(t.bytes) / 1024 / 1024 totalspace,
SUM(t.blocks) totalblocks
FROM dba_data_files t
GROUP BY t.tablespace_name) dbf,
(SELECT tt.tablespace_name,
SUM(tt.bytes) / 1024 / 1024 freespace,
SUM(tt.blocks) freeblocks
FROM dba_free_space tt
GROUP BY tt.tablespace_name) dfs
WHERE TRIM(dbf.tablespace_name) = TRIM(dfs.tablespace_name)
AND dfs.tablespace_name='TBS_LARGE';
TABLESPACE_NAME 总量(G) 总块数 剩余总量(G) 剩余块数 空闲比例
1 TBS_LARGE 9 1179648 4.4853515625 581120 49.2621527777778
--2.查询了解数据文件的路径和 file_id
SELECT * FROM dba_data_files t
WHERE t.TABLESPACE_NAME='TBS_LARGE';
FILE_NAME FILE_ID
/oradata/orcl/TBS_LARGE01.dbf 5
--3.查找此数据文件最大的BLOCK_ID位置
SELECT MAX(BLOCK_ID) FROM DBA_EXTENTS WHERE file_id=5
select segment_name, block_id from dba_extents where tablespace_name='TBS_LARGE'
order by block_id desc;
1597888
select value from v$parameter where name = 'db_block_size';--8192
--4.得到目前使用的最大的表空间位置
SELECT 1597888*8/1024/1024 FROM DUAL;
--5.根据之前得到的 剩余总量 ,使用公式 SELECT 4.4853515625【剩余总量(G)】/8*1024*1024 FROM DUAL 得到 587904,执行以下语句
SELECT DISTINCT 'alter table CLEAR.' || segment_name ||
' MOVE STORAGE(INITIAL 500k) ;'
FROM dba_extents
WHERE tablespace_name = 'TBS_LARGE'
AND segment_type = 'TABLE'
AND file_id = 5
AND block_id > 587904;
SELECT DISTINCT 'alter index clear.' || segment_name ||
' REBUILD tablespace TBS_LARGE storage(initial 500K next 500k pctincrease 0);'
FROM dba_extents
WHERE tablespace_name = 'TBS_LARGE'
AND segment_type = 'INDEX'
AND file_id = 5
AND block_id > 587904;
--6.重建索引
SELECT 'alter index CLEAR.' || t.index_name ||' rebuild;' FROM dba_indexes t WHERE t.owner ='CLEAR';
--7.执行以下缩表空间语句。
select 'alter database datafile ''' || a.file_name || ''' resize ' ||
round(a.filesize - (a.filesize - c.hwmsize - 100) * 0.8) || 'M;',
a.filesize || 'M' as "数据文件的总大小",
c.hwmsize || 'M' as "数据文件的实用大小"
from (select file_id, file_name, round(bytes / 1024 / 1024) as filesize
from dba_data_files
WHERE file_id=5) a,
(select file_id, round(max(block_id) * 8 / 1024) as HWMsize
from dba_extents
group by file_id) c
where a.file_id = c.file_id
and a.filesize - c.hwmsize > 100
AND c.file_id='5';
--8.如果还报错,执行以下语句,可能是回收站引起。sqlplus / as sysdba 登录后执行 purge dba_recyclebin
purge dba_recyclebin
引申:
purge tablespace tablespace_name;
purge tablespace tablespace_name user user_name;
purge recyclebin;--清空当前用户的回收站
purge dba_recyclebin;--清空所有用户的回收站