---
title: "Oracle 查询表空间使用情况(转)"
author: "foolraty"
date: "2010-03-27T22:26:55Z"
tags: ["C", "Cache", "F#", "Oracle", "SQL"]
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2010-09-01T18:31:24Z"
id: 626282
url: "https://foolraty.iteye.com/blog/626282"
---

--查询表空间使用情况  
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 1

--查询表空间的free space  
select tablespace\_name,  
       count(\*) as extends,  
       round(sum(bytes) / 1024 / 1024, 2) as MB,  
       sum(blocks) as blocks  
from dba\_free\_space  
group by tablespace\_name;

--查询表空间的总容量  
select tablespace\_name, sum(bytes) / 1024 / 1024 as MB  
from dba\_data\_files  
group by tablespace\_name;

--查询表空间使用率  
select total.tablespace\_name,  
       round(total.MB, 2) as Total\_MB,  
       round(total.MB - free.MB, 2) as Used\_MB,  
       round((1 - free.MB / total.MB) * 100, 2) || '%' as Used\_Pct  
from (select tablespace\_name, sum(bytes) / 1024 / 1024 as MB  
          from dba\_free\_space  
         group by tablespace\_name) free,  
       (select tablespace\_name, sum(bytes) / 1024 / 1024 as MB  
          from dba\_data\_files  
         group by tablespace\_name) total  
where free.tablespace\_name = total.tablespace\_name;

1.建立表空间:create tablespace test datafile '/u01/test.dbf'  size 10M uniform size 128k

#指定区尺寸为128k ,块大小为默认8K  

#大文件表空间 create bigfile tablespace big\_tbs datafile  '/u01/big\_tbs.dbf ' size 100G

2.建非标准表show parameter db alter system set db\_2k\_cache\_size=10M create tablespace test datafile '/u01/test.dbf' size 10M blocksize 2K uniform size 128k  

#常见错误

SQL&gt; alter system set db\_2k\_cache\_size=2M; alter system set db\_2k\_cache\_size=2M ERROR at line 1: ORA-02097: parameter cannot be modified because specified value is invalid ORA-00384: Insufficient memory to grow cache

#解决

SQL&gt; alter system set sga\_max\_size=400M scope=spfile; SQL&gt; shutdown immediate; SQL&gt; startup SQL&gt; alter system set db\_2k\_cache\_size=10M; System altered.

3.查看区大小与块大小#区大小 conn y / 123 create table t(i number) tablespace test; Insert into t values(10) select bytes/1024 from user\_segments where segment\_name=upper('t');

#块大小 Show parameter block(默认64K)

#非标准表空间的blocksize SQL&gt; select * from v$dbfile; SQL&gt; select name,block\_size,status from v$datafile; SQL&gt; select block\_size from v$datafile where file#=14;

4.删除表空间drop tablespace test including contents and datafiles

5.查表空间:#查数据文件 select * from v$dbfile; #所有表空间 select * from v$tablespace;

#表空间的数据文件 select file\_name,tablespace\_name from dba\_data\_files;

6.建立undo表空间create undo tablespace undotbs01 datafile '/u01/undotbs01.dbf' size 5M;

#切换到新建的undo表空间 alter system set undo\_tablespace=undotbs01;

7.建立临时表空间create temporary tablespace temp\_data tempfile '/u01/temp.db'  size 5M; create bigfile temporary tablespace bigtem tempfile '/u01/bigtemp.db'  size 5M;

8.改变表空间状态  
(0.)查看状态

#表空间状态 select tablespace\_name,block\_size,status from dba\_tablespaces;

#数据文件状态 select name,block\_size,status from v$datafile;

(1.)表空间脱机alter tablespace test offline

#如果意外删除了数据文件 alter tablespace test offline for recover

(2.)表空间联机alter tablespace test online

(3.)数据文件脱机select * from v$dbfile; alter database datafile 3 offline

(4.)数据文件联机recover datafile 3; alter database datafile 3 online;

(5.)使表空间只读alter tablespace test read only

(6.)使表空间可读写alter tablespace test read write;

9.扩展表空间#首先查看表空间的名字和所属文件及空间 select tablespace\_name, file\_id, file\_name,round(bytes/(1024\*1024),0) total\_space from dba\_data\_files order by tablespace\_name;   #三种扩展方法

1.alter tablespace test add datafile '/u01/test02.dbf' size 10M(自动加一个datafile)

2.alter database datafile '/u01/test.dbf' resize 20M;

3.alter database datafile '/u01/test.dbf' autoextend on next 10M maxsize 1G;  

#设定后查看表空间信息

select a.tablespace\_name,a.bytes total,b.bytes used,c.bytes free,(b.bytes\*100)/a.bytes "% used",(c.bytes\*100)/a.bytes "% free" from sys.sm$ts\_avail a,sys.sm$ts\_used b,sys.sm$ts\_free c where a.tablespace\_name=b.tablespace\_name and a.tablespace\_name=c.tablespace\_name;

10.移动表空间的数据文件

#先确定数据文件据在表空间

SQL&gt;select tablespace\_name,file\_name from dba\_data\_files where file\_name='/u01/test.dbf';

#open状态

SQL&gt;alter tablespace test offline; SQL&gt;host move /u01/test.dbf  /u01/oracle/test.dbf; SQL&gt;alter tablespace test rename datafile '/u01/test.dbf' to '/u01/oracle/test.dbf'; SQL&gt;alter tablespace test offline;  

#mount状态 SQL&gt;shutdown immediate; SQL&gt;startup mount SQL&gt;host move /u01/test.dbf  /u01/oracle/test.dbf; SQL&gt;alter database rename file '/u01/test.dbf' to '/u01/oracle/test.dbf';

11.表空间和数据文件常用的数据字典与动态性能视图v$dbfile v$datafile dba\_segments user\_segments dba\_data\_files v$tablespace dba\_tablespaces user\_tablespaces

表空间扩充参考:http://www.dbmotive.com/oracle\_error\_codes.php?errcode=1658