link:
date: 2022-09-06
相关表和视图:
dba_data_files
dba_tablespaces,v$tablespace
dba_free_space
dba_users
dba_segments
dba_tables
all_tables
-- 1 )方式1:dba_tablespaces select * from dba_tablespaces; -- 2 )方式2:v$tablespace select * from v$tablespace;
select tablespace_name, file_id, file_name, round(bytes / (1024 * 1024), 0) total_space from sys.dba_data_files order by tablespace_name;
--查询 select username,default_tablespace, temporary_tablespace, t.* from dba_users t; --修改默认表空间 ALTER USER $usercode DEFAULT TABLESPACE $tablespacename; ALTER USER $usercode TEMPORARY TABLESPACE temp;
select table_name,tablespace_name from user_tables where TABLE_NAME='表名';
/*查看表空间下有多少用户,tablespace_name表空间 的名字一定要大写 */ select distinct s.owner from dba_segments s where s.tablespace_name ='TMS21';
select owner,table_name,tablespace_name from dba_tables where owner='用户名';
select * from all_tables where tablespace_name='表空间名'
select a.tablespace_name, a.bytes / 1024 / 1024 "sum MB", (a.bytes - b.bytes) / 1024 / 1024 "used MB", b.bytes / 1024 / 1024 "free MB", round(((a.bytes - b.bytes) / a.bytes) * 100, 2) "used%" 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;
--sql2 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
--包含临时表空间使用情况 select * from (Select a.tablespace_name, to_char(a.bytes / 1024 / 1024, '99,999.999') total_bytes, to_char(b.bytes / 1024 / 1024, '99,999.999') free_bytes, to_char(a.bytes / 1024 / 1024 - b.bytes / 1024 / 1024, '99,999.999') use_bytes, to_char((1 - b.bytes / a.bytes) * 100, '99.99') || '%' use from (select tablespace_name, sum(bytes) bytes from dba_data_files group by tablespace_name) a, (select tablespace_name, sum(bytes) bytes from dba_free_space group by tablespace_name) b where a.tablespace_name = b.tablespace_name union all select c.tablespace_name, to_char(c.bytes / 1024 / 1024, '99,999.999') total_bytes, to_char((c.bytes - d.bytes_used) / 1024 / 1024, '99,999.999') free_bytes, to_char(d.bytes_used / 1024 / 1024, '99,999.999') use_bytes, to_char(d.bytes_used * 100 / c.bytes, '99.99') || '%' use from (select tablespace_name, sum(bytes) bytes from dba_temp_files group by tablespace_name) c, (select tablespace_name, sum(bytes_cached) bytes_used from v$temp_extent_pool group by tablespace_name) d where c.tablespace_name = d.tablespace_name) order by tablespace_name ;
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;
可以通过以下三种方法实现表空间的扩容(M1,M2,M3)
单个数据文件最大不超过32G。
alter database datafile 'D:\APP\ADMINISTRATOR\ORADATA\XYSHARE\SGSHARE.DBF' resize 4096m;
select a.file# as "数据文件id", a.name as "数据文件路径", a.bytes / 1024 / 1024 as "当前数据文件大小(MB)", ceil(HWM * a.block_size) / 1024 / 1024 as "可调整至大小(MB)", (a.bytes - HWM * a.block_size) / 1024 / 1024 AS "释放空间大小(MB)", 'alter database datafile ''' || a.name || ''' resize ' || ceil(ceil(HWM * a.block_size) / 1024 / 1024) || 'M;' as "SQL语句" from v$datafile a, (SELECT file_id, MAX(block_id + blocks - 1) HWM FROM DBA_EXTENTS GROUP BY file_id) b where a.file# = b.file_id(+) And (a.bytes - HWM * a.block_size) > 0 and rownum < 30 order by "释放空间大小(MB)" desc
参考链接:ORACLE调整数据文件大小
ALTER DATABASE DATAFILE 'D:\ORACLE\PRODUCT\10.2.0\ORADATA\EDWTEST\APP03.DBF' AUTOEXTEND ON NEXT 100M MAXSIZE 20480M;
ALTER TABLESPACE SYSTEM ADD DATAFILE 'C:\APP\ORACLE\ORADATA\DFYYCDB\DATAFILE\O2_MF_SYSTEM_CWMNZ9XV_.DBF' size 7167M autoextend on ;
DROP TABLESPACE tablespace_name INCLUDING CONTENTS AND DATAFILES;
如果没有通过以上命令删除而直接删除了数据文件,将导致数据库无法打开。
Alter tablespace tablespace_name drop datafile file_name;
alter database tempfile '/home/oracle/temp01.dbf' drop including datafiles;
非归档模式使用:alter database datafile '...' offline drop; 归档模式使用: alter database datafile '...' offline; 说明: 1) 以上命令只是将该数据文件OFFLINE,而不是在数据库中删除数据文件。该数据文件的信息在控制文件种仍存在。查询v$datafile,仍显示该文件。 2) 归档模式下offline和offline drop效果是一样的 3) offline后,存在此datafile上的对象将不能访问 4) noarchivelog模式下,只要online redo日志没有被重写,可以对这个文件recover后进行online操作 ———————————————— 原文链接:https://blog.csdn.net/junmail/article/details/5081714
--查看数据库表大小SQL select bytes,owner,segment_name from dba_segments where segment_type='TABLE' order by bytes desc; --查看aud$表大小SQL select bytes,owner,segment_name from dba_segments where segment_type='TABLE' and segment_name = 'AUD$';
--检查统计信息保存时间(默认应该是31天) select dbms_stats.get_stats_history_retention from dual; --如果31天将其改为7天 exec dbms_stats.alter_stats_history_retention(7); --验证是否修改成功 select dbms_stats.get_stats_history_retention from dual;
补充:Oracle 10g中快照会保留7天,11g的快照保留8天,超出会自动删除。AWR快照可以从其他数据库导入,而这部分数据会保存时间极长。有时候也会遇到自动快照不能自动收集,而手工创建的快照又可以成功,对于这种情况就需要把以前的快照清理掉。
删除AWR有两种方式进行删除:dbms_workload_repository,dbms_swrf_internal。dbms_workload_repository可以删除本地和其他数据库的快照,可以选择不同的快照来进行删除;dbms_swrf_internal只能对其他数据库的快照来进行操作,会把所有的快照直接干掉。
使用dbms_workload_repository包删除:
select dbid, retention from dba_hist_wr_control; select min(snap_id), max(snap_id)from dba_hist_snapshot where dbid = '得到的dbid值'; exec dbms_workload_repository.drop_snapshot_range('得到的min(snap_id)值','得到的max(snap_id)值','得到的dbid值'); select * from dba_hist_snapshot where dbid = '得到的dbid值';
使用dbm_swrf_internal包删除:
select dbid, retention from dba_hist_wr_control; select min(snap_id), max(snap_id)from dba_hist_snapshot where dbid = '得到的dbid值'; exec dbms_swrf_internal.unregister_database('得到的dbid值'); select * from dba_hist_snapshot where dbid = '得到的dbid值';