1. 首页 > Oracle教程 > 正文

Oracle教程FG281-Oracle AWR报告分析实战

内容大纲

内容简介:本文主要介绍Oracle数据库的AWR(Automatic Workload Repository)报告分析,包括AWR报告的生成、内容解读和性能问题诊断。风哥教程参考Oracle官方文档AWR相关内容,为生产环境提供完整的AWR报告分析解决方案。

Part01-基础概念与理论知识

1.1 AWR概念

Oracle AWR(Automatic Workload Repository)是Oracle数据库自动收集性能统计信息的机制,用于分析数据库性能。AWR会定期收集数据库的性能数据,包括系统负载、SQL执行情况、资源使用情况等,并将这些数据存储在数据字典中。通过分析AWR报告,可以识别数据库性能瓶颈,优化数据库性能。

1.2 AWR报告内容

  • 数据库概览:数据库版本、实例信息、快照信息等
  • 工作负载概览:DB Time、平均活动会话数、CPU使用情况等
  • Top SQL:最消耗资源的SQL语句
  • 等待事件:数据库等待事件统计
  • 内存使用:SGA和PGA使用情况
  • I/O统计:磁盘I/O使用情况
  • 段统计:表和索引的访问情况
  • 参数设置:数据库参数配置

1.3 AWR报告生成方法

  • 使用DBMS_WORKLOAD_REPOSITORY包:通过PL/SQL包生成AWR报告
  • 使用awrrpt.sql脚本:通过SQL*Plus执行脚本生成AWR报告
  • 使用Enterprise Manager:通过EM控制台生成AWR报告

Part02-生产环境规划与建议

2.1 AWR配置规划

制定合理的AWR配置规划:

  • 设置适当的快照间隔(默认60分钟)
  • 设置适当的快照保留时间(默认8天)
  • 根据数据库负载调整AWR收集频率
  • 配置AWR基线,用于性能比较
  • 定期生成AWR报告,分析数据库性能

2.2 AWR报告分析建议

AWR报告分析建议:

  • 关注DB Time和平均活动会话数
  • 分析Top SQL语句,优化高消耗SQL
  • 检查等待事件,识别性能瓶颈
  • 分析内存使用情况,调整内存配置
  • 检查I/O使用情况,优化存储配置

2.3 AWR数据管理

AWR数据管理建议:

  • 定期备份AWR数据
  • 清理过期的AWR数据,释放空间
  • 使用AWR基线进行性能比较
  • 保存重要的AWR报告,用于历史分析
  • 监控AWR数据收集的开销

Part03-生产环境项目实施方案

3.1 AWR配置

# 1. 查看AWR配置
SQL> SELECT * FROM dba_hist_wr_control;

DBID SNAP_INTERVAL RETENTION TOPNSQL
———- ————————— ————————— ———-
1234567890 +00000 01:00:00.0 +00008 00:00:00.0 DEFAULT

# 2. 修改AWR配置
SQL> EXEC DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS(
retention => 14*24*60, — 保留14天
interval => 30, — 30分钟一个快照
topnsql => 20 — 收集前20条SQL
);

PL/SQL procedure successfully completed.

# 3. 创建AWR基线
SQL> EXEC DBMS_WORKLOAD_REPOSITORY.CREATE_BASELINE(
start_snap_id => 100,
end_snap_id => 110,
baseline_name => ‘BASELINE_2026_04_03’,
description => ‘Baseline for 2026-04-03’
);

PL/SQL procedure successfully completed.

# 4. 查看AWR基线
SQL> SELECT * FROM dba_hist_baseline;

3.2 AWR报告生成

# 1. 使用awrrpt.sql脚本生成AWR报告
$ sqlplus / as sysdba

SQL> @$ORACLE_HOME/rdbms/admin/awrrpt.sql

Current Instance
~~~~~~~~~~~~~~~~
DBID DB_NAME INST_NUM INST_NAME
——— ——— ———- —————-
1234567890 FGEDUDB 1 FGEDUDB

Specify the Report Type
~~~~~~~~~~~~~~~~~~~~~~
Would you like an HTML report, or a plain text report?
Enter ‘html’ for an HTML report, or ‘text’ for plain text
Defaults to ‘html’
Enter value for report_type: html

Type Specified: html

Instances in this Workload Repository schema
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DBID Inst Num DB Name Instance Host
———— ——– ——— ———- ———-
* 1234567890 1 FGEDUDB FGEDUDB fgedu-db.net.cn

Using 1234567890 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 without
specifying a number lists all completed snapshots.

Enter value for num_days: 7

Listing the last 7 days of Completed Snapshots

Instance DB Name Snap Id Snap Started Snap Level
———— ——— ———- —————— ———-
FGEDUDB FGEDUDB 100 30 Mar 2026 00:00 1
00
FGEDUDB FGEDUDB 101 30 Mar 2026 01:00 1
00

FGEDUDB FGEDUDB 114 03 Apr 2026 12:00 1
00

