ORACLE表空间操作实例
Posted Beng Dou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ORACLE表空间操作实例相关的知识,希望对你有一定的参考价值。
本文主要介绍oracle表空间常见的操作实例,包括创建、查询、增加、删除、修改。表空间和数据文件常用的数据字典和动态性能视图包括v$dbfile、v$datafile、dba_segments、user_segments、dba_data_files、v$tablespace、dba_tablespaces、user_tablespaces。
创建表空间
1、创建数据表空间
CREATE TABLESPACE test DATAFILE ‘/opt/oracle/oradata/test.dbf‘ SIZE 1024M;
2、创建undo表空间
CREATE UNDO TABLESPACE undotbs DATAFILE ‘/opt/oracle/oradata/undotbs.dbf‘ SIZE 1024M;
3、创建临时表空间
CREATE TEMPORARY TABLESPACE TEMP TEMPFILE ‘/opt/oracle/oradata/temp.dbf‘ SIZE 1024M;
查询表空间
1、查询oracle系统用户的默认表空间和临时表空间
select default_tablespace,temporary_tablespace from dba_users where username = ‘USER‘;
2、查看系统当前默认的数据表空间和临时表空间
select * from dba_properties where property_name like ‘DEFAULT%‘ ; #查询 alter database default tablespace DATATBS2; #修改默认数据表空间 alter database default temporary tablespace TEMP2; #修改默认临时表空间
3、查看所有表空间、数据文件及表空间的数据文件
select * from v$datafile; #查看数据文件 select * from v$tempfile; #查看临时文件 select * from v$tablespace; #查询所有表空间 select file_name,tablespace_name from dba_data_files; #查看表空间对应的数据文件 select sum(d.bytes)/1024/1024/1024 ||‘G‘ as total_bytes,d.tablespace_name from dba_datafiles d where d.tablespace_name = ‘UNDOTBS‘ group by d.tablespace_name; #查看表空间文件大小 select bytes/1024/1024/1024 as "Size(G)",name from v$tempfile order by bytes; #查看临时文件大小
4、查看用户表占用空间大小
#USER_SEGMENTS SELECT SEGMENT_NAME TABLE_NAME,SUM(BYTES)/(1024*1024) "TABLE_SIZE[MB]" FROM USER_SEGMENTS WHERE SEGMENT_TYPE=‘TABLE‘ AND SEGMENT_NAME=‘TABLE_NAME‘ GROUP BY SEGMENT_NAME; #DBA_SEGMENTS,shell编程中常用 SELECT SEGMENT_NAME TABLE_NAME,SUM(BYTES)/(1024*1024) "TABLE_SIZE[MB]" FROM DBA_SEGMENTS WHERE SEGMENT_TYPE=‘TABLE‘ AND SEGMENT_NAME=‘TABLE_NAME‘ GROUP BY SEGMENT_NAME;
5、查询单张表占用空间大小
select segment_name,bytes from dba_segments where segment_name = ‘TABLE_NAME‘ and owner = ‘USER‘;
6、查询所有用户表占用空间的前十名
select * from (select segment_name,bytes from dba_segments where owner = ‘USER‘ order by bytes desc ) where rownum <= 10;
7、查询表空间使用情况
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;
8、查询表空间总大小
select tablespace_name, sum(bytes) / 1024 / 1024 as MB from dba_data_files group by tablespace_name;
9、查询表空间剩余大小
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,BLOCK_SIEZE,STATUS FROM DBA_TABLESPACE; #查看表空间状态 SLEECT NAME,BLOKC_SIZE,STATUS FROM V$DATAFILE; #查看数据文件状态 ALTER TABLESPACE DATATBS OFFLINE; #使表空间脱机 ALTER TABLESPACE DATATBS OFFLINE FOR RECOVER; #如果是意外删除了数据文件,则必须带有RECOVER选项 ALTER TABLESPACE DATATBS ONLINE; #使表空间联机 ALTER DATABASE DATAFILE 3 OFFLINE; #使数据文件脱机 ALTER DATABASE DATAFILE 3 ONLINE; #使数据文件联机 ALTER TABLESPACE DATATBS READ WRITE; #使表空间可读写
扩展表空间
SQL> alter tablespace tablespacename add datafile ‘/home/oracle/add_tablespacename.dbf‘ size 1024M; #增加数据文件方式,不允许自动增长 SQL> alter tablespace tablespacename add datafile ‘/home/oracle/add_tablespacename.dbf‘ size 1024M autoextend on next 5M maxsize 2048M; #增加数据文件方式且允许自动增长 SQL> alter database datafile ‘/home/oracle/tablespace.dbf‘ autoextend on next 5M maxsize 2048M; #允许已存在的数据文件自动扩展 SQL> alter database datafile ‘/home/oracle/tablespace.dbf‘ resize 2048M; # 改变当前数据文件大小 #调整临时表空间文件 SQL> alter tablespace temp add tempfile ‘/home/oracle/temp2.dbf‘ size 2048M; #扩展临时表空间大小,默认autoextend off SQL> alter tablespace temp add tempfile ‘/home/oracle/temp2.dbf‘ size 1024M antoextend on next 128M maxsize 2048M; #扩展临时表空间大小 SQL> alter database tempfile ‘/home/oracle/temp.dbf‘ resize 2048M;
删除表空间
DROP TABLESPACE DATATBS INCLUDING CONTENTS AND DATAFILES;
说明:including contents 字句用来删除段,and datafiles 字句用来删除数据文件,cascade constraints 字句用来删除所有的引用完整性约束
相关实例
1、移动表空间数据文件
SQL> select tablespace_name,file_name from dba_data_files where file_name=‘/opt/oracle/oradata/datatbs.dbf‘; SQL> alter tablespace datatbs offline; SQL> host move /opt/oracle/oradata/datatbs.dbf /home/oracle/datatbs.dbf; SQL> alter tablespace datatbs rename datafile ‘/opt/oracle/oradata/datatbs.dbf‘ to ‘/home/oracle/datatbs.dbf‘; SQL> alter tablespace datatbs online;
以上是关于ORACLE表空间操作实例的主要内容,如果未能解决你的问题,请参考以下文章