Docs/数据库/oracle/查询具体表占用的空间.md

2.2 KiB

当前用户表占用空间

select SEGMENT_NAME as TABLE_NAME,BYTES/1024/1024 as 大小MB 
from USER_SEGMENTS 
where SEGMENT_TYPE='TABLE' 
order by BYTES desc;

所有用户表占用空间

select OWNER,SEGMENT_NAME as TABLE_NAME,BYTES/1024/1024 as 大小MB
from DBA_SEGMENTS
where SEGMENT_TYPE='TABLE'
order by OWNER,BYTES desc;

查询表空间及表空间所占空间大小

SELECT
	tablespace_name,
	sum( bytes ) / 1024 / 1024 
FROM
	dba_data_files 
GROUP BY
	tablespace_name;

查询表空间物理文件的名称及大小

SELECT
	a.tablespace_name,
	total,
	free,
	total - free AS used,
	substr( free / total * 100, 1, 5 ) AS "FREE%",
	substr( ( total - free ) / total * 100, 1, 5 ) AS "USED%" 
FROM
	(SELECT tablespace_name, sum( bytes ) / 1024 / 1024 AS total FROM dba_data_files GROUP BY tablespace_name) a,
	(SELECT tablespace_name, sum( bytes ) / 1024 / 1024 AS free FROM dba_free_space GROUP BY tablespace_name) b 
WHERE
	a.tablespace_name = b.tablespace_name 
ORDER BY
	a.tablespace_name;

查询所有表空间以及每个表空间的大小,已用空间,剩余空间,使用率和空闲率

SELECT
	a.tablespace_name,
	total,
	free,
	total - free AS used,
	substr( free / total * 100, 1, 5 ) AS "FREE%",
	substr( ( total - free ) / total * 100, 1, 5 ) AS "USED%" 
FROM
	(SELECT tablespace_name, sum( bytes ) / 1024 / 1024 AS total FROM dba_data_files GROUP BY tablespace_name) a,
	(SELECT tablespace_name, sum( bytes ) / 1024 / 1024 AS free FROM dba_free_space GROUP BY tablespace_name) b 
WHERE
	a.tablespace_name = b.tablespace_name 
ORDER BY
	a.tablespace_name;

查询某个具体的表所占空间的大小

SELECT
	t.segment_name,
	t.segment_type,
	sum( t.bytes / 1024 / 1024 ) "占用空间(M)" 
FROM
	dba_segments t 
WHERE
	t.segment_type = 'TABLE' 
	AND t.segment_name = 'TABLE_NAME' 
GROUP BY
	OWNER,
	t.segment_name,
	t.segment_type;

查看每个表所占空间大小

select * from (
  select t.tablespace_name,t.owner, t.segment_name, t.segment_type, sum(t.bytes / 1024 / 1024) mb
  from dba_segments t
  where t.segment_type='TABLE'
  group by t.tablespace_name,t.OWNER, t.segment_name, t.segment_type
) t
order by t.mb desc
;