[自制工具]批量后台更新统计信息

2021年12月15日 753点热度 0人点赞 0条评论

有时需要批量收集数据库的统计信息,如在大量数据迁移或大量数据更新以后,但是收集的时间可能会较长,为了避免网络中断等意外情况可能引起的麻烦,今天调试了这个小脚本,可以分用户批量执行,同时记录执行时间等日志信息,比较实用。
执行方式:
sh /tmp/gather_schema_stats.sh syspasswd ip port service "schema1 schema2" degree&
详见以下代码:

cat > /tmp/gather_schema_stats.sh << EOF2
#!/bin/bash

# Usage:
# sh /tmp/gather_schema_stats.sh syspasswd ip port service "schema1 schema2" degree&

gather_schema_stats() {
date
sqlplus /nolog << EOF
connect sys/\$1@\$2:\$3/\$4 as sysdba
set timing on
exec dbms_stats.gather_schema_stats(ownname=>'\$5',options=>'GATHER AUTO',estimate_percent=>dbms_stats.auto_sample_size,method_opt=>'for all columns size auto',degree=>\$6,cascade=>TRUE);
exit
EOF
date
}

for SCHEMAS in \${5}
do
TIMESTAMP=\date "+%Y%m%d"\;LOG_FILE=/tmp/gather_schema_stats_\${TIMESTAMP}_\${SCHEMAS}.log
gather_schema_stats \${1} \${2} \${3} \${4} \${SCHEMAS} \$6 > \${LOG_FILE} 2>&1
done
EOF2

最后生成的脚本及日志如下所示:

ll /tmp
total 36
-rw-r----- 1 oracle oinstall  409 Dec 15 19:17 gather_schema_stats_20211215_CM.log
-rw-r----- 1 oracle oinstall  409 Dec 15 19:17 gather_schema_stats_20211215_CUSTCARE.log
-rw-r----- 1 oracle oinstall  409 Dec 15 19:18 gather_schema_stats_20211215_DW_TOTALCC.log
-rw-r----- 1 oracle oinstall  409 Dec 15 19:18 gather_schema_stats_20211215_OTHER_USER.log
-rw-r----- 1 oracle oinstall  409 Dec 15 19:18 gather_schema_stats_20211215_REPORT.log
-rw-r----- 1 oracle oinstall  409 Dec 15 19:18 gather_schema_stats_20211215_SXGIS.log
-rw-r----- 1 oracle oinstall  409 Dec 15 19:18 gather_schema_stats_20211215_TOTALCC_FC.log
-rw-r----- 1 oracle oinstall  600 Dec 15 19:17 gather_schema_stats.sh

值得注意的是,收集统计信息的各个参数配置,采用了最佳实践配置,可以放心参考使用。

liking

这个人很懒,什么都没留下

文章评论