内容大纲
内容简介:本文主要介绍Oracle数据库的ASH(Active Session History)报告分析,包括ASH报告的生成、内容解读和性能问题诊断。风哥教程参考Oracle官方文档ASH相关内容,为生产环境提供完整的ASH报告分析解决方案。
Part01-基础概念与理论知识
1.1 ASH概念
Oracle ASH(Active Session History)是Oracle数据库实时收集活动会话信息的机制,用于分析数据库实时性能。ASH会定期采样活动会话的状态,包括SQL语句、等待事件、资源使用情况等,并将这些数据存储在内存和数据字典中。通过分析ASH报告,可以实时识别数据库性能瓶颈,优化数据库性能。
1.2 ASH报告内容
- 会话概览:活动会话数量、等待事件分布等
- Top等待事件:最常见的等待事件
- Top SQL:最消耗资源的SQL语句
- Top会话:最活跃的会话
- Top对象:最常访问的对象
- 等待事件详情:详细的等待事件统计
- SQL详情:详细的SQL执行情况
1.3 ASH报告生成方法
- 使用ashrpt.sql脚本:通过SQL*Plus执行脚本生成ASH报告
- 使用Enterprise Manager:通过EM控制台生成ASH报告
- 使用DBMS_WORKLOAD_REPOSITORY包:通过PL/SQL包生成ASH报告
Part02-生产环境规划与建议
2.1 ASH配置规划
制定合理的ASH配置规划:
- 设置适当的ASH采样频率(默认1秒)
- 设置适当的ASH数据保留时间(默认1小时)
- 根据数据库负载调整ASH采样频率
- 配置ASH数据的存储和管理
- 定期生成ASH报告,分析数据库实时性能
2.2 ASH报告分析建议
ASH报告分析建议:
- 关注活动会话数量和等待事件分布
- 分析Top SQL语句,优化高消耗SQL
- 检查Top等待事件,识别性能瓶颈
- 分析Top会话,识别异常会话
- 结合AWR报告,全面分析数据库性能
2.3 ASH数据管理
ASH数据管理建议:
- 定期监控ASH数据收集的开销
- 调整ASH数据保留时间,平衡存储空间和分析需求
- 使用ASH数据进行实时性能分析
- 结合AWR数据进行历史性能分析
- 保存重要的ASH报告,用于性能分析
Part03-生产环境项目实施方案
3.1 ASH配置
SQL> SELECT * FROM v$ash_info;
SAMPLE_INTERVAL ASH_SIZE ASH_BUCKET_COUNT ESTIMATED_RETENTION
—————– ———- —————– ——————–
1 1048576 1048576 60
# 2. 修改ASH采样频率
SQL> ALTER SYSTEM SET ash_sampling_interval=2 SCOPE=both;
System altered.
# 3. 修改ASH缓冲区大小
SQL> ALTER SYSTEM SET ash_buffer_size=2097152 SCOPE=spfile;
System altered.
# 4. 重启数据库使参数生效
SQL> SHUTDOWN IMMEDIATE;
SQL> STARTUP;
3.2 ASH报告生成
$ sqlplus / as sysdba
SQL> @$ORACLE_HOME/rdbms/admin/ashrpt.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 minutes of ASH data to analyze
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Entering the number of minutes (n) will result in the most recent
(n) minutes of ASH data being analyzed. Pressing
specifying a number lists all completed snapshots.
Enter value for num_minutes: 30
Specify the Report Name
~~~~~~~~~~~~~~~~~~~~~~~
The default report file name is ashrpt_1.html.
To use this name, press
Enter value for report_name: /oracle/reports/ash_20260403.html
Using the report name /oracle/reports/ash_20260403.html
# 2. 使用Enterprise Manager生成ASH报告
# 登录EM控制台
# 导航到”目标” -> “数据库” -> “fgedudb” -> “性能” -> “ASH报告”
# 设置时间范围:过去30分钟
# 点击”生成”按钮
# 查看ASH报告
3.3 ASH报告分析
# 查看活动会话数量、等待事件分布等
# 2. 分析Top等待事件
# 识别最常见的等待事件,确定性能瓶颈
# 3. 分析Top SQL
# 识别最消耗资源的SQL语句
# 4. 分析Top会话
# 识别最活跃的会话,检查是否有异常会话
# 5. 分析Top对象
# 识别最常访问的对象,优化对象访问
# 6. 分析等待事件详情
# 查看详细的等待事件统计,了解等待原因
# 7. 分析SQL详情
# 查看详细的SQL执行情况,优化SQL语句
3.4 ASH数据管理
SQL> SELECT * FROM v$ash_info;
SAMPLE_INTERVAL ASH_SIZE ASH_BUCKET_COUNT ESTIMATED_RETENTION
—————– ———- —————– ——————–
2 2097152 2097152 120
# 2. 查看ASH数据大小
SQL> SELECT owner, segment_name, bytes/1024/1024 MB
FROM dba_segments
WHERE segment_name LIKE ‘WRH$_ACTIVE_SESSION_HISTORY’
ORDER BY bytes DESC;
# 3. 清理过期的ASH数据
SQL> EXEC DBMS_WORKLOAD_REPOSITORY.PURGE_
(start_snap_id => 1,
end_snap_id => 99,
dbid => 1234567890);
PL/SQL procedure successfully completed.
# 4. 使用ASH数据进行实时性能分析
SQL> SELECT event, count(*) FROM v$active_session_history
WHERE sample_time > SYSDATE – 30/1440
GROUP BY event
ORDER BY count(*) DESC;
Part04-生产案例与实战讲解
4.1 ASH报告生成实战
$ sqlplus / as sysdba
SQL*Plus: Release 19.0.0.0.0 – Production on Thu Apr 3 14: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. 生成ASH报告
SQL> @$ORACLE_HOME/rdbms/admin/ashrpt.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 minutes of ASH data to analyze
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Entering the number of minutes (n) will result in the most recent
(n) minutes of ASH data being analyzed. Pressing
specifying a number lists all completed snapshots.
Enter value for num_minutes: 60
Specify the Report Name
~~~~~~~~~~~~~~~~~~~~~~~
The default report file name is ashrpt_1.html.
To use this name, press
Enter value for report_name: /oracle/reports/ash_20260403.html
Using the report name /oracle/reports/ash_20260403.html
# 3. 查看ASH报告
$ ls -l /oracle/reports/ash_20260403.html
-rw-r–r– 1 oracle oinstall 524288 Apr 3 14:10 /oracle/reports/ash_20260403.html
4.2 ASH报告分析实战
# 查看活动会话数量、等待事件分布等
Active Sessions: 20
Average Active Sessions: 15
# 2. 分析Top等待事件
# 查看最常见的等待事件
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%
# 3. 分析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: 150 minutes
CPU Time: 50 minutes
I/O Wait Time: 100 minutes
# 4. 分析Top会话
# 查看最活跃的会话
Session ID: 1234
User: FGEDU
Program: sqlplus@fgedu-client.net.cn
Active Time: 120 minutes
Wait Time: 100 minutes
# 5. 分析Top对象
# 查看最常访问的对象
Object Name: FGEDU.FGEDU_ORDERS
Access Count: 10000
Wait Time: 80 minutes
4.3 ASH报告优化实战
# 分析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. 分析等待事件
# 查看等待事件详情
SQL> SELECT event, p1text, p1, p2text, p2, p3text, p3, count(*)
FROM v$active_session_history
WHERE sample_time > SYSDATE – 30/1440
AND event = ‘db file sequential read’
GROUP BY event, p1text, p1, p2text, p2, p3text, p3
ORDER BY count(*) DESC;
# 3. 监控优化效果
# 生成优化后的ASH报告
SQL> @$ORACLE_HOME/rdbms/admin/ashrpt.sql
# 分析优化前后的性能变化
Part05-风哥经验总结与分享
5.1 ASH报告分析最佳实践
- 实时分析:使用ASH报告进行实时性能分析,及时发现问题
- 关注重点:关注Top等待事件、Top SQL和Top会话
- 对比分析:与历史ASH报告对比,识别性能变化
- 优化优先:优先优化高消耗的SQL语句和主要等待事件
- 持续监控:持续监控优化效果,调整优化策略
5.2 ASH报告分析注意事项
- 选择合适的时间范围,避免分析过多或过少的数据
- 关注活动会话数量,识别系统负载变化
- 分析Top SQL时,关注执行计划和资源消耗
- 检查等待事件时,区分系统等待和用户等待
- 结合AWR报告,全面分析数据库性能
5.3 ASH报告分析建议
- 建立ASH报告分析流程,定期分析数据库实时性能
- 使用ASH报告进行实时性能监控,及时发现问题
- 培训运维人员,提高ASH报告分析能力
- 建立ASH报告知识库,积累性能分析经验
- 与Oracle支持团队保持沟通,获取ASH报告分析的最佳实践
更多视频教程www.fgedu.net.cn
更多学习教程公众号风哥教程itpux_com
from oracle:www.itpux.com
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
