天天看点

【Oracle日常积累】Oracle表空间扩展

用户生产系统运行一段时间后,表空间已经耗尽,只有扩展表空间,才能继续在表空间上添加表数据。

1.先去查询表空间的使用情况及使用百分比

select a.tablespace_name "表空间名",
       a.bytes / 1024 / 1024 "表空间大小(m)",
       (a.bytes - b.bytes) / 1024 / 1024 "已使用空间(m)",
       b.bytes / 1024 / 1024 "空闲空间(m)",
       round(((a.bytes - b.bytes) / a.bytes) * 100, 2) "使用%比"
  from (select tablespace_name, sum(bytes) bytes
          from dba_data_files
         group by tablespace_name) a,
       (select tablespace_name, sum(bytes) bytes, max(bytes) largest
          from dba_free_space
         group by tablespace_name) b
 where a.tablespace_name = b.tablespace_name
 order by ((a.bytes - b.bytes) / a.bytes) desc;
           

2.查询表空间名,文件名,是否自动扩展,表空间大小及最大空间

select t.tablespace_name,
       d.file_name,
       d.autoextensible,
       d.bytes,
       d.maxbytes,
       d.status
  from dba_tablespaces t, dba_data_files d
 where t.tablespace_name = d.tablespace_name
 order by tablespace_name, file_name;
           

3.表空间扩容

注意:一个数据文件最大只能32G

3.1 方法一

--手工改变已存在数据文件的大小
alter tablespace test_comm add datafile'd:\oracle\product\10.2.0\oradata\hisdb\test01.dbf' size 20480m;
           

3.2 方法二

--允许已存在的数据文件自动增长
alter database datafile 'd:\oracle\product\10.2.0\oradata\hisdb\test01.dbf'autoextend on next 100m maxsize 20480m; 
           

3.3 方法三

--增加数据文件
 /* 其中设置的每个文件初始分配空间为7g, autoextend on为自动增长大小,oracle单个文件大小最大不超过32g*/
 /*说明1:sql脚本(增加两个数据文件,需要扩容的表空间是test_comm)*/
 /*说明2:
  /data/oracle/tablespace/msg21.dbf -文件名及存放路径
  size -表空间的大小
  utoextend -是否开启自动扩展,on开启
*/
alter tablespace test_comm add datafile '/data/oracle/tablespace/test02.dbf' size 7167m autoextend on ;
alter tablespace test_comm add datafile '/data/oracle/tablespace/test03.dbf' size 7167m autoextend on ;