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

 

posted @   钱若梨花落  阅读(892)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示