Oracle扩展表空间方案

1、检查表空间占用率

1.1. 脚本检查

查看一个用户所占的空间

用该用户登录

select 
sum(bytes)/1024/1024 MB 
from user_extents u

 查看表空间还剩多少,用这个,还能看每个文件情况(使用语句1或者语句2查询)
语句1:

select b.file_id 文件ID,
       b.tablespace_name 表空间,
       b.file_name 物理文件名,
       b.bytes 总字节数,
       (b.bytes - sum(nvl(a.bytes, 0))) 已使用,
       sum(nvl(a.bytes, 0)) 剩余,
       sum(nvl(a.bytes, 0)) / (b.bytes) * 100 剩余百分比
  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.file_id, b.bytes
 order by b.tablespace_name

语句2:

select a.a1 tablespacename, -- 表空间名称,
       c.c2 tablespacetype, -- 类型,
       c.c3, --区管理, 
       b.b2 / 1024 / 1024 tablespaceSize, --表空间大小M, 
       (b.b2 - a.a2) / 1024 / 1024 tablespaceYONG, --已使用M, 
       substr((b.b2 - a.a2) / b.b2 * 100, 1, 5) beifen --利用率 
  from (select tablespace_name a1, sum(nvl(bytes, 0)) a2
          from dba_free_space
         group by tablespace_name) a,
       (select tablespace_name b1, sum(bytes) b2
          from dba_data_files
         group by tablespace_name) b,
       (select tablespace_name c1, contents c2, extent_management c3
          from dba_tablespaces) c
 where a.a1 = b.b1
   and c.c1 = b.b1;

该语句通过查询dba_free_space,dba_data_files,dba_tablespaces这三个数据字典表,得到了表空间名称,表空间类型,区管理类型,以”兆”为单位的表空间大小,已使用的表空间大小及表空间利用率。dba_free_space表描述了表空间的空闲大小,dba_data_files表描述了数据库中的数据文件,dba_tablespaces表描述了数据库中的表空间。

1.2. 异常现象

Internal Exception: java.sql.BatchUpdateException: ORA-01653: unable to extend table XXXXXX.XXXXTABLE by 128 in tablespace XXXXX

2. 扩展表空间解决方案

2.1. 第一步

查看表空间的名字和档案位置。

select tablespace_name,
       file_id,
       file_name,
       round(bytes / (1024 * 1024), 0) total_space
  from dba_data_files
 order by tablespace_name;

2.2. 第二步

增大所需表空间的尺寸
alter database datafile '表空间储存位置'resize 新的尺寸,例如:

alter database datafile 'e:\oracle\oradata\esps_2008.dba'resize 4000m;

BTW:
对於ORACLE数据库的表空间。除了手动增大所需表空见的尺寸的方法外:也可使用其他方式来扩展表空见的尺寸。

第一种:增加数据档案。让表空间名对应更多的数据档案
alter tablespace 表空间名称
add datafile '新数据档案的储存位置' size 新数据档案的尺寸,例如:

alter tablespace ESPS_2008 add datafile 'e:\oracle\oradata\esps_2010.dba' size 1000m

第二种:设定数据档案自动扩展,以杜绝表空间不足的问题
alter database datafile '数据档案的储存位置'
autoextend on next 下一次扩展数据档案的尺寸 maxsize 最大可接受的扩展尺寸的极限,例如:

alter database datafile 'e:\oracle\oradata\esps_2008.dba'
        autoextend on next 100m maxsize 10000m

2.3. 第三步

查阅设定后的表空间资讯。

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;

猜你喜欢

转载自blog.csdn.net/akaks0/article/details/82345192