Specify the Begin and End Snapshot Ids
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter value for begin_snap: 110
Begin Snapshot Id specified: 110

Enter value for end_snap: 114
End Snapshot Id specified: 114

Specify the Report Name
~~~~~~~~~~~~~~~~~~~~~~~
The default report file name is awrrpt_1_110_114.html.
To use this name, press to continue, otherwise enter an alternative.

Enter value for report_name: /oracle/reports/awr_20260403.html

Using the report name /oracle/reports/awr_20260403.html

# 2. 使用DBMS_WORKLOAD_REPOSITORY包生成AWR报告
SQL> DECLARE
l_report CLOB;
BEGIN
l_report := DBMS_WORKLOAD_REPOSITORY.AWR_REPORT_HTML(
l_dbid => 1234567890,
l_instance_number => 1,
l_start_snap_id => 110,
l_end_snap_id => 114
);
DBMS_OUTPUT.PUT_LINE(‘AWR Report generated successfully’);
END;
/

3.3 AWR报告分析

# 1. 分析数据库概览
# 查看数据库版本、实例信息、快照信息等

# 2. 分析工作负载概览
# 关注DB Time、平均活动会话数、CPU使用情况等

# 3. 分析Top SQL
# 识别最消耗资源的SQL语句

# 4. 分析等待事件
# 识别主要的等待事件,确定性能瓶颈

# 5. 分析内存使用
# 检查SGA和PGA使用情况,调整内存配置

# 6. 分析I/O统计
# 检查磁盘I/O使用情况,优化存储配置

# 7. 分析段统计
# 检查表和索引的访问情况,优化段设计

# 8. 分析参数设置
# 检查数据库参数配置,优化参数设置

3.4 AWR数据管理

# 1. 清理过期的AWR数据
SQL> EXEC DBMS_WORKLOAD_REPOSITORY.PURGE_
(start_snap_id => 1,
end_snap_id => 99,
dbid => 1234567890);

PL/SQL procedure successfully completed.

# 2. 查看AWR数据大小
SQL> SELECT owner, segment_name, bytes/1024/1024 MB
FROM dba_segments
WHERE segment_name LIKE ‘WRH$_%’
ORDER BY bytes DESC;

# 3. 备份AWR数据
# 使用RMAN备份数据库,包括AWR数据

# 4. 恢复AWR数据
# 从备份中恢复数据库,包括AWR数据

Part04-生产案例与实战讲解

4.1 AWR报告生成实战

# 1. 连接数据库
$ sqlplus / as sysdba

SQL*Plus: Release 19.0.0.0.0 – Production on Thu Apr 3 13:00:00 2026
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle. All rights reserved.

Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 – Production
Version 19.3.0.0.0

# 2. 生成AWR报告
SQL> @$ORACLE_HOME/rdbms/admin/awrrpt.sql

Current Instance
~~~~~~~~~~~~~~~~
DBID DB_NAME INST_NUM INST_NAME
——— ——— ———- —————-
1234567890 FGEDUDB 1 FGEDUDB

Specify the Report Type
~~~~~~~~~~~~~~~~~~~~~~
Would you like an HTML report, or a plain text report?
Enter ‘html’ for an HTML report, or ‘text’ for plain text
Defaults to ‘html’
Enter value for report_type: html

Type Specified: html

Instances in this Workload Repository schema
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DBID Inst Num DB Name Instance Host
———— ——– ——— ———- ———-
* 1234567890 1 FGEDUDB FGEDUDB fgedu-db.net.cn

Using 1234567890 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 without
specifying a number lists all completed snapshots.

Enter value for num_days: 1

Listing the last 1 days of Completed Snapshots

Instance DB Name Snap Id Snap Started Snap Level
———— ——— ———- —————— ———-
FGEDUDB FGEDUDB 110 03 Apr 2026 09:00 1
00
FGEDUDB FGEDUDB 111 03 Apr 2026 10:00 1
00
FGEDUDB FGEDUDB 112 03 Apr 2026 11:00 1
00
FGEDUDB FGEDUDB 113 03 Apr 2026 12:00 1
00

Specify the Begin and End Snapshot Ids
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter value for begin_snap: 110
Begin Snapshot Id specified: 110

Enter value for end_snap: 113
End Snapshot Id specified: 113

Specify the Report Name
~~~~~~~~~~~~~~~~~~~~~~~
The default report file name is awrrpt_1_110_113.html.
To use this name, press to continue, otherwise enter an alternative.

Enter value for report_name: /oracle/reports/awr_20260403.html

Using the report name /oracle/reports/awr_20260403.html

# 3. 查看AWR报告
$ ls -l /oracle/reports/awr_20260403.html
-rw-r–r– 1 oracle oinstall 1048576 Apr 3 13:05 /oracle/reports/awr_20260403.html

4.2 AWR报告分析实战

# 1. 分析工作负载概览
# 查看DB Time和平均活动会话数

