ORACLE-查询表和表空间的基本情况

梦里梦外; 2023-01-18 02:28 271阅读 0赞
  1. /* 查询表分配的空间大小,不管空间是否被占用*/
  2. -- 单位:MB
  3. select segment_name,bytes/1024/1024 "分配空间(MB)"
  4. from user_segments
  5. where segment_type='TABLE'
  6. AND SEGMENT_NAME='TABLE_NAME';
  7. /*查看表实际占用空间大小*/
  8. -- 单位:MB
  9. select segment_name,sum(bytes)/1024/1024 AS "实际使用量(MB)"
  10. from User_Extents
  11. where segment_name='TABLE_NAME'
  12. Group By Segment_Name;
  13. /*查看表空间大小*/
  14. -- 单位MB
  15. Select Tablespace_Name,Sum(bytes)/1024/1024
  16. From dba_Segments Group By Tablespace_Name;
  17. /*查看剩余表空间大小*/
  18. -- 单位MB
  19. SELECT tablespace_name 表空间,sum(blocks*8192/1000000) 剩余空间M
  20. FROM dba_free_space GROUP BY tablespace_name;
  21. /*查看所有表空间总体空间*/
  22. -- 单位MB
  23. select b.name,sum(a.bytes/1000000)总空间
  24. from v$datafile a,v$tablespace b
  25. where a.ts#=b.ts# group by b.name;
  26. /*表空间大小及使用率*/
  27.  select
  28.   a.a1 表空间名称,
  29.   c.c2 类型,
  30.   c.c3 区管理,
  31.   b.b2/1024/1024 表空间大小M,
  32.   (b.b2-a.a2)/1024/1024 已使用M,
  33.   substr((b.b2-a.a2)/b.b2*100,1,5) 利用率
  34.   from
  35.   (select tablespace_name a1, sum(nvl(bytes,0)) a2 from dba_free_space group by tablespace_name) a,
  36.   (select tablespace_name b1,sum(bytes) b2 from dba_data_files group by tablespace_name) b,
  37.   (select tablespace_name c1,contents c2,extent_management c3 from dba_tablespaces) c
  38.   where a.a1=b.b1 and c.c1=b.b1;
  39. /*数据文件大小及使用率*/
  40. select
  41.   b.file_name 物理文件名,
  42.   b.tablespace_name 表空间,
  43.   b.bytes/1024/1024 大小M,
  44.   (b.bytes-sum(nvl(a.bytes,0)))/1024/1024 已使用M,
  45.   substr((b.bytes-sum(nvl(a.bytes,0)))/(b.bytes)*100,1,5) 利用率
  46.   from dba_free_space a,dba_data_files b
  47.   where a.file_id=b.file_id
  48.   group by b.tablespace_name,b.file_name,b.bytes
  49.   order by b.tablespace_name;
  50. /*临时表空间的名称、大小、数据文件*/
  51. select
  52.   a.talbespace_name 表空间名称,
  53.   b.bytes 大小bytes,
  54.   b.file_name 数据文件名
  55.   from dba_tablespaces a, dba_data_files b
  56.   Where a.talbespace_name=b.talbespace_name and a.contents='TEMPORARY';
  57. /*查询oracle表空间的使用情况*/
  58. select b.file_id 文件ID,
  59.   b.tablespace_name 表空间,
  60.   b.file_name 物理文件名,
  61.   b.bytes 总字节数,
  62.   (b.bytes-sum(nvl(a.bytes,0))) 已使用,
  63.   sum(nvl(a.bytes,0)) 剩余,
  64.   sum(nvl(a.bytes,0))/(b.bytes)*100 剩余百分比
  65.   from dba_free_space a,dba_data_files b
  66.   where a.file_id=b.file_id
  67.   group by b.tablespace_name,b.file_name,b.file_id,b.bytes
  68.   order by b.tablespace_name;
  69. /*查询oracle系统用户的默认表空间和临时表空间*/
  70. select default_tablespace,temporary_tablespace from dba_users;
  71. /*查询单张表的使用情况*/
  72. select segment_name,bytes
  73. from dba_segments
  74. where segment_name = 'RE_STDEVT_FACT_DAY' and owner = USER;
  75. /*查询所有用户表使用大小的前三十名*/
  76. select * from (select segment_name,bytes from dba_segments where owner = USER order by bytes desc )
  77. where rownum <= 30;
  78. /*查询当前用户默认表空间的使用情况*/
  79. select tablespacename,sum(totalContent),sum(usecontent),sum(sparecontent),avg(sparepercent)
  80. from
  81. (
  82. SELECT b.file_id as id,b.tablespace_name as tablespacename,b.bytes as totalContent,(b.bytes-sum(nvl(a.bytes,0))) as usecontent,sum(nvl(a.bytes,0)) as sparecontent,sum(nvl(a.bytes,0))/(b.bytes)*100 as sparepercent
  83. FROM dba_free_space a,dba_data_files b
  84. WHERE a.file_id=b.file_id and b.tablespace_name = (select default_tablespace from dba_users where username = user)
  85. group by b.tablespace_name,b.file_name,b.file_id,b.bytes
  86. )
  87. GROUP BY tablespacename;
  88. /*查询用户表空间的表*/
  89. select * from user_tables;

发表评论

表情:
评论列表 (有 0 条评论,271人围观)

还没有评论,来说两句吧...

相关阅读