oracle實用sql(6)--tablespace/datafile resize

selectshen發表於2016-06-05

點選(此處)摺疊或開啟

  1. --tablespace size
  2. select a.tablespace_name,
  3.        round(a.bytes_alloc / 1024 / 1024 /1024,2) Size_G,
  4.        round(nvl(b.bytes_free, 0) / 1024 / 1024 /1024,2) Free_G,
  5.        round((a.bytes_alloc - nvl(b.bytes_free, 0)) / 1024 / 1024 /1024,2) Used_G,
  6.        round((nvl(b.bytes_free, 0) / a.bytes_alloc) * 100) Free_Percent,
  7.        100 - round((nvl(b.bytes_free, 0) / a.bytes_alloc) * 100) Usage_Percent,
  8.        round(maxbytes/ 1024 / 1024 /1024) Max_G
  9. from ( select f.tablespace_name,
  10.                sum(f.bytes) bytes_alloc,
  11.                sum(decode(f.autoextensible, 'YES',f.maxbytes,'NO', f.bytes)) maxbytes
  12.         from dba_data_files f
  13.         group by tablespace_name) a,
  14.       ( select f.tablespace_name,
  15.                sum(f.bytes) bytes_free
  16.         from dba_free_space f
  17.         group by tablespace_name) b
  18. where a.tablespace_name = b.tablespace_name (+)
  19. union all
  20. select h.tablespace_name,
  21.        round(sum(h.bytes_free + h.bytes_used) / 1024 / 1024 /1024,2) Size_G,
  22.        round(sum((h.bytes_free + h.bytes_used) - nvl(p.bytes_used, 0)) / 1024 / 1024 /1024,2) Free_G,
  23.        round(sum(nvl(p.bytes_used, 0))/ 1024 / 1024 /1024,2) Used_G,
  24.        round((sum((h.bytes_free + h.bytes_used) - nvl(p.bytes_used, 0)) / sum(h.bytes_used + h.bytes_free)) * 100) Pct_Free,
  25.        100 - round((sum((h.bytes_free + h.bytes_used) - nvl(p.bytes_used, 0)) / sum(h.bytes_used + h.bytes_free)) * 100) pct_used,
  26.        round(sum(decode(f.autoextensible, 'YES', f.maxbytes, 'NO', f.bytes) / 1024 / 1024 /1024),2) Max_G
  27. from sys.v_$TEMP_SPACE_HEADER h,
  28.        sys.v_$Temp_extent_pool p,
  29.        dba_temp_files f
  30. where p.file_id(+) = h.file_id
  31. and p.tablespace_name(+) = h.tablespace_name
  32. and f.file_id = h.file_id
  33. and f.tablespace_name = h.tablespace_name
  34. group by h.tablespace_name
  35. ORDER BY 1;

  36. --tablespace can resize
  37. select fs.tablespace_name, fs.file_id, round(nvl(sum(fs.bytes), 0) / 1048576) can_shrink_by
  38. from dba_free_space fs,
  39.       (
  40.       select e1.tablespace_name, e1.file_id, e1.block_id + e1.blocks last_block
  41.        from dba_extents e1,
  42.                 (Select tablespace_name, file_id, max(block_id) block_id
  43.                  from dba_extents
  44.                  group by tablespace_name, file_id) e2
  45.       where e1.tablespace_name = e2.tablespace_name
  46.       and e1.file_id = e2.file_id
  47.       and e1.block_id = e2.block_id
  48.       ) e
  49. where fs.tablespace_name = e.tablespace_name
  50. and fs.file_id = e.file_id
  51. and fs.block_id >= e.last_block
  52. group by fs.tablespace_name, fs.file_id;


  53. --datafile size
  54. SELECT t.tablespace_name, 'Datafile' file_type,
  55.        t.status tablespace_status, d.status file_status,
  56.        round((d.bytes - NVL(f.sum_bytes, 0)) / 1024 / 1024 /1024,2) used_G,
  57.        round(NVL(f.sum_bytes, 0) / 1024 / 1024 /1024,2) free_G,
  58.        t.initial_extent,t.next_extent, t.min_extents, t.max_extents, t.pct_increase,
  59.        d.file_name, d.file_id, d.autoextensible, d.maxblocks,
  60.        round(d.maxbytes / 1024 / 1024 /1024,2) max_G,
  61.        nvl(d.increment_by,0) increment_by, t.block_size
  62.  FROM (SELECT tablespace_name, file_id, SUM(bytes) sum_bytes
  63.        FROM DBA_FREE_SPACE
  64.        GROUP BY tablespace_name, file_id) f,
  65.       DBA_DATA_FILES d,
  66.       DBA_TABLESPACES t
  67. WHERE t.tablespace_name = d.tablespace_name
  68. AND f.tablespace_name(+) = d.tablespace_name
  69. AND f.file_id(+) = d.file_id
  70. GROUP BY t.tablespace_name, d.file_name, d.file_id, t.initial_extent,
  71.          t.next_extent, t.min_extents, t.max_extents,
  72.          t.pct_increase, t.status, d.bytes, f.sum_bytes, d.status,
  73.          d.AutoExtensible, d.maxblocks, d.maxbytes, d.increment_by, t.block_size
  74. UNION ALL
  75. SELECT h.tablespace_name,
  76.        'Tempfile',
  77.        ts.status,
  78.        t.status,
  79.        round(SUM(NVL(p.bytes_used, 0)) / 1024 / 1024 /1024,2) used_G,
  80.        round(SUM((h.bytes_free + h.bytes_used) - NVL(p.bytes_used, 0)) / 1024 / 1024 /1024,2) free_G,
  81.        -1,
  82.        -1,
  83.        -1,
  84.        -1,
  85.        -1,
  86.        t.file_name,
  87.        t.file_id,
  88.        t.autoextensible, t.maxblocks,
  89.        round(t.maxbytes / 1024 / 1024 /1024,2) max_G,
  90.        nvl(t.increment_by, 0) increment_by, ts.block_size
  91. FROM sys.V_$TEMP_SPACE_HEADER h, sys.V_$TEMP_EXTENT_POOL p, sys.DBA_TEMP_FILES t, sys.dba_tablespaces ts
  92. WHERE p.file_id(+) = h.file_id
  93. AND p.tablespace_name(+) = h.tablespace_name
  94. AND h.file_id = t.file_id
  95. AND h.tablespace_name = t.tablespace_name
  96. and ts.tablespace_name = h.tablespace_name
  97. GROUP BY h.tablespace_name, t.status, t.file_name, t.file_id, ts.status,
  98.        t.autoextensible, t.maxblocks, t.maxbytes, t.increment_by, ts.block_size
  99. ORDER BY 1, 5 DESC;

  100. --datafile can resize
  101. Select sum(bytes)/1024/1024 Can_Resize_M
  102. from dba_free_space
  103. where file_id = 4
  104. and block_id >= nvl((Select (block_id + (bytes/8192))
  105.                     from dba_extents
  106.                     where block_id = (Select max(block_id)
  107.                                       from dba_extents
  108.                                       where file_id = 4
  109.                                       )
  110.                     and file_id = 4), 0);
  111. --tempfile can resize
  112. Select round(sum(bytes) / 1048576) Can_Resize_M
  113. from sys.v_$TEMP_EXTENT_MAP
  114. where file_id = 1
  115. and block_id >= nvl((Select block_id + (bytes / 4096)
  116.                      from sys.v_$TEMP_EXTENT_MAP
  117.                      where block_id = (Select max(block_id)
  118.                                        from sys.v_$TEMP_EXTENT_MAP
  119.                                        where file_id = 1
  120.                                        and owner <> 0)
  121.                      and file_id = 1), 0);


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28539951/viewspace-2114594/,如需轉載,請註明出處,否則將追究法律責任。

相關文章