运维联盟俱乐部

 找回密码
 立即注册
查看: 348|回复: 0

[技术专题] fragment

[复制链接]
  • TA的每日心情
    开心
    2023-8-9 11:05
  • 发表于 2023-12-1 10:25:32 | 显示全部楼层 |阅读模式
    -- 数据库总空间
    1. set linesize 160 pagesize 999;
    2. select sum(bytes)/(1024*1024*1024) from dba_data_files;
    3. select sum(bytes)/(1024*1024*1024) from dba_segments;
    4. select sum(bytes)/(1024*1024*1024) from dba_free_space;
    复制代码

    -- FSFI值
    1. select a.tablespace_name,
    2.        sqrt(max(a.blocks) / sum(a.blocks)) * (100 / sqrt(sqrt(count(a.blocks)))) FSFI
    3.   from dba_free_space a, dba_tablespaces b
    4.   where a.tablespace_name = b.tablespace_name
    5. and b.contents not in ('TEMPORARY','UNDO')
    6. group by a.tablespace_name
    7. order by FSFI;
    复制代码

    -- 按表空间显示连续的空闲空间
    1. ========
    2. Script : tfstsfgm
    3. ========
    4. SET ECHO off
    5. REM NAME:TFSTSFRM.SQL
    6. REM USAGE:"@path/tfstsfgm"
    7. REM ------------------------------------------------------------------------
    8. REM REQUIREMENTS:
    9. REM    SELECT ON DBA_FREE_SPACE
    10. REM ------------------------------------------------------------------------
    11. REM PURPOSE:
    12. REM    The following is a script that will determine how many extents
    13. REM    of contiguous free space you have in Oracle as well as the  
    14. REM total amount of free space you have in each tablespace. From  
    15. REM    these results you can detect how fragmented your tablespace is.  
    16. REM   
    17. REM    The ideal situation is to have one large free extent in your  
    18. REM    tablespace. The more extents of free space there are in the  
    19. REM    tablespace, the more likely you  will run into fragmentation  
    20. REM    problems. The size of the free extents is also  very important.  
    21. REM    If you have a lot of small extents (too small for any next   
    22. REM    extent size) but the total bytes of free space is large, then  
    23. REM    you may want to consider defragmentation options.  
    24. REM ------------------------------------------------------------------------
    25. REM DISCLAIMER:
    26. REM    This script is provided for educational purposes only. It is NOT  
    27. REM    supported by Oracle World Wide Technical Support.
    28. REM    The script has been tested and appears to work as intended.
    29. REM    You should always run new scripts on a test instance initially.
    30. REM ------------------------------------------------------------------------
    31. REM Main text of script follows:

    32. create table SPACE_TEMP (   
    33. TABLESPACE_NAME        CHAR(30),   
    34. CONTIGUOUS_BYTES       NUMBER)   
    35. /   
    36.    
    37. declare   
    38.   cursor query is select *   
    39.           from dba_free_space   
    40.                   order by tablespace_name, block_id;   
    41.   this_row        query%rowtype;   
    42.   previous_row    query%rowtype;   
    43. total           number;   
    44.    
    45. begin   
    46.   open query;   
    47.   fetch query into this_row;   
    48.   previous_row := this_row;   
    49.   total := previous_row.bytes;   
    50.   loop   
    51. fetch query into this_row;   
    52.      exit when query%notfound;   
    53.      if this_row.block_id = previous_row.block_id + previous_row.blocks then   
    54.         total := total + this_row.bytes;   
    55.         insert into SPACE_TEMP (tablespace_name)   
    56.                   values (previous_row.tablespace_name);   
    57.      else   
    58.         insert into SPACE_TEMP values (previous_row.tablespace_name,   
    59.                total);   
    60.         total := this_row.bytes;   
    61.      end if;   
    62. previous_row := this_row;   
    63.   end loop;   
    64.   insert into SPACE_TEMP values (previous_row.tablespace_name,   
    65.                            total);   
    66. end;   
    67. /   
    68.    
    69. set pagesize 60   
    70. set newpage 0   
    71. set echo off   
    72. ttitle center 'Contiguous Extents Report'  skip 3   
    73. break on "TABLESPACE NAME" skip page duplicate   
    74. spool contig_free_space.lis   
    75. rem   
    76. column "CONTIGUOUS BYTES"       format 999,999,999,999   
    77. column "COUNT"                  format 999999   
    78. column "TOTAL BYTES"            format 999,999,999,999   
    79. column "TODAY"   noprint new_value new_today format a1   
    80. rem   
    81. select TABLESPACE_NAME  "TABLESPACE NAME",   
    82.        CONTIGUOUS_BYTES "CONTIGUOUS BYTES"   
    83. from SPACE_TEMP   
    84. where CONTIGUOUS_BYTES is not null   
    85. order by TABLESPACE_NAME, CONTIGUOUS_BYTES desc;   
    86.    
    87. select tablespace_name, count(*) "# OF EXTENTS",   
    88.          sum(contiguous_bytes) "TOTAL BYTES"   
    89. from space_temp   
    90. group by tablespace_name;   
    91.    
    92. spool off   
    93.    
    94. drop table SPACE_TEMP   
    95. /
    复制代码

    -- 普通表碎片评估方法
    1. col frag format 99.99
    2. col owner format a20;
    3. col table_name format a30;
    4. select * from (
    5. select a.owner,
    6.        a.table_name,
    7.        a.num_rows,
    8.        a.avg_row_len * a.num_rows space,
    9.        b.bytes,
    10.        (a.avg_row_len * a.num_rows / b.bytes) frag,
    11.        to_char(a.last_analyzed,'YYYY-MM-DD')
    12.   from dba_tables a, dba_segments b
    13. where a.table_name = b.segment_name
    14. and a.owner= b.owner
    15.    and a.owner not in
    16.        ('SYS', 'SYSTEM', 'OUTLN', 'DMSYS', 'TSMSYS', 'DBSNMP', 'WMSYS',
    17.         'EXFSYS', 'CTXSYS', 'XDB', 'OLAPSYS', 'ORDSYS', 'MDSYS', 'SYSMAN')
    18.   and (a.avg_row_len * a.num_rows / b.bytes) < 0.7
    19. --  and PARTITIONED='NO'
    20.   order by b.bytes desc)
    21. where rownum <= 50;
    复制代码

    -- 单表碎片情况
    1. select a.owner,
    2.        a.table_name,
    3.        a.num_rows,
    4.        a.avg_row_len * a.num_rows space,
    5.        b.bytes,
    6.        (a.avg_row_len * a.num_rows / b.bytes) frag,
    7.        to_char(a.last_analyzed,'YYYY-MM-DD')
    8.   from dba_tables a, dba_segments b
    9. where a.table_name = b.segment_name
    10. and a.owner= b.owner
    11.    and a.owner not in
    12.        ('SYS', 'SYSTEM', 'OUTLN', 'DMSYS', 'TSMSYS', 'DBSNMP', 'WMSYS',
    13.         'EXFSYS', 'CTXSYS', 'XDB', 'OLAPSYS', 'ORDSYS', 'MDSYS', 'SYSMAN')
    14.   and PARTITIONED='NO'
    15.   and a.table_name='TF_B_TRADE_BOOKING'
    16.   and a.owner='UCR_CRM1'
    17.   order by b.bytes desc
    18. /
    复制代码

    -- 分区表碎片评估方法
    1. col frag format 99.99
    2. col table_owner format a20;
    3. col table_name format a30;
    4. col partition_name format a15;

    5. select * from (
    6. select a.table_owner,
    7.        a.table_name,
    8.        a.partition_name,
    9.        a.num_rows,
    10.        a.avg_row_len * a.num_rows space,
    11.        b.bytes,
    12.        (a.avg_row_len * a.num_rows / b.bytes) frag,
    13.        to_char(a.last_analyzed,'YYYY-MM-DD')
    14.   from dba_tab_partitions a, dba_segments b
    15. where a.table_name = b.segment_name
    16. and a.table_owner= b.owner
    17. and a.partition_name = b.PARTITION_NAME
    18.    and a.table_owner not in
    19.        ('SYS', 'SYSTEM', 'OUTLN', 'DMSYS', 'TSMSYS', 'DBSNMP', 'WMSYS',
    20.         'EXFSYS', 'CTXSYS', 'XDB', 'OLAPSYS', 'ORDSYS', 'MDSYS', 'SYSMAN','SH')
    21.   and (a.avg_row_len * a.num_rows / b.bytes) < 0.7
    22.   and b.segment_type='TABLE PARTITION'
    23.   order by b.bytes desc)
    24. where rownum <= 50;
    复制代码

    -- 单个分区表碎片情况
    1. select a.table_owner,
    2.        a.table_name,
    3.        a.partition_name,
    4.        a.num_rows,
    5.        a.avg_row_len * a.num_rows space,
    6.        b.bytes,
    7.        (a.avg_row_len * a.num_rows / b.bytes) frag,
    8.        to_char(a.last_analyzed,'YYYY-MM-DD')
    9.   from dba_tab_partitions a, dba_segments b
    10. where a.table_name = b.segment_name
    11. and a.table_owner= b.owner
    12. and a.partition_name = b.PARTITION_NAME
    13.   and b.segment_type='TABLE PARTITION'
    14.   and a.table_name='SERV'
    15.   and a.table_owner='USER_PRODUCT'
    16.   order by 1,2,3;
    复制代码

    -- 索引碎片评估方法
    1. col tablespace_name format a20;
    2. col owner format a10;
    3. col index_name format a30;
    4. select *
    5.   from (select id.tablespace_name,
    6.                id.owner,
    7.                id.index_name,
    8.                id.blevel,
    9.                sum(sg.bytes) / 1024 / 1024 "size(M)",
    10.                sg.blocks,
    11.                sg.extents
    12.           from dba_indexes id, dba_segments sg
    13.          where id.owner = sg.owner
    14.            and id.index_name = sg.segment_name
    15.            and id.tablespace_name = sg.tablespace_name
    16.            and id.owner not in
    17.                ('SYS', 'SYSTEM', 'USER', 'DBSNMP', 'ORDSYS', 'OUTLN')
    18.            and sg.extents > 100
    19.            and id.blevel >= 3
    20.          group by id.tablespace_name,
    21.                   id.owner,
    22.                   id.index_name,
    23.                   id.blevel,
    24.                   sg.blocks,
    25.                   sg.extents
    26.         having sum(sg.bytes) / 1024 / 1024 > 100
    27.         order by sum(sg.bytes) / 1024 / 1024 desc )
    28. where rownum <= 50;
    复制代码

    -- 单索引碎片情况
    1. select id.tablespace_name,
    2.        id.owner,
    3.        id.index_name,
    4.        id.blevel,
    5.        sum(sg.bytes) / 1024 / 1024,
    6.        sg.blocks,
    7.        sg.extents
    8.   from dba_indexes id, dba_segments sg
    9. where id.owner = sg.owner
    10.    and id.index_name = sg.segment_name
    11.    and id.tablespace_name = sg.tablespace_name
    12.    and id.owner ='UCR_CRM1'
    13.    and id.index_name = 'PK_TF_B_TRADE_4G_VAC_PRODUCT'
    14. group by id.tablespace_name,
    15.           id.owner,
    16.           id.index_name,
    17.           id.blevel,
    18.           sg.blocks,
    19.           sg.extents;
    复制代码

    -- 索引大小>80%表大小的索引列表
    1. select * from (
    2.          WITH Q AS (
    3.                  SELECT
    4.                         S.OWNER                  A_OWNER,
    5.                         TABLE_NAME               A_TABLE_NAME,
    6.                         INDEX_NAME               A_INDEX_NAME,
    7.                         INDEX_TYPE               A_INDEX_TYPE,
    8.                         I.PARTITIONED             A_PART,                                                                                                
    9.                         SUM(S.bytes) / 1048576   A_MB
    10.                    FROM DBA_SEGMENTS S,
    11.                         DBA_INDEXES  I
    12.                   WHERE S.OWNER = I.owner
    13.                     AND INDEX_NAME = SEGMENT_NAME
    14.                   GROUP BY S.OWNER, TABLE_NAME, INDEX_NAME, INDEX_TYPE,PARTITIONED
    15.                  HAVING SUM(S.BYTES) > 1048576 * 100
    16.          )
    17.          SELECT /*+ NO_QUERY_TRANSFORMATION(S) */
    18.                 A_OWNER                                    OWNER,
    19.                 A_TABLE_NAME                               TABLE_NAME,
    20.                 A_INDEX_NAME                               INDEX_NAME,
    21.                 A_INDEX_TYPE                               INDEX_TYPE,
    22.                A_PART                       PARTITIONEND,
    23.                A_MB                                       IND_MB,
    24.                 sum(bytes)/1024/1024               tab_MB
    25.           FROM  Q,
    26.                 dba_segments d
    27.           WHERE
    28.                 D.owner(+)= q.A_OWNER AND
    29.                 D.segment_NAME = q.A_TABLE_NAME
    30.          GROUP BY
    31.                 A_OWNER,
    32.                 A_TABLE_NAME,
    33.                 A_INDEX_NAME,
    34.                 A_INDEX_TYPE,
    35.                 A_PART,
    36.                 A_MB  HAVING A_MB>0.8*(SUM(bytes) / 1024/1024  )   
    37.          ORDER BY
    38.                 A_OWNER,
    39.                 A_TABLE_NAME,
    40.                 A_INDEX_NAME,
    41.                 A_INDEX_TYPE,
    42.                 A_MB DESC  
    43. ) where rownum <=50;
    复制代码





    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    运维联盟俱乐部 ( 冀ICP备19036648号 )

    GMT+8, 2024-5-10 22:01 , Processed in 0.053650 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

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