天天看點

[自制工具]批量背景更新統計資訊

Oracle資料庫有時需要批量收集資料庫的統計資訊,如在大量資料遷移或大量資料更新以後,但是收集的時間可能會較長,為了避免網絡中斷等意外情況可能引起的麻煩,今天調試了這個小腳本,可以分使用者批量執行,同時記錄執行時間等日志資訊,比較實用。

執行方式:

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           

值得注意的是,gather_schema_stats收集統計資訊的各個參數配置,采用了最佳實踐配置,可以放心參考使用。