zoukankan      html  css  js  c++  java
  • oracle收集统计信息

    ----小于500M的表每天收集全表统计信息

    select 'exec dbms_stats.gather_table_stats(ownname=>''' || owner || '''' ||
           ',tabname=>''' || table_name || '''' ||
           ',estimate_percent=>100,method_opt=>''' ||
           'FOR all columns size repeat''' || ',cascade=> true);'
      from dba_tab_statistics
     where (owner, table_name) in
           (SELECT owner, table_name
              FROM DBA_TABLES
             WHERE OWNER in  ('TS', 'SS')
               AND (owner, TABLE_NAME) in
                   (select owner, segment_name
                      from dba_segments
                     where owner in  ('TS', 'SS')
                     group by owner, segment_name
                    having sum(bytes) / 1024 / 1024 < 500)
               and partitioned != 'YES')
       and (stale_stats = 'YES' or last_analyzed is null)
       AND OBJECT_TYPE = 'TABLE';

    ----非分区表大于500M,收集统计信息脚本(每天收集30%的统计信息)

    select 'exec dbms_stats.gather_table_stats(ownname=>''' || owner || '''' ||
           ',tabname=>''' || table_name || '''' ||
           ',estimate_percent=>30,method_opt=>''' ||
           'FOR all columns size repeat''' || ',degree => 4,cascade=> true);'
      from dba_tab_statistics
     where (owner, table_name) in
           (SELECT owner, table_name
              FROM DBA_TABLES
             WHERE OWNER in ('TS', 'SS')
               AND (owner, TABLE_NAME) in
                   (select owner, segment_name
                      from dba_segments
                     where owner in ('TS', 'SS')
                     group by owner, segment_name
                    having sum(bytes) / 1024 / 1024 >= 500)
               and partitioned != 'YES')
       and (stale_stats = 'YES' or last_analyzed is null)
       AND OBJECT_TYPE = 'TABLE';

    ------分区表--每天copy上一个分区的统计信息(自增长分区表)

    select 'EXEC DBMS_STATS.COPY_TABLE_STATS (' || '''' || table_owner || '''' || ',' || '''' ||
           table_name || '''' || ',' || '''' || 'SYS_P402' || '''' || ',' || '''' ||
           partition_name || '''' || ', FORCE=>TRUE);'
      from dba_tab_partitions
     where table_name = 'T_SS_AC_IM'
       and partition_position =
           (select max(partition_position)
              from dba_tab_partitions
             where table_name = 'T_SS_AC_IM');

    ------copy分区表统计信息脚本

    [oracle@test1 ~]$ cat /home/oracle/copy_statics.sh

    #!/bin/bash

    source /home/oracle/.bash_profile

    SDATE=$(date  +%Y%m)

    TDATE=$(date -d 'next-month' +%Y%m)

    SPNAME="P"${SDATE}

    TPNAME="P"${TDATE}

    exec >> /home/oracle/copy_statics`date +%y%m%d%H`.log

    sqlplus / as sysdba << EOF

    set timing on

    EXEC DBMS_STATS.UNLOCK_TABLE_STATS ('TS','T_TS_TR');

    EXEC DBMS_STATS.UNLOCK_TABLE_STATS ('TS','T_TS_TR_ND');

    EXEC DBMS_STATS.COPY_TABLE_STATS ('TS', 'T_TS_CUP_Z_TR', '$SPNAME', '$TPNAME',FORCE=>TRUE);

    EXEC DBMS_STATS.COPY_TABLE_STATS ('TS', 'T_TS_TR', '$SPNAME', '$TPNAME', FORCE=>TRUE);

    EXEC DBMS_STATS.COPY_TABLE_STATS ('TS', 'T_TS_TR_ND', '$SPNAME', '$TPNAME', FORCE=>TRUE);

    EXEC DBMS_STATS.LOCK_TABLE_STATS ('TS','T_TS_TR');

    EXEC DBMS_STATS.LOCK_TABLE_STATS ('TS','T_TS_TR_ND');

    exit;

    EOF

  • 相关阅读:
    使用 Istio 进行 JWT 身份验证(充当 API 网关)
    DNS 私有域的选择:internal.xxx.com/lan.xxx.com 还是 xxx.local/xxx.srv?
    「Bug」K8s 节点的 IP 地址泄漏,导致 IP 被耗尽
    Linux网络学习笔记(二):域名解析(DNS)——以 CoreDNS 为例
    Linux 发行版的选用(服务器和个人桌面)
    「Bug」VMware 虚拟机的关机测试中,Ubuntu 明显比 CentOS 慢
    VMware vSphere :服务器虚拟化
    「Bug」ubuntu 使用国内 apt 源构建 docker 时提示 hash 不匹配
    留言板
    Idea 自定义快捷代码输入 如syso => System.out.println()
  • 原文地址:https://www.cnblogs.com/ss-33/p/8706769.html
Copyright © 2011-2022 走看看