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

  • 相关阅读:
    Linux Bash
    grep 及正则表达式
    Linux 文件系统
    Linux 操作系统基础
    常见的磁盘I/O和网络I/O优化技巧
    NIO的工作方式
    网络I/O 工作机制
    spring Boot环境下dubbo+zookeeper的一个基础讲解与示例
    深入分析Java I/O 工作机制
    CDN工作机制和负载均衡
  • 原文地址:https://www.cnblogs.com/ss-33/p/8706769.html
Copyright © 2011-2022 走看看