如何查看各个表空间占用磁盘情况

本文介绍在Windows NT4.0环境下,使用ORACLE8.0.4通过SQL语句查询不同表空间的磁盘占用情况,包括已使用空间、剩余空间及百分比。

如何查看各个表空间占用磁盘情况
  
  如何查看各个表空间占用磁盘情况 如何查看各个表空间占用磁盘情况?
  软件环境:
  1、Windows NT4.0+ORACLE 8.0.4
  2、ORACLE安装路径为:C:\ORANT
  SQL语句:
   /* 中文环境 */
   col 表空间名 format a20;
   select
   b.file_id 文件ID号,
   b.tablespace_name 表空间名,
   b.bytes 字节数,
   (b.bytes-sum(nvl(a.bytes,0))) 已使用,
   sum(nvl(a.bytes,0)) 剩余空间,
   sum(nvl(a.bytes,0))/(b.bytes)*100 剩余百分比
   from dba_free_space a,dba_data_files b
   where a.file_id=b.file_id
   group by b.tablespace_name,b.file_id,b.bytes
   order by b.file_id;
   /* 英文环境 */
   col tablespace_name format a20;
   select
   b.file_id file_ID,
   b.tablespace_name tablespace_name,
   b.bytes Bytes,
   (b.bytes-sum(nvl(a.bytes,0))) used,
   sum(nvl(a.bytes,0)) free,
   sum(nvl(a.bytes,0))/(b.bytes)*100 Percent
   from dba_free_space a,dba_data_files b
   where a.file_id=b.file_id
   group by b.tablespace_name,b.file_id,b.bytes
   order by b.file_id;
  
  文件ID号 表空间名 字节数 已使用 剩余空间 剩余百分比
  --------- ------------------------------ --------- --------- --------- ----------
   1 SYSTEM 20971520 9971712 10999808 52.451172
   2 USER_DATA 3145728 432128 2713600 86.263021
   3 ROLLBACK_DATA 5242880 1640448 3602432 68.710938
   4 TEMPORARY_DATA 2097152 2048 2095104 99.902344

### 查看 PostgreSQL 数据库中表的空间占用情况 在 PostgreSQL 中,可以通过系统内置函数和系统表来查询数据库各个表的空间占用情况。以下 SQL 查询可以用于查看指定模式(如 `public`)下所有表的数据大小及索引大小: ```sql SELECT table_schema || '.' || table_name AS table, pg_size_pretty(pg_relation_size(table_schema || '.' || table_name)) AS data_size, pg_size_pretty(pg_total_relation_size(table_schema || '.' || table_name)) AS total_size FROM information_schema.tables WHERE table_schema = 'public' ORDER BY pg_total_relation_size(table_schema || '.' || table_name) DESC; ``` 该查询返回每个表的名称、仅数据部分的大小以及包括索引在内的总大小,并按照总大小降序排列,适用于查看表在磁盘上的实际占用情况[^1]。 --- ### 查看 MySQL 数据库中表的空间占用情况 在 MySQL 中,可以通过 `information_schema` 数据库中的 `tables` 表来查询每个表的数据大小和索引大小。以下 SQL 查询可用于查看特定数据库中每个表的存储空间占用情况: ```sql SELECT table_name AS table, concat(round(sum(data_length / 1024 / 1024), 2), ' MB') AS data_size, concat(round(sum(index_length / 1024 / 1024), 2), ' MB') AS index_size, concat(round(sum((data_length + index_length) / 1024 / 1024), 2), ' MB') AS total_size FROM information_schema.tables WHERE table_schema = 'your_database_name' GROUP BY table_name ORDER BY total_size DESC; ``` 此查询将返回每个表的数据大小、索引大小及总大小,并按总大小降序排列,便于识别占用空间较大的表[^3]。 若需查看某个表的剩余空间(即分配但未使用的空间),可以使用以下 SQL 查询: ```sql SELECT concat(round(sum(data_free / 1024 / 1024), 2), ' MB') AS free_space FROM information_schema.partitions WHERE table_schema = 'your_database_name' AND table_name = 'your_table_name'; ``` 此查询适用于查看表中未使用的空间,有助于优化存储使用[^2]。 --- ### 总结 - **PostgreSQL** 提供了内置函数 `pg_relation_size()` 和 `pg_total_relation_size()` 来获取表的数据大小和包含索引的总大小。 - **MySQL** 通过 `information_schema.tables` 提供了数据长度、索引长度等信息,可计算表的存储占用情况。 - 两者均支持按大小排序,帮助识别占用空间较大的表,便于数据库优化和维护。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值