运维联盟俱乐部

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

[日常管理] q_compress_table

[复制链接]
  • TA的每日心情
    开心
    2023-8-9 11:05
  • 发表于 2022-9-28 10:18:45 | 显示全部楼层 |阅读模式
    1.当前使用压缩得表和分区
    1. select owner,sum(zs) from
    2. (select owner,count(*) zs
    3. from dba_tables
    4. where compression='ENABLED'
    5. group by owner
    6. union all
    7. SELECT table_owner ,count(*) zs
    8. from (select table_owner,table_name from dba_tab_partitions where compression='ENABLED' group by table_owner,table_name)
    9. group by table_owner
    10. union all
    11. SELECT table_owner ,count(*) zs
    12. from (select table_owner,table_name from dba_tab_subpartitions where compression='ENABLED' group by table_owner,table_name)
    13. group by table_owner
    14. )
    15. group by owner;
    复制代码

    2.待压缩得表和分区
    1. with st as
    2. (select owner,segment_name,'_NonPart' partition_name,ROUND(sum(bytes)/1048576/1024,2) sizegb
    3. from dba_segments
    4. where segment_type='TABLE'
    5.   and owner not in ('SYS','SYSAUX')
    6. group by owner,segment_name
    7. having sum(bytes)/1048576/1024>0.01),
    8. pt30g as
    9. (select owner,segment_name,partition_name,sum(bytes)/1048576/1024 sizegb
    10. from dba_segments
    11. where segment_type in ('TABLE PARTITION','TABLE SUBPARTITION')
    12.    and owner not in ('SYS','SYSAUX') and owner='LXJ'
    13.    and (owner,segment_name,partition_name) in
    14.        (select table_owner,TABLE_NAME,PARTITION_NAME
    15.         from dba_tab_partitions
    16.         where COMPRESSION<>'ENABLED'
    17.         group by table_owner,TABLE_NAME,PARTITION_NAME
    18.         union
    19.         select table_owner,TABLE_NAME,subPARTITION_NAME
    20.         from dba_tab_subpartitions
    21.         where COMPRESSION<>'ENABLED'
    22.         group by table_owner,TABLE_NAME,subPARTITION_NAME )
    23.   group by owner,segment_name,partition_name
    24.   having sum(bytes)/1048576/1024>0.01),
    25. pt50g as
    26. (
    27. select owner,segment_name
    28. from dba_segments
    29. where segment_type in ('TABLE PARTITION','TABLE SUBPARTITION')
    30.    and owner not in ('SYS','SYSAUX') and owner='LXJ'
    31.   group by owner,segment_name
    32.   having sum(bytes)/1048576/1024>0.02),
    33. pnc as
    34. (select table_owner,TABLE_NAME,PARTITION_NAME
    35. from dba_tab_partitions
    36. where COMPRESSION<>'ENABLED'
    37. group by table_owner,TABLE_NAME,PARTITION_NAME
    38. union
    39. select table_owner,TABLE_NAME,subPARTITION_NAME PARTITION_NAME
    40. from dba_tab_subpartitions
    41. where COMPRESSION<>'ENABLED'
    42. group by table_owner,TABLE_NAME,subPARTITION_NAME),
    43. t_lob as
    44. (
    45. select OWNER,TABLE_NAME
    46. from dba_tab_columns
    47. where owner not in ('SYS','SYSTEM')
    48.    and (data_type like '%LOB%' or data_type like '%LONG%')
    49. group by owner,table_name),
    50. stc as
    51. (select owner,table_name,COMPRESSION,COMPRESS_FOR from dba_tables where IOT_NAME is null),
    52. ptc as
    53. (select table_owner,table_name,partition_name,COMPRESSION,COMPRESS_FOR
    54. from dba_tab_partitions where table_owner not in ('SYS','SYSTEM')
    55. union
    56. select table_owner,table_name,subpartition_name partition_name,COMPRESSION,COMPRESS_FOR
    57. from dba_tab_subpartitions where table_owner not in ('SYS','SYSTEM')),
    58. dc as
    59. (select o.owner,o.object_name,o.SUBOBJECT_NAME,
    60.   sum(dhss.db_block_changes_total) bct,
    61.   sum(dhss.physical_writes_total) pwt,
    62.   sum(dhss.physical_writes_direct_total) pwdt
    63. from dba_hist_seg_stat dhss, DBA_HIST_SEG_STAT_OBJ o
    64. where dhss.obj#=o.obj#
    65.   and dhss.DATAOBJ#=o.DATAOBJ#
    66.   and o.owner not in ('SYS','SYSTEM')
    67.   and o.object_type like 'TABLE%'
    68. group by o.owner,o.object_name,o.SUBOBJECT_NAME)
    69. select st.owner,st.segment_name,st.partition_name,st.sizegb,stc.COMPRESSION ,stc.COMPRESS_FOR ,dc.bct,dc.pwt,dc.pwdt
    70. from st,stc,dc
    71. where (st.owner,st.segment_name) not in (select * from t_lob)
    72.   and st.owner=stc.owner
    73.   and st.segment_name=stc.table_name
    74.   and (st.owner,st.segment_name) not in (select owner,table_name from dba_tables
    75.                                          where COMPRESSION='ENABLED'
    76.                                          and owner not in('SYS','SYSTEM'))
    77.   and st.owner=dc.owner(+)
    78.   and st.segment_name=dc.object_name(+)
    79. union
    80. select pt30g.owner,pt30g.segment_name,pt30g.partition_name,pt30g.sizegb,
    81. ptc.COMPRESSION ,ptc.COMPRESS_FOR,dc.bct,dc.pwt,dc.pwdt
    82. from pt30g,ptc,dc
    83. where (pt30g.owner,pt30g.segment_name) not in (select owner,table_name from t_lob)
    84.   and pt30g.owner=ptc.table_owner
    85.   and pt30g.segment_name=ptc.table_name
    86.   and pt30g.partition_name=ptc.partition_name
    87.   and pt30g.owner=dc.owner(+)
    88.   and pt30g.segment_name=dc.object_name(+)
    89.   and pt30g.PARTITION_NAME=dc.SUBOBJECT_NAME(+)
    90. union
    91. select ds.owner,ds.segment_name,ds.partition_name,round(bytes/1048576/1024,2) sizegb,
    92. ptc.COMPRESSION ,ptc.COMPRESS_FOR,dc.bct,dc.pwt,dc.pwdt
    93. from dba_segments ds,pnc,ptc,dc
    94. where ds.owner not in ('SYS','SYSTEM')
    95.   and ds.segment_type in ('TABLE PARTITION','TABLE SUBPARTITION')
    96.   and ds.owner=pnc.table_owner
    97.   and ds.segment_name=pnc.table_name
    98.   and ds.partition_name=pnc.partition_name
    99.   and (ds.owner,ds.SEGMENT_NAME) in (select owner,segment_name from pt50g)
    100.   and ds.owner=ptc.table_owner
    101.   and ds.segment_name=ptc.table_name
    102.   and ds.partition_name=ptc.partition_name
    103.   and ds.owner=dc.owner(+)
    104.   and ds.segment_name=dc.object_name(+)
    105.   and ds.PARTITION_NAME=dc.SUBOBJECT_NAME(+);
    复制代码
    3.表压缩比评估
    1. set serveroutput on
    2. declare
    3. v_blkcnt_cmp pls_integer;
    4. v_blkcnt_uncmp pls_integer;
    5. v_row_cmp pls_integer;
    6. v_row_uncmp pls_integer;
    7. v_cmp_ratio number;
    8. v_comptype_str varchar2(60);

    9. begin
    10. DBMS_COMPRESSION.GET_COMPRESSION_RATIO (
    11.    scratchtbsname=>'TEST',  --评估中间表空间
    12.    ownname=>'LXJ',  --表所属的用户
    13.    tabname=>'TT',  --评估的目标表名字
    14.    partname=>NULL,   --分区名字
    15.    comptype=>2,    --压缩类型,参考6.1.6 说明
    16.    blkcnt_cmp => v_blkcnt_cmp,
    17.    blkcnt_uncmp => v_blkcnt_uncmp,
    18.    row_cmp => v_row_cmp,
    19.    row_uncmp => v_row_uncmp,
    20.    cmp_ratio => v_cmp_ratio,
    21.    comptype_str => v_comptype_str);
    22. dbms_output.put_line('Estimated Compression Ratio: '||to_char(v_cmp_ratio));
    23. dbms_output.put_line('Blocks used by compressed sample: '||to_char(v_blkcnt_cmp));
    24. dbms_output.put_line('Blocks used by uncompressed sample: '||to_char(v_blkcnt_uncmp));
    25. end;
    26. /
    复制代码








    回复

    使用道具 举报

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

    本版积分规则

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

    GMT+8, 2024-5-17 16:51 , Processed in 0.046231 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

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