表空间监控脚本
-----查询表空间使用情况
SELECT TABLESPACE_NAME,
ROUND(TABLESPACE_SIZE / 128 * 1024 / 1024 / 1024, 3) "总空间(GB)",
ROUND(USED_SPACE / 128 * 1024 / 1024 / 1024, 3) "已用(GB)",
ROUND(TABLESPACE_SIZE / 128 * 1024 / 1024 / 1024 -
USED_SPACE / 128 * 1024 / 1024 / 1024,
3) "剩余(GB)",
ROUND(USED_SPACE / TABLESPACE_SIZE * 100, 2) || '%' 使用率
FROM SYS.DBA_TABLESPACE_USAGE_METRICS T
/* WHERE ROUND(TABLESPACE_SIZE / 128 * 1024 / 1024 / 1024 -
USED_SPACE / 128 * 1024 / 1024 / 1024,
3) < '80'
AND TABLESPACE_NAME LIKE 'TBS_%'*/
ORDER BY TABLESPACE_NAME ASC;
2.
-----查询某个表空间下储存的表
SELECT OWNER || '.' || SEGMENT_NAME, SUM(BYTES) / 1024 / 1024 / 1024 BYTES
FROM DBA_SEGMENTS
WHERE TABLESPACE_NAME = 'TBS_YYFX'
AND SEGMENT_NAME ='###表名###'
AND BYTES > 196608 --196608是空表的大小 ,BYTES>196608 抛出空表
GROUP BY OWNER || '.' || SEGMENT_NAME
ORDER BY BYTES DESC;
3.清理表空间的两种方式
(1)
-----清某个表的数据
SELECT BYTES / 1024 / 1024 / 1024,
'ALTER TABLE ' || OWNER || '.' || SEGMENT_NAME ||
' TRUNCATE PARTITION ' || PARTITION_NAME || ';'
FROM DBA_SEGMENTS
WHERE OWNER || '.' || SEGMENT_NAME ='###表名###'
AND BYTES > 196608 --196608是空表的大小 ,BYTES>196608 抛出空表
ORDER BY BYTES DESC;
(2)
-----压缩表 把某个表的部分数据迁移到另一个表空间下
SELECT BYTES / 1024 / 1024 / 1024,
'ALTER TABLE ' || OWNER || '.' || SEGMENT_NAME || ' MOVE PARTITION ' ||
PARTITION_NAME || ' TABLESPACE TBS_DWD;'--要迁移到的表空间
FROM DBA_SEGMENTS
WHERE TABLESPACE_NAME = 'TBS_DWD' --表所在的表空间
AND OWNER || '.' || SEGMENT_NAME = '###表名###'
AND BYTES > 196608 --196608是空表的大小 ,BYTES>196608 抛出空表
ORDER BY BYTES DESC;
正文到此结束