本文为博文Active Session History (ASH)的读书笔记。
AWR,ADDM,SQL Trace是对过去事件的分析,[G]V$视图包含大量实时信息,但使用界面不友好,对初学者较难。因此Oracle 10g推出了ASH,属于Oracle Diagnostics Pack。
活动会话信息每秒采样一次,并存放在 V$ACTIVE_SESSION_HISTORY 视图中。
如果数据库会话位于 CPU 上或正在等待非空闲等待类的事件,则该数据库会话被视为活动的。
V$ACTIVE_SESSION_HISTORY 视图本质上是一个事实表,它可以链接到多个维度,以提供特定于各种事物的统计信息,包括 SQL 语句、执行计划、对象、等待事件、会话、模块、操作、客户端标识符、 这使得它成为一种非常灵活的方式来识别活动会话正在做什么或已经做了什么。 例如,如果我想查看过去 5 分钟数据库的主要活动,我可以使用以下查询。
SELECT NVL(a.event, 'ON CPU') AS event,
COUNT(*) AS total_wait_time
FROM v$active_session_history a
WHERE a.sample_time > SYSDATE - 5/(24*60) -- 5 mins
GROUP BY a.event
ORDER BY total_wait_time DESC;
EVENT TOTAL_WAIT_TIME
---------------------------------------------------------------- ---------------
ON CPU 157
log file sync 8
db file scattered read 4
latch: cache buffers chains 3
library cache: mutex X 1
PGA memory operation 1
6 rows selected.
注意以上SQL中计时的方式,是用count(*),这是因为每1秒采样一次。这里不能用WAIT_TIME或TIME_WAITED的累计,说明见原文。
来看一下v$active_session_history表的定义:
SQL> desc v$active_session_history
Name Null? Type
----------------------------------------- -------- ----------------------------
SAMPLE_ID NUMBER
SAMPLE_TIME TIMESTAMP(3)
SAMPLE_TIME_UTC TIMESTAMP(3)
USECS_PER_ROW NUMBER
IS_AWR_SAMPLE VARCHAR2(1)
SESSION_ID NUMBER
SESSION_SERIAL# NUMBER
SESSION_TYPE VARCHAR2(10)
FLAGS NUMBER
USER_ID NUMBER
SQL_ID VARCHAR2(13)
IS_SQLID_CURRENT VARCHAR2(1)
SQL_CHILD_NUMBER NUMBER
SQL_OPCODE NUMBER
SQL_OPNAME VARCHAR2(64)
FORCE_MATCHING_SIGNATURE NUMBER
TOP_LEVEL_SQL_ID VARCHAR2(13)
TOP_LEVEL_SQL_OPCODE NUMBER
SQL_ADAPTIVE_PLAN_RESOLVED NUMBER
SQL_FULL_PLAN_HASH_VALUE NUMBER
SQL_PLAN_HASH_VALUE NUMBER
SQL_PLAN_LINE_ID NUMBER
SQL_PLAN_OPERATION VARCHAR2(30)
SQL_PLAN_OPTIONS VARCHAR2(30)
SQL_EXEC_ID NUMBER
SQL_EXEC_START DATE
PLSQL_ENTRY_OBJECT_ID NUMBER
PLSQL_ENTRY_SUBPROGRAM_ID NUMBER
PLSQL_OBJECT_ID NUMBER
PLSQL_SUBPROGRAM_ID NUMBER
QC_INSTANCE_ID NUMBER
QC_SESSION_ID NUMBER
QC_SESSION_SERIAL# NUMBER
PX_FLAGS NUMBER
EVENT VARCHAR2(64)
EVENT_ID NUMBER
EVENT# NUMBER
SEQ# NUMBER
P1TEXT VARCHAR2(64)
P1 NUMBER
P2TEXT VARCHAR2(64)
P2 NUMBER
P3TEXT VARCHAR2(64)
P3 NUMBER
WAIT_CLASS VARCHAR2(64)
WAIT_CLASS_ID NUMBER
WAIT_TIME NUMBER
SESSION_STATE VARCHAR2(7)
TIME_WAITED NUMBER
BLOCKING_SESSION_STATUS VARCHAR2(11)
BLOCKING_SESSION NUMBER
BLOCKING_SESSION_SERIAL# NUMBER
BLOCKING_INST_ID NUMBER
BLOCKING_HANGCHAIN_INFO VARCHAR2(1)
CURRENT_OBJ# NUMBER
CURRENT_FILE# NUMBER
CURRENT_BLOCK# NUMBER
CURRENT_ROW# NUMBER
TOP_LEVEL_CALL# NUMBER
TOP_LEVEL_CALL_NAME VARCHAR2(64)
CONSUMER_GROUP_ID NUMBER
XID RAW(8)
REMOTE_INSTANCE# NUMBER
TIME_MODEL NUMBER
IN_CONNECTION_MGMT VARCHAR2(1)
IN_PARSE VARCHAR2(1)
IN_HARD_PARSE VARCHAR2(1)
IN_SQL_EXECUTION VARCHAR2(1)
IN_PLSQL_EXECUTION VARCHAR2(1)
IN_PLSQL_RPC VARCHAR2(1)
IN_PLSQL_COMPILATION VARCHAR2(1)
IN_JAVA_EXECUTION VARCHAR2(1)
IN_BIND VARCHAR2(1)
IN_CURSOR_CLOSE VARCHAR2(1)
IN_SEQUENCE_LOAD VARCHAR2(1)
IN_INMEMORY_QUERY VARCHAR2(1)
IN_INMEMORY_POPULATE VARCHAR2(1)
IN_INMEMORY_PREPOPULATE VARCHAR2(1)
IN_INMEMORY_REPOPULATE VARCHAR2(1)
IN_INMEMORY_TREPOPULATE VARCHAR2(1)
IN_TABLESPACE_ENCRYPTION VARCHAR2(1)
CAPTURE_OVERHEAD VARCHAR2(1)
REPLAY_OVERHEAD VARCHAR2(1)
IS_CAPTURED VARCHAR2(1)
IS_REPLAYED VARCHAR2(1)
IS_REPLAY_SYNC_TOKEN_HOLDER VARCHAR2(1)
SERVICE_HASH NUMBER
PROGRAM VARCHAR2(48)
MODULE VARCHAR2(64)
ACTION VARCHAR2(64)
CLIENT_ID VARCHAR2(64)
MACHINE VARCHAR2(64)
PORT NUMBER
ECID VARCHAR2(64)
DBREPLAY_FILE_ID NUMBER
DBREPLAY_CALL_COUNTER NUMBER
TM_DELTA_TIME NUMBER
TM_DELTA_CPU_TIME NUMBER
TM_DELTA_DB_TIME NUMBER
DELTA_TIME NUMBER
DELTA_READ_IO_REQUESTS NUMBER
DELTA_WRITE_IO_REQUESTS NUMBER
DELTA_READ_IO_BYTES NUMBER
DELTA_WRITE_IO_BYTES NUMBER
DELTA_INTERCONNECT_IO_BYTES NUMBER
DELTA_READ_MEM_BYTES NUMBER
PGA_ALLOCATED NUMBER
TEMP_SPACE_ALLOCATED NUMBER
CON_DBID NUMBER
CON_ID NUMBER
DBOP_NAME VARCHAR2(30)
DBOP_EXEC_ID NUMBER
其中几个重要的列为:
- SAMPLE_TIME:采样时间
- SESSION_STATE:WAITING或ON CPU
- EVENT:等待事件(SESSION_STATE=WAITING)或空(SESSION_STATE=ON CPU)
- TIME_WAITED:等待时间(SESSION_STATE=WAITING),单位为微秒
历史的ASH数据可以查询视图DBA_HIST_ACTIVE_SESS_HISTORY,其实也就是存到AWR了。可以认为DBA_HIST_ACTIVE_SESS_HISTORY是对V$ACTIVE_SESSION_HISTORY的采样,采样时间是10秒一次。
SELECT NVL(a.event, 'ON CPU') AS event,
COUNT(*)*10 AS total_wait_time
FROM dba_hist_active_sess_history a
WHERE a.sample_time > SYSDATE - 1
GROUP BY a.event
ORDER BY total_wait_time DESC;
EVENT TOTAL_WAIT_TIME
---------------------------------------------------------------- ---------------
ON CPU 710
library cache pin 410
buffer busy waits 90
latch: cache buffers chains 30
log file sync 20
enq: SQ - contention 20
PGA memory operation 10
Failed Logon Delay 10
control file sequential read 10
9 rows selected.
Oracle EM Performance 页面可以方便的浏览实时和历史的ASH信息,此不赘述。
EM和SQL Plus都可以出ASH报告,格式支持HTML和TXT。
@$ORACLE_HOME/rdbms/admin/ashrpt.sql
例如:
SQL> connect sys@orclpdb1 as sysdba
SQL> @?/rdbms/admin/ashrpt.sql
Specify the Report Type
~~~~~~~~~~~~~~~~~~~~~~~
Enter 'html' for an HTML report, or 'text' for plain text
Defaults to 'html'
Enter value for report_type:
Specify the location of AWR Data
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
AWR_ROOT - Use AWR data from root (default)
AWR_PDB - Use AWR data from PDB
Enter value for awr_location: AWR_PDB
...
Location of AWR Data Specified: AWR_PDB
Current Instance
~~~~~~~~~~~~~~~~
DB Id DB Name Inst Num Instance
----------- ------------ -------- ------------
3484266510 ORCL 1 ORCL
...
Defaults to current database
Using database id: 3484266510
Enter instance numbers. Enter 'ALL' for all instances in a
RAC cluster or explicitly specify list of instances (e.g., 1,2,3).
Defaults to current instance.
Using instance number(s): 1
ASH Samples in this Workload Repository schema
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Oldest ASH sample available: 06-Oct-23 13:20:18 [ 822 mins in the past]
Latest ASH sample available: 07-Oct-23 02:45:08 [ 17 mins in the past]
...
Specify the timeframe to generate the ASH report
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter begin time for report:
Defaults to -15 mins
Enter value for begin_time:
Report begin time specified:
Enter duration in minutes starting from begin time:
Defaults to SYSDATE - begin_time
Press Enter to analyze till current time
Enter value for duration:
Report duration specified:
Using 07-Oct-23 02:46:55 as report begin time
Using 07-Oct-23 03:02:00 as report end time
...
Report written to ashrpt_1_1007_0302.html
输出见附录。
SQL Developer也支持ASH报告。
还有一个开源工具ASH Viewer,就不试了。
参考
- How to Open Port 1521 on Linux Server
- Generate Some Database Activity
- SQL Developer ASH Report Viewer
- Use ASH Report Writer in Oracle SQL Developer
- ASH Report I - Theory
- 2 Day DBA 11g Release 2
- 2-Day DBA (12c Release 2)
- Oracle Database 2-Day DBA (19c)
附录:ASH Report输出样例
ASH Report For ORCL/ORCL
DB Name | DB Id | Instance | Inst num | Release | RAC | Host |
---|---|---|---|---|---|---|
ORCL | 3484266510 | ORCL | 1 | 19.0.0.0.0 | NO | instance-20230918-1052-db19c-iaas |
CPUs | SGA Size | Buffer Cache | Shared Pool | ASH Buffer Size | In-memory Area Size |
---|---|---|---|---|---|
2 | 4,704M (100%) | 3,232M (68.7%) | 545M (11.6%) | Not Available | 0.0M (0.0%) |
Sample Time | Data Source | |
---|---|---|
Analysis Begin Time: | 07-Oct-23 02:46:55 | V$ACTIVE_SESSION_HISTORY |
Analysis End Time: | 07-Oct-23 03:02:00 | V$ACTIVE_SESSION_HISTORY |
Elapsed Time: | 15.1 (mins) | |
Sample Count: | 0 | |
Average Active Sessions: | 0.00 | |
Avg. Active Session per CPU: | 0.00 | |
Report Target: | None specified |
ASH Report
- Top Events
- Load Profile
- Top SQL
- Top PL/SQL
- Top Java
- Top Call Types
- Top Sessions
- Top Objects/Files/Latches
- Activity Over Time
Back to Top
Top Events
- Top User Events
- Top Background Events
- Top Event P1/P2/P3 Values
Top User Events
No data exists for this section of the report.
Back to Top Events
Back to Top
Top Background Events
No data exists for this section of the report.
Back to Top Events
Back to Top
Top Event P1/P2/P3 Values
No data exists for this section of the report.
Back to Top Events
Back to Top
Load Profile
- Top Service/Module
- Top Client IDs
- Top SQL Command Types
- Top Phases of Execution
Top Service/Module
No data exists for this section of the report.
Back to Load Profile
Back to Top
Top Client IDs
No data exists for this section of the report.
Back to Load Profile
Back to Top
Top SQL Command Types
No data exists for this section of the report.
Back to Load Profile
Back to Top
Top Phases of Execution
No data exists for this section of the report.
Back to Load Profile
Back to Top
Top SQL
- Top SQL with Top Events
- Top SQL with Top Row Sources
- Top SQL using literals
- Top Parsing Module/Action
- Complete List of SQL Text
Top SQL with Top Events
No data exists for this section of the report.
Back to Top SQL
Back to Top
Top SQL with Top Row Sources
No data exists for this section of the report.
Back to Top SQL
Back to Top
Top SQL using literals
No data exists for this section of the report.
Back to Top SQL
Back to Top
Top Parsing Module/Action
No data exists for this section of the report.
Back to Top SQL
Back to Top
Complete List of SQL Text
No data exists for this section of the report.
Back to Top SQL
Back to Top
Top PL/SQL Procedures
No data exists for this section of the report.
Back to Top
Top Java Workload
No data exists for this section of the report.
Back to Top
Top Call Types
No data exists for this section of the report.
Back to Top
Top Sessions
- Top Sessions
- Top Blocking Sessions
- Top Sessions running PQs
Top Sessions
No data exists for this section of the report.
Back to Top Sessions
Back to Top
Top Blocking Sessions
No data exists for this section of the report.
Back to Top Sessions
Back to Top
Top Sessions running PQs
No data exists for this section of the report.
Back to Top Sessions
Back to Top
Top Objects/Files/Latches
- Top DB Objects
- Top DB Files
- Top Latches
Top DB Objects
No data exists for this section of the report.
Back to Top Objects/Files/Latches
Back to Top
Top DB Files
No data exists for this section of the report.
Back to Top Objects/Files/Latches
Back to Top
Top Latches
No data exists for this section of the report.
Back to Top Objects/Files/Latches
Back to Top
Activity Over Time
No data exists for this section of the report.
Back to Top
End of Report