本教程详细介绍如何查询数据库中各个表空间的当前容量及其使用情况,帮助用户监控和管理存储资源。
以下是查询表空间大小及已使用百分比的SQL代码:
```sql
SELECT a.tablespace_name,
a.bytes / 1024 / 1024 Sum MB,
(a.bytes - b.bytes) / 1024 / 1024 used MB,
b.bytes / 1024 / 1024 free MB,
ROUND(((a.bytes - b.bytes) / a.bytes) * 100, 2) percent_used
FROM (
SELECT tablespace_name,
SUM(bytes) bytes
FROM dba_data_files
GROUP BY tablespace_name
) a,
(
SELECT tablespace_name,
SUM(bytes) bytes,
MAX(bytes) largest
FROM dba_free_space
GROUP BY tablespace_name
) b
WHERE a.tablespace_name = b.tablespace_name
ORDER BY ((a.bytes - b.bytes) / a.bytes) DESC;
```
这段代码用于查询数据库中各表空间的总大小、已使用大小及剩余空间,并计算出使用的百分比。