设为首页 收藏本站
查看: 726|回复: 0

[经验分享] Oracle 查询表空间使用情况(转)

[复制链接]

尚未签到

发表于 2016-7-24 11:59:01 | 显示全部楼层 |阅读模式
  --查询表空间使用情况
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
ORDER BY 1

--查询表空间的free space
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, sum(bytes) / 1024 / 1024 as MB
from dba_data_files
group by tablespace_name;

--查询表空间使用率
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;



1.建立表空间:create tablespace test datafile '/u01/test.dbf'  size 10M uniform size 128k
  #指定区尺寸为128k ,块大小为默认8K  
  #大文件表空间 create bigfile tablespace big_tbs datafile  '/u01/big_tbs.dbf ' size 100G

2.建非标准表show parameter db alter system set db_2k_cache_size=10M create tablespace test datafile '/u01/test.dbf' size 10M blocksize 2K uniform size 128k  
  #常见错误
  SQL> alter system set db_2k_cache_size=2M; alter system set db_2k_cache_size=2M ERROR at line 1: ORA-02097: parameter cannot be modified because specified value is invalid ORA-00384: Insufficient memory to grow cache
  #解决
  SQL> alter system set sga_max_size=400M scope=spfile; SQL> shutdown immediate; SQL> startup SQL> alter system set db_2k_cache_size=10M; System altered.

3.查看区大小与块大小#区大小 conn y / 123 create table t(i number) tablespace test; Insert into t values(10) select bytes/1024 from user_segments where segment_name=upper('t');
  #块大小 Show parameter block(默认64K)
  #非标准表空间的blocksize SQL> select * from v$dbfile; SQL> select name,block_size,status from v$datafile; SQL> select block_size from v$datafile where file#=14;

4.删除表空间drop tablespace test including contents and datafiles

5.查表空间:#查数据文件 select * from v$dbfile; #所有表空间 select * from v$tablespace;
  #表空间的数据文件 select file_name,tablespace_name from dba_data_files;

6.建立undo表空间create undo tablespace undotbs01 datafile '/u01/undotbs01.dbf' size 5M;
  #切换到新建的undo表空间 alter system set undo_tablespace=undotbs01;

7.建立临时表空间create temporary tablespace temp_data tempfile '/u01/temp.db'  size 5M; create bigfile temporary tablespace bigtem tempfile '/u01/bigtemp.db'  size 5M;

8.改变表空间状态
(0.)查看状态
  #表空间状态 select tablespace_name,block_size,status from dba_tablespaces;
  #数据文件状态 select name,block_size,status from v$datafile;

(1.)表空间脱机alter tablespace test offline
  #如果意外删除了数据文件 alter tablespace test offline for recover

(2.)表空间联机alter tablespace test online

(3.)数据文件脱机select * from v$dbfile; alter database datafile 3 offline

(4.)数据文件联机recover datafile 3; alter database datafile 3 online;

(5.)使表空间只读alter tablespace test read only

(6.)使表空间可读写alter tablespace test read write;

9.扩展表空间#首先查看表空间的名字和所属文件及空间 select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_space from dba_data_files order by tablespace_name;   #三种扩展方法
  1.alter tablespace test add datafile '/u01/test02.dbf' size 10M(自动加一个datafile)
  2.alter database datafile '/u01/test.dbf' resize 20M;
  3.alter database datafile '/u01/test.dbf' autoextend on next 10M maxsize 1G;  
  #设定后查看表空间信息
  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;

10.移动表空间的数据文件
  #先确定数据文件据在表空间
  SQL>select tablespace_name,file_name from dba_data_files where file_name='/u01/test.dbf';
  #open状态
  SQL>alter tablespace test offline; SQL>host move /u01/test.dbf  /u01/oracle/test.dbf; SQL>alter tablespace test rename datafile '/u01/test.dbf' to '/u01/oracle/test.dbf'; SQL>alter tablespace test offline;  
  #mount状态 SQL>shutdown immediate; SQL>startup mount SQL>host move /u01/test.dbf  /u01/oracle/test.dbf; SQL>alter database rename file '/u01/test.dbf' to '/u01/oracle/test.dbf';

11.表空间和数据文件常用的数据字典与动态性能视图v$dbfile v$datafile dba_segments user_segments dba_data_files v$tablespace dba_tablespaces user_tablespaces

表空间扩充参考:http://www.dbmotive.com/oracle_error_codes.php?errcode=1658
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-248609-1-1.html 上篇帖子: SQL语句优化规律总结(ORACLE) 下篇帖子: Oracle数据导入导出IMP/EXP [转]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表