一、AWR技术原理与核心架构
Automatic Workload Repository(AWR) 是Oracle 10g引入的核心性能诊断工具,通过自动化数据收集机制构建数据库性能数据仓库。其技术架构包含三大核心组件:
- 快照机制(Snapshots)
后台进程MMON(Manageability Monitor)默认每小时自动捕获一次系统性能快照,MMNL辅助处理轻量级任务。快照内容涵盖600+项性能指标,包括SQL执行统计、等待事件、系统负载等。 - 数据存储(SYSAUX表空间)
所有快照数据持久化存储在SYSAUX表空间内,以WRM$_*(元数据)和WRH$_*(历史数据)格式存储。存储空间占用公式约为:空间(MB) = 快照数量 × 1.2(需预留冗余)。 - 报告引擎(Report Generator)
通过对比两个快照生成差异化分析报告(TXT/HTML格式),使用DBMS_WORKLOAD_REPOSITORY包实现数据处理。
关键进程解析:
- - MMON:负责快照调度、阈值警报、SQL统计信息更新
- - MMNL:当内存缓冲区(ASH)满时,将会话历史数据写入磁盘
- - CKPT:确保检查点期间数据一致性
二、版本演进与功能增强
版本 |
快照保留策略 |
关键改进 |
报告格式变化 |
Oracle 10g |
默认7天 |
取代Statspack,引入自动化快照 |
基础表格布局,无OS信息 |
Oracle 11g |
默认8天 |
增加TOP SQL分析维度 |
新增操作系统类型显示 |
11.2.0.4 |
- |
报告结构重构 |
新增"Wait Classes"、"IO Profile"章节,TOP 5事件扩展为TOP 10 |
Oracle 12c |
- |
多租仓支持 |
引入全局报告(Global Report),整合RAC多节点数据 |
Oracle 19c |
可定制保留期 |
ADDM集成增强 |
HTML5优化,支持时间范围对比 |
三、各版本操作指南与脚本
基础配置管理
-- 查看当前配置
SELECT snap_interval, retention
FROM dba_hist_wr_control;
-- 调整快照间隔(20分钟)和保留期(30天)
BEGIN
dbms_workload_repository.modify_snapshot_settings(
interval => 20,
retention => 30 * 24 * 60
);
END;
/
生成AWR报告通用步骤
- 连接数据库:sqlplus / as sysdba
- oracle用户下运行,报告生成保存位置默认在/home/oracle/XXX.html 或是text
- sql下面执行报告脚本: 注意是在sqlplus登录后
@awrrpt.sql -- 单实例
@awrgrpt.sql -- RAC全局报告
@awrrpti.sql -- 指定实例报告(RAC)
运维脚本手动版
1. 手动创建快照(准备测试前后)
-- 压测前
EXEC dbms_workload_repository.create_snapshot;
-- 压测后
EXEC dbms_workload_repository.create_snapshot;
2. 基线管理(性能对比基准)
-- 创建基线
BEGIN
dbms_workload_repository.create_baseline(
start_snap_id => 100,
end_snap_id => 110,
baseline_name => 'PEAK_LOAD_BASELINE'
);
END;
/
-- 删除基线
EXEC dbms_workload_repository.drop_baseline('PEAK_LOAD_BASELINE');
3. Shell自动化脚本(Linux环境)
#!/bin/bash
ORACLE_SID=ORCLCDB --注意自己的ORACLE_SID
ORACLE_HOME=/u01/app/oracle/product/19.3/dbhome_1
REPORT_FILE="awr_$(date +%Y%m%d).html"
sqlplus -s / as sysdba <<EOF
set pages 0 lin 200 feed off
spool $REPORT_FILE
SELECT dbms_workload_repository.awr_report_html(
(SELECT dbid FROM v$database),
(SELECT instance_number FROM v$instance),
(SELECT MAX(snap_id)-1 FROM dba_hist_snapshot),
(SELECT MAX(snap_id) FROM dba_hist_snapshot)
) FROM dual;
spool off
EOF
echo "Report generated: $REPORT_FILE"
4. 单节点和RAC环境实操,输入步骤一样,使用的sql脚本不同。
[root@OL97 ~]# su - oracle
[oracle@OL97 ~]$ sqlplus / as sysdba
SQL*Plus: Release 23.0.0.0.0 - Production on Thu Jun 19 21:19:57 2025
Version 23.8.0.25.04
Copyright (c) 1982, 2025, Oracle. All rights reserved.
Connected to:
Oracle Database 23ai Free Release 23.0.0.0.0 - Develop, Learn, and Run for Free
Version 23.8.0.25.04
SYS@FREE> @?/rdbms/admin/awrrpt.sql
Specify the Report Type
~~~~~~~~~~~~~~~~~~~~~~~
AWR reports can be generated in the following formats. Please enter the
name of the format at the prompt. Default value is 'html'.
'html' HTML format (default)
'text' Text format
'active-html' Includes Performance Hub active report
Enter value for report_type:html
Current Instance
~~~~~~~~~~~~~~~~
DB Id DB Name Inst Num Instance Container Name
-------------- -------------- -------------- -------------- --------------
1475650002 FREE 1 FREE CDB$ROOT
Instances in this Workload Repository schema
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DB Id Inst Num DB Name Instance Host
------------ ---------- --------- ---------- ------
* 1475650002 1 FREE FREE OL97
Using 1475650002 for database Id
Using 1 for instance number
Specify the number of days of snapshots to choose from
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Entering the number of days (n) will result in the most recent
(n) days of snapshots being listed. Pressing <return> without
specifying a number lists all completed snapshots.
Enter value for num_days:1
179 19 Jun 2025 10:29 1
180 19 Jun 2025 10:59 1
181 19 Jun 2025 11:29 1
182 19 Jun 2025 11:59 1
183 19 Jun 2025 12:29 1
184 19 Jun 2025 12:59 1
185 19 Jun 2025 13:29 1
186 19 Jun 2025 13:59 1
187 19 Jun 2025 14:29 1
188 19 Jun 2025 14:59 1
189 19 Jun 2025 15:29 1
190 19 Jun 2025 15:59 1
191 19 Jun 2025 16:29 1
192 19 Jun 2025 16:59 1
193 19 Jun 2025 17:29 1
194 19 Jun 2025 17:59 1
195 19 Jun 2025 18:29 1
196 19 Jun 2025 18:59 1
197 19 Jun 2025 19:29 1
198 19 Jun 2025 19:59 1
199 19 Jun 2025 20:29 1
200 19 Jun 2025 20:59 1
Specify the Begin and End Snapshot Ids
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter value for begin_snap:185
Begin Snapshot Id specified: 185
Enter value for end_snap: 192
End Snapshot Id specified: 192
Specify the Report Name
~~~~~~~~~~~~~~~~~~~~~~~
The default report file name is awrrpt_1_185_192.html. To use this name,
press <return> to continue, otherwise enter an alternative.
Enter value for report_name:23ai-free-20250619
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Additional Information
----------------------
Miscellaneous Information
-------------------------
There was no significant database activity to run the ADDM.
</pre>
<br /><a class="awr" href="#top">Back to Top</a><p />
<p />
<a class="awr" name="99991"></a>
<h3 class="awr">Report Footnotes</h3>
<ul>
<li class="awr"> This section contains footnotes used by this report</li>
</ul>
<table border="0" class="tdiff" summary="Report Footnotes"><tr></tr>
</table>
<br /><a class="awr" name="1">[1]. The elapsed time statistic actually shows the measured database time.</a><br />
<br />
<br /><a class="awr" href="#top">Back to Top</a><p />
<p />
<p />
<p />
<p />
<p />
<p />
<p />
<p />
<p />
End of Report
</body></html>
Report written to 23ai-free-20250619.html
SYS@FREE>
四、性能分析关键
解读AWR报告需聚焦三大核心领域:
- 负载特征(Load Profile)
- Redo Size:> 1MB/秒可能预示日志写入瓶颈
- Logical Reads:> 10,000/秒需检查索引效率
- Hard Parses:> 20/秒表明SQL重用率低
- 等待事件分析(Top 10 Events)
等待事件 |
问题指向 |
优化方案 |
db file sequential read |
索引扫描延迟 |
优化SQL、SSD迁移 |
log file sync |
提交延迟 |
分批提交、闪存日志 |
buffer busy waits |
热点块争用 |
分区、反向索引 |
- TOP SQL识别关注指标:
- Elapsed Time/Exec > 1秒
- Executions 异常偏高
- Disk Reads 占比 > 20%
诊断黄金法则:当DB Time > Elapsed Time × CPU核心数时,表明数据库处于持续高压状态
五、最佳实践与避坑
- 空间管理策略
SYSAUX空间不足是常见故障,监控脚本:
--
SELECT space_usage_kbytes/1024 MB_used
FROM v$sysaux_occupants
WHERE occupant_name='SM/AWR';
--
SELECT space_usage_kbytes/1024 MB_used
2 FROM v$sysaux_occupants
3 WHERE occupant_name='SM/AWR';
MB_USED
----------
472.25
SYS@FREE>
- 快照策略优化
场景 |
间隔 |
保留期 |
生产环境 |
30分钟 |
30天 |
压测期间 |
5分钟 |
保留至分析结束 |
开发环境 |
60分钟 |
7天 |
- 故障排查:AWR生成失败
- 检查SYSAUX表空间使用率(>95%需扩容)
- 降低快照频率:ALTER SYSTEM SET snapshot_interval=30;
- 清理历史数据:EXEC dbms_workload_repository.drop_snapshot_range(low_snap_id,high_snap_id);
- 报告关键指标缺失
- 确认STATISTICS_LEVEL=TYPICAL(BASIC模式禁用AWR)
- 检查dba_hist_snapshot是否有对应时间段快照
AWR作为Oracle性能分析的基石工具,其价值随着版本迭代不断提升。掌握其技术细节并配合自动化脚本,可使DBA在复杂性能问题诊断中游刃有余。建议定期创建性能基线,结合ADDM实现预测式优化,构建完整的数据库健康管理体系。
也可以在12c+环境中使用@?/rdbms/admin/awrinfo.sql可生成包含AWR配置、空间使用、快照分布的综合性报告,格式默认txt。