Oracle查看表空间使用情况,oracle查看表空间
查看表空间使用情况
select upper(f.tablespace_name) "表空间名",
d.tot_grootte_mb "表空间大小(m)",
d.tot_grootte_mb - f.total_bytes "已使用空间(m)",
to_char(round((d.tot_grootte_mb - f.total_bytes) / d.tot_grootte_mb * 100,
2),
'990.99') "使用比",
f.total_bytes "空闲空间(m)",
f.max_bytes "最大块(m)"
from (select tablespace_name,
round(sum(bytes) / (1024 * 1024), 2) total_bytes,
round(max(bytes) / (1024 * 1024), 2) max_bytes
from sys.dba_free_space
group by tablespace_name) f,
(select dd.tablespace_name,
round(sum(dd.bytes) / (1024 * 1024), 2) tot_grootte_mb
from sys.dba_data_files dd
group by dd.tablespace_name) d
where d.tablespace_name = f.tablespace_name
order by 4 desc;
查看表空间中每个表的大小
select segment_name, isize || 'M'
from (select segment_name, sum(bytes) / 1024 / 1024 isize
from dba_segments
where tablespace_name = 'SNBST_HIST'
AND segment_type = 'TABLE'
GROUP BY segment_name)
order by isize desc;
查看表空间文件使用大小
SELECT B.TABLESPACE_NAME 表空间,
B.FILE_NAME 物理文件名,
B.BYTES / 1024 / 1024 大小M,
(B.BYTES - SUM(NVL(A.BYTES, 0))) / 1024 / 1024 已使用M,
SUBSTR((B.BYTES - SUM(NVL(A.BYTES, 0))) / (B.BYTES) * 100, 1, 5) 利用率
FROM DBA_FREE_SPACE A, DBA_DATA_FILES B
WHERE A.FILE_ID = B.FILE_ID
GROUP BY B.TABLESPACE_NAME, B.FILE_NAME, B.BYTES
ORDER BY B.TABLESPACE_NAME;
1. 查看所有表空间大小
SQL> select tablespace_name,sum(bytes)/1024/1024 from dba_data_files
2 group by tablespace_name;
2. 已经使用的表空间大小
SQL> select tablespace_name,sum(bytes)/1024/1024 from dba_free_space
2 group by tablespace_name;
3. 所以使用空间可以这样计算
select a.tablespace_name,total,free,total-free used from
( select tablespace_name,sum(bytes)/1024/1024 total from dba_data_files
group by tablespace_name) a,
( select tablespace_name,sum(bytes)/1024/1024 free from dba_free_space
group by tablespace_name) b
where a.tablespace_name=b.tablespace_name;
4. 下面这条语句查看所有segment的大小。
Select Segment_Name,Sum(bytes)/1024/1024 From User_Extents Group By Segment_Name
5. 还有在命令行情况下如何将结果放到一个文件里。
SQL> spool out.txt
SQL> select * from v$database;
SQL> spool off
oracle 数据库里查看表空间使用状况;
oracle表空间的事情状况要经常查看,一般空闲比例过低的时候就应该考虑增大表看空间了。查看方法如下SQL:
方法一:
select dbf.tablespace_name,
dbf.totalspace "总量(M)",
dbf.totalblocks as 总块数,
dfs.freespace "剩余总量(M)",
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)
方法二:
SELECT Total.name "Tablespace Name",
Free_space, (total_space-Free_space) Used_space, total_space
FROM
(select tablespace_name, sum(bytes/1024/1024) Free_Space
from sys.dba_free_space
group by tablespace_name
) Free,
(select b.name, sum(bytes/1024/1024) TOTAL_SPACE
from sys.v_$datafile a, sys.v_$tablespace B
where a.ts# = b.ts#
group by b.name
) Total
WHERE Free.Tablespace_name = Total.name
当发现有的表空间不够的错误时,处理如下:
1:找出该表空间对应的数据文件及路径
select * from dba_data_files t
where t.tablespace_name = 'ARD'
2:增大数据文件
alter database datafile '全路径的数据文件名称' resize ***M
3:增加数据文件
alter tablespace 表空间名称
add datafile '全路径的数据文件名称' ***M
注解:表空间尽量让free百分比保持在10%以上,如果低于10%就增加datafile或者resizedatafile,一般数据文件不要超过2G...余下全文>>