DB Time: 1,200 minutes
DB CPU: 800 minutes
Average Active Sessions: 10

# 分析:DB Time远大于DB CPU,说明存在等待事件

# 2. 分析Top SQL
# 查看最消耗资源的SQL语句

SQL ID: 1234567890abcdef
SQL Text: SELECT * FROM fgedu.fgedu_orders WHERE order_date BETWEEN ‘2026-01-01’ AND ‘2026-04-01’
Elapsed Time: 300 minutes
CPU Time: 100 minutes
I/O Wait Time: 200 minutes

# 分析:该SQL语句消耗大量I/O时间,需要优化

# 3. 分析等待事件
# 查看主要的等待事件

Top 5 Wait Events:
1. db file sequential read: 40%
2. db file scattered read: 30%
3. direct path read: 15%
4. latch free: 10%
5. buffer busy waits: 5%

# 分析:主要等待事件是I/O相关的,需要优化存储和SQL

# 4. 分析内存使用
# 查看SGA和PGA使用情况

SGA Target: 8G
SGA Used: 7.5G
PGA Target: 2G
PGA Used: 1.8G

# 分析:内存使用合理,不需要调整

# 5. 分析I/O统计
# 查看磁盘I/O使用情况

Tablespace: USERS
Read Requests: 1,000,000
Write Requests: 500,000
Read Bytes: 100GB
Write Bytes: 50GB

# 分析:USERS表空间I/O活动频繁,需要优化

4.3 AWR报告优化实战

# 1. 优化Top SQL
# 分析SQL执行计划
SQL> EXPLAIN PLAN FOR SELECT * FROM fgedu.fgedu_orders WHERE order_date BETWEEN ‘2026-01-01’ AND ‘2026-04-01’;
SQL> SELECT * FROM TABLE(dbms_xplan.display);

Plan hash value: 1234567890

——————————————————————————-
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
——————————————————————————-
| 0 | SELECT STATEMENT | | 10000 | 500K | 5000 (1)| 00:01:00 |
|* 1 | TABLE ACCESS FULL| FGEDU_ORDERS| 10000 | 500K | 5000 (1)| 00:01:00 |
——————————————————————————-

# 创建索引
SQL> CREATE INDEX idx_fgedu_orders_order_date ON fgedu.fgedu_orders(order_date);

Index created.

# 验证优化效果
SQL> EXPLAIN PLAN FOR SELECT * FROM fgedu.fgedu_orders WHERE order_date BETWEEN ‘2026-01-01’ AND ‘2026-04-01’;
SQL> SELECT * FROM TABLE(dbms_xplan.display);

Plan hash value: 9876543210

—————————————————————————————
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
—————————————————————————————
| 0 | SELECT STATEMENT | | 10000 | 500K | 100 (1)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| FGEDU_ORDERS | 10000 | 500K | 100 (1)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_FGEDU_ORDERS_ORDER_DATE| 10000 | | 10 (0)| 00:00:01 |
—————————————————————————————

# 2. 优化I/O性能
# 查看表空间使用情况
SQL> SELECT tablespace_name, used_percent FROM dba_tablespace_usage_metrics WHERE tablespace_name = ‘USERS’;

TABLESPACE_NAME USED_PERCENT
—————————— ————
USERS 90

# 扩展表空间
SQL> ALTER TABLESPACE USERS ADD DATAFILE ‘/oradata/fgedudb/users02.dbf’ SIZE 100M AUTOEXTEND ON NEXT 50M MAXSIZE UNLIMITED;

Tablespace altered.

# 3. 监控优化效果
# 生成优化后的AWR报告
SQL> @$ORACLE_HOME/rdbms/admin/awrrpt.sql
# 分析优化前后的性能变化

Part05-风哥经验总结与分享

5.1 AWR报告分析最佳实践

  • 定期生成:定期生成AWR报告,及时了解数据库性能
  • 对比分析:与基线或历史报告对比,识别性能变化
  • 关注重点:关注Top SQL、等待事件和I/O统计
  • 优化优先:优先优化高消耗的SQL语句
  • 持续监控:持续监控优化效果,调整优化策略

5.2 AWR报告分析注意事项

  • 选择合适的快照间隔,避免生成过多的AWR数据
  • 关注DB Time与DB CPU的比例,识别等待事件
  • 分析Top SQL时,关注执行计划和资源消耗
  • 检查等待事件时,区分系统等待和用户等待
  • 结合其他性能工具,全面分析数据库性能

5.3 AWR报告分析建议

  • 建立AWR报告分析流程,定期分析数据库性能
  • 使用AWR基线进行性能比较,识别性能变化
  • 培训运维人员,提高AWR报告分析能力
  • 建立AWR报告知识库,积累性能分析经验
  • 与Oracle支持团队保持沟通,获取AWR报告分析的最佳实践

更多视频教程www.fgedu.net.cn

更多学习教程公众号风哥教程itpux_com

from oracle:www.itpux.com

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息