在Oracle 10g之前,对数据库性能检测一般使用statspack工具。而从10g开始,oracle提供了一个新的工具:AWR(Automatic Workload Repository)。定期生成AWR报告并分析,有利于我们对数据库进行监控和调优。
使用awr有一个前提:参数statistics_level不能为basic。该参数默认为typical,如非必要不建议修改。
执行如下命令并根据提示输入信息,即可生成awr报告:
SQL>@?/rdbms/admin/awrrpt.sql
其实通过查看awrrpt.sql,可以看到,awrrpt.sql会调用awrrpti.sql。
以下是awrrpti.sql部分内容:
-- ***************************************************
-- Customer-customizable report settings
-- Change these variables to run a report on different statistics
-- ***************************************************
-- The default number of days of snapshots to list when displaying the
-- list of snapshots to choose the begin and end snapshot Ids from.
--
-- List all snapshots
-- define num_days = '';
--
-- List no (i.e. 0) snapshots
-- define num_days = 0;
--
-- List past 3 day's snapshots
-- define num_days = 3;
--
-- Reports can be printed in text or html, and you must set the report_type
-- in addition to the report_name
--
-- Issue Report in Text Format
--define report_type='text';
--
-- Issue Report in HTML Format
--define report_type='html';
-- Optionally, set the snapshots for the report. If you do not set them,
-- you will be prompted for the values.
--define begin_snap = 545;
--define end_snap = 546;
-- Optionally, set the name for the report itself
--define report_name = 'awrrpt_1_545_546.html'
看到这里,我们已经发现,其实,只需要对上述参数手动define,就可以将生成awr的动作写成脚本,避免交互式操作。换句话说,可以部署在我们需要的主机上定期自动生成awr报告。
以下是脚本部分,awr的时间段定义为最大snap跨度(默认为7天):
#!/bin/ksh
for i in `cat SID.txt`
do
ORACLE_SID=$i
export ORACLE_SID
sqlplus /nolog<>/dev/null
connect / as sysdba
set heading off
spool tempfile.tmp
select min(SNAP_ID) from dba_hist_snapshot;
spool off
!
MINSNAPID=`cat tempfile.tmp|grep -v "SQL>" |grep -v ^$`
sqlplus /nolog</dev/null
connect / as sysdba
set heading off
spool tempfile.tmp
select max(SNAP_ID) from dba_hist_snapshot;
spool off
!
MAXSNAPID=`cat tempfile.tmp|grep -v "SQL>" |grep -v ^$`
echo "${i}_awrrpt_`date +%Y%m%d`.html" >tempfile.tmp
REPORT_NAME=`cat tempfile.tmp`
rm tempfile.tmp
echo "define num_days = 7">awrrpt_tmp.sql
echo "define report_type = 'html'">>awrrpt_tmp.sql
echo "define begin_snap = $MINSNAPID">>awrrpt_tmp.sql
echo "define end_snap = $MAXSNAPID">>awrrpt_tmp.sql
echo "define report_name = '$REPORT_NAME'">>awrrpt_tmp.sql
echo "@@?/rdbms/admin/awrrpt" >>awrrpt_tmp.sql
sqlplus /nolog</dev/null
connect / as sysdba
@awrrpt_tmp.sql
!
done
在SID.txt文件里,可以将需要生成awr的实例写入其中。
SID.txt: