Oracle表空间管理
Oracle表空间维护是DBA工作之一。
查看表空间
1、查看表空间的名称及大小
select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size from dba_tablespaces t, dba_data_files d where t.tablespace_name = d.tablespace_name group by t.tablespace_name;
2、查看表空间物理文件的名称及大小
select tablespace_name, file_id, file_name, round(bytes/(1024*1024),0) total_space from dba_data_files order by tablespace_name;
3、显示表空间所包含的数据文件
select tablespace_name,file_id,bytes/1024/1024,file_name from dba_data_files where tablespace_name='表空间名';
查看表空间使用率
set linesize 800;
select tablespace_name,datafile_count,total_mb,used_mb,total_mb - used_mb free_mb, ROUND(100.0 * used_mb/total_mb,2) pct_used, 100 - ROUND(100.0 * used_mb/total_mb,2) pct_free
from (SELECT df.tablespace_name,
COUNT(*) datafile_count,
ROUND(SUM(df.MAXBYTES) / 1048576, 2) total_mb,
ROUND(SUM(df.BYTES) / 1048576 - SUM(free.BYTES) / 1048576, 2) used_mb,
100 - ROUND(100.0 * SUM(free.BYTES) / SUM(df.BYTES), 2) pct_used,
ROUND(100.0 * SUM(free.BYTES) / SUM(df.BYTES), 2) pct_free
FROM dba_data_files df,
(SELECT tablespace_name,
file_id,
SUM(BYTES) BYTES
FROM dba_free_space
GROUP BY tablespace_name, file_id) free
WHERE df.tablespace_name = free.tablespace_name(+)
AND df.file_id = free.file_id(+)
GROUP BY df.tablespace_name)
ORDER BY 7;
--推荐使用(简单快捷)
set linesize 800;
SELECT
tablespace_name,
tablespace_size * 8 / 1024 "Total MB",
used_space * 8 / 1024 "Used MB",
(tablespace_size - used_space) * 8 / 1024 "Free MB",
(used_space / tablespace_size) * 100 "Used%"
FROM
dba_tablespace_usage_metrics order by 5 desc;
表空间管理
--创建表空间
create tablespace test datafile '/opt/oracle/oradata/testdb110/test01.dbf' size 128M autoextend on next 128M MAXSIZE 32760M;
--增加数据文件
ALTER TABLESPACE test ADD DATAFILE '/opt/oracle/oradata/testdb110/test02.dbf' SIZE 128M AUTOEXTEND ON NEXT 128M MAXSIZE 32760M;
--删除表空间
drop tablespace 表空间名 including contents and datafiles;(彻底删除,请勿操作);
--使表空间脱机
alter tablespace 表空间名 offine;
--使表空间连机
alter tablespace 表空间名 onine;
--只读表空间
alter tablespace 表空间名 read only;
--可读写表空间
alter tablespace 表空间名 read write;
以上是工作常用的脚本,留下来做个备忘,希望对你也有所帮助。