文档课题:运用dbms_sqltune.report_sql_monitor生成sql语句监控信息.
数据库:oracle 11.2.0.1
1、理论知识
通过给SQL语句加/*+ MONITOR */,强制收集monitor信息(正常情况下5秒以上的SQL语句会被自动收集)后,用BMS_SQLTUNE.report_sql_monitor可以得到执行计划等详细信息。
2、实际测试
SYS@orcl> select * from v$version;
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
--添加monitor hint监控sql语句.
SYS@orcl> select /*+monitor */ object_id,owner from test where object_id=1000;
......
说明:此处省略查询结果.
SYS@orcl> col sql_text for a70
SYS@orcl> set line 200
SYS@orcl> select sql_id,status,sql_text from v$sql_monitor where sql_text like '%from test where object_id=1000%';
SQL_ID STATUS SQL_TEXT
------------- ------------------- ----------------------------------------------------------------------
0hv6ggtrkxg2c DONE (ALL ROWS) select /*+monitor */ object_id,owner from test where object_id=1000
SYS@orcl> set long 1000000
SYS@orcl> set longchunksize 1000000
SYS@orcl> set linesize 1000
SYS@orcl> set pagesize 0
SYS@orcl> set trim on
SYS@orcl> set trimspool on
SYS@orcl> set echo off
SYS@orcl> set feedback off
SYS@orcl> select dbms_sqltune.report_sql_monitor(
2 sql_id => '0hv6ggtrkxg2c',
3 type => 'TEXT',
4 report_level => 'ALL') as report
5 from dual;
SQL Monitoring Report
SQL Text
------------------------------
select /*+monitor */ object_id,owner from test where object_id=1000
Global Information
------------------------------
Status : DONE (ALL ROWS)
Instance ID : 1
Session : SYS (63:781)
SQL ID : 0hv6ggtrkxg2c
SQL Execution ID : 16777216
Execution Started : 06/04/2023 18:23:49
First Refresh Time : 06/04/2023 18:23:49
Last Refresh Time : 06/04/2023 18:23:50
Duration : 1s
Module/Action : sqlplus@leo-oracle (TNS V1-V3)/-
Service : SYS$USERS
Program : sqlplus@leo-oracle (TNS V1-V3)
Fetch Calls : 4794
Global Stats
======================================
| Elapsed | Cpu | Fetch | Buffer |
| Time(s) | Time(s) | Calls | Gets |
======================================
| 0.04 | 0.04 | 4794 | 5752 |
======================================
SQL Plan Monitoring Details (Plan Hash Value=1357081020)
=========================================================================================================================
| Id | Operation | Name | Rows | Cost | Time | Start | Execs | Rows | Activity | Activity Detail |
| | | | (Estim) | | Active(s) | Active | | (Actual) | (%) | (# samples) |
=========================================================================================================================
| 0 | SELECT STATEMENT | | | | 2 | +0 | 1 | 71895 | | |
| 1 | TABLE ACCESS FULL | TEST | 71888 | 280 | 2 | +0 | 1 | 71895 | | |
=========================================================================================================================
3、测试不加monitor
--以下测试不添加monitor参数,查询超5秒的场景.
SYS@orcl> drop table test purge;
Table dropped.
SYS@orcl> create table test as select * from dba_objects;
Table created.
Elapsed: 00:00:00.52
SYS@orcl> insert into test select * from test;
71937 rows created.
Elapsed: 00:00:00.38
SYS@orcl> r
1* insert into test select * from test
143874 rows created.
Elapsed: 00:00:00.46
SYS@orcl> r
1* insert into test select * from test
287748 rows created.
Elapsed: 00:00:00.89
SYS@orcl> r
1* insert into test select * from test
575496 rows created.
Elapsed: 00:00:04.96
SYS@orcl> select object_id,owner from test;
......(省略若干)
1150992 rows selected.
Elapsed: 00:00:23.93
SYS@orcl> set line 100
SYS@orcl> select * from table(dbms_xplan.display_cursor('','',''));
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
SQL_ID d5s05r9q6nba9, child number 0
-------------------------------------
select object_id,owner from test
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 4431 (100)| |
| 1 | TABLE ACCESS FULL| TEST | 1080K| 30M| 4431 (1)| 00:00:54 |
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
17 rows selected.
Elapsed: 00:00:00.01
SYS@orcl> select sql_text from v$sql where sql_id='d5s05r9q6nba9';
SQL_TEXT
----------------------------------------------------------------------
select object_id,owner from test
Elapsed: 00:00:00.00
SYS@orcl> select sql_id,executions,version_count from v$sqlarea where sql_text like 'select object_id,owner from test';
SQL_ID EXECUTIONS VERSION_COUNT
------------- ---------- -------------
d5s05r9q6nba9 1 1
Elapsed: 00:00:00.01
SYS@orcl> select dbms_sqltune.report_sql_monitor(
2 sql_id => 'd5s05r9q6nba9',
3 report_level=> 'ALL',
4 type => 'TEXT')
5 from dual;
DBMS_SQLTUNE.REPORT_SQL_MONITOR(SQL_ID=>'D5S05R9Q6NBA9',REPORT_LEVEL=>'ALL',TYPE
--------------------------------------------------------------------------------
SQL Monitoring Report
Elapsed: 00:00:00.01
说明:如上所示,即使查询语句超过5s,执行计划等信息并没有显示出来,也就意味着并没有被监控到.
参考文档:https://www.cnblogs.com/gaojian/p/14642289.html
标签:dbms,monitor,00,SYS,orcl,sql,test,select
From: https://blog.51cto.com/u_12991611/6412007