Real-Time SQL Monitoring using DBMS_SQLTUNE

Real-Time SQL Monitoring reports are available from three locations:

  • Enterprise Manager - Click the "Performance" tab, then the "SQL Monitoring" link at the bottom-right of the page to display the "Monitored SQL Executions" screen. Click the SQL_ID of interest to display the SQL monitoring report.
  • SQL Developer - Available from the "Tools > Monitor SQL" menu.
  • DBMS_SQLTUNE package.

In this article I will demonstrate the use of the DBMS_SQLTUNE package to display SQL monitoring reports without using Enterprise Manager or SQL Developer. This article has been updated to include additional functionality
introduced in Oracle 11g Release 2.

Related articles.

Introduction

Oracle 11g automatically monitors SQL statements if they are run in parallel, or consume 5 or more seconds of CPU or I/O in a single execution. This allows resource intensive SQL to be monitored as it is executing, as well as giving
access to detailed information about queries once they are complete.

SQL monitoring requires the STATISTICS_LEVEL parameter to be set to ‘TYPICAL‘ or ‘ALL‘, and the CONTROL_MANAGEMENT_PACK_ACCESS parameter set to ‘DIAGNOSTIC+TUNING‘.

SQL> CONN / AS SYSDBA
Connected.
SQL> SHOW PARAMETER statistics_level

NAME				     TYPE	 VALUE
------------------------------------ ----------- ------------------------------
statistics_level		     string	 TYPICAL

SQL> SHOW PARAMETER control_management_pack_access

NAME				     TYPE	 VALUE
------------------------------------ ----------- ------------------------------
control_management_pack_access	     string	 DIAGNOSTIC+TUNING

SQL>

MONITOR Hint

The MONITOR hint switches on SQL monitoring for statements that would not otherwise initiate it.

SELECT /*+ MONITOR */ d.dname, WM_CONCAT(e.ename) AS employees
FROM   emp e
       JOIN dept d ON e.deptno = d.deptno
GROUP BY d.dname
ORDER BY d.dname;

REPORT_SQL_MONITOR

The REPORT_SQL_MONITOR function is used to return a SQL monitoring report for a specific SQL statement. The SQL statement can be identified using a variety of parameters, but it will typically be identified using the SQL_ID parameter.

The function can accept many optional parameters, shown here, but most of the time you will probably
only use the following.

  • SQL_ID - The SQL_ID of the query of interest. When NULL (the default) the last monitored statement is targeted.
  • SQL_EXEC_ID - When the SQL_ID is specified, the SQL_EXEC_ID indicates the individual execution of interest. When NULL (the default) the most recent execution of the statement targeted by the SQL_ID is
    assumed.
  • REPORT_LEVEL - The amount of information displayed in the report. The basic allowed values are ‘NONE‘, ‘BASIC‘, ‘TYPICAL‘ or ‘ALL‘, but the information displayed can be modified further by adding (+) or subtracting (-) named report sections
    (eg. ‘BASIC +PLAN +BINDS‘ or ‘ALL -PLAN‘). This is similar to the way DBMS_XPLAN output can be tailored in the later releases. I almost always use ‘ALL‘.
  • TYPE - The format used to display the report (‘TEXT‘, ‘HTML‘, ‘XML‘ or ‘ACTIVE‘). The ‘ACTIVE‘ setting is new to Oracle 11g Release 2 and displays the output using HTML and Flash, similar to the way it is shown in Enterprise Manager.
  • SESSION_ID - Targets a subset of queries based on the specified SID. Use SYS_CONTEXT(‘USERENV‘,‘SID‘) for the current session.

The report accesses several dynamic performance views, so you will most likely access it from a privileged user, or a user granted the SELECT_CATALOG_ROLE role.

To see it in action, first we make sure we have a monitored statement to work with.

CONN scott/tiger

SELECT /*+ MONITOR */ d.dname, WM_CONCAT(e.ename) AS employees
FROM   emp e
       JOIN dept d ON e.deptno = d.deptno
GROUP BY d.dname
ORDER BY d.dname;

Monitored statements can be identified using the V$SQL_MONITOR view. This view was present in Oracle 11g Release 1, but has additional columns in Oracle 11g Release 2, making it much more useful. It contains an entry for
each execution monitored, so it can contain multiple entries for individual SQL statements.

CONN / AS SYSDBA

-- 11gR1
SELECT sql_id, status
FROM   v$sql_monitor;

SQL_ID	      STATUS
------------- -------------------
526mvccm5nfy4 DONE (ALL ROWS)

SQL>

-- 11gR2
SET LINESIZE 200
COLUMN sql_text FORMAT A80

SELECT sql_id, status, sql_text
FROM   v$sql_monitor
WHERE  username = ‘SCOTT‘;

SQL_ID        STATUS              SQL_TEXT
------------- ------------------- --------------------------------------------------------------------------------
526mvccm5nfy4 DONE (ALL ROWS)     SELECT /*+ MONITOR */ d.dname, WM_CONCAT(e.ename) AS employees
                                  FROM   emp e
                                         JOIN dept d ON e.deptno = d.deptno
                                  GROUP BY d.dname
                                  ORDER BY d.dname

SQL>

Once the SQL_ID is identified, we can generate a report using the REPORT_SQL_MONITOR function.

SET LONG 1000000
SET LONGCHUNKSIZE 1000000
SET LINESIZE 1000
SET PAGESIZE 0
SET TRIM ON
SET TRIMSPOOL ON
SET ECHO OFF
SET FEEDBACK OFF

SPOOL /host/report_sql_monitor.htm
SELECT DBMS_SQLTUNE.report_sql_monitor(
  sql_id       => ‘526mvccm5nfy4‘,
  type         => ‘HTML‘,
  report_level => ‘ALL‘) AS report
FROM dual;
SPOOL OFF

Examples of the output for each available TYPE are displayed below.

  • TEXT
  • HTML
  • XML
  • ACTIVE - Active HTML available in 11gR2 requires a download of Javascript libraries and a Flash movie from an Oracle website, so
    must be used on a PC connected to the internet, unless you download the relevant libraries and use the BASE_PATH parameter in the function call to identify their location.

REPORT_SQL_MONITOR_LIST

The REPORT_SQL_MONITOR_LIST function was added in Oracle 11g Release 2 to generate a summary screen, similar to that on the "Monitored SQL Executions" page of Enterprise Manager. There are a number of parameters to filer
the content of the report (shown here), but most of the time you will probably only use the TYPE and REPORT_LEVEL parameters,
similar to those in the REPORT_SQL_MONITOR function. The query below shows how the function can be used.

SET LONG 1000000
SET LONGCHUNKSIZE 1000000
SET LINESIZE 1000
SET PAGESIZE 0
SET TRIM ON
SET TRIMSPOOL ON
SET ECHO OFF
SET FEEDBACK OFF

SPOOL /host/report_sql_monitor_list.htm
SELECT DBMS_SQLTUNE.report_sql_monitor_list(
  type         => ‘HTML‘,
  report_level => ‘ALL‘) AS report
FROM dual;
SPOOL OFF

Examples of the output for each available TYPE are displayed below.

  • TEXT
  • HTML
  • XML
  • ACTIVE - Active HTML is not currently supported, but the parameter list, specifically the BASE_PATH, suggest it will be supported in future.

REPORT_SQL_DETAIL

Although not documented as part of Real-Time SQL Monitoring, the REPORT_SQL_DETAIL function added in Oracle 11g Release 2 returns a report containing SQL monitoring information. Once again, it has several parameters (shown
here
), but you will probably only use a subset of them to target specific SQL statements, as shown below.

SET LONG 1000000
SET LONGCHUNKSIZE 1000000
SET LINESIZE 1000
SET PAGESIZE 0
SET TRIM ON
SET TRIMSPOOL ON
SET ECHO OFF
SET FEEDBACK OFF

SPOOL /host/report_sql_detail.htm
SELECT DBMS_SQLTUNE.report_sql_detail(
  sql_id       => ‘526mvccm5nfy4‘,
  type         => ‘ACTIVE‘,
  report_level => ‘ALL‘) AS report
FROM dual;
SPOOL OFF

Examples of the output for each available TYPE are displayed below.

Active HTML Reports Offline

As mentioned previously, by default Active HTML available in 11gR2 require a download of Javascript libraries and a Flash movie from an Oracle website, so must be used on a PC connected to the internet. An alternative to this is to
download the relevant files to a HTTP server on your network (or local machine) and use the BASE_PATH parameter to reference those files rather than the Oracle website.

To show this I will create a new directory under a HTTP server on my network and download the relevant files to it.

mkdir -p /var/www/html/sqlmon
cd /var/www/html/sqlmon
wget --mirror --no-host-directories --cut-dirs=1 http://download.oracle.com/otn_software/emviewers/scripts/flashver.js
wget --mirror --no-host-directories --cut-dirs=1 http://download.oracle.com/otn_software/emviewers/scripts/loadswf.js
wget --mirror --no-host-directories --cut-dirs=1 http://download.oracle.com/otn_software/emviewers/scripts/document.js
wget --mirror --no-host-directories --cut-dirs=1 http://download.oracle.com/otn_software/emviewers/sqlmonitor/11/sqlmonitor.swf

When calling functions in the DBMS_SQLTUNE package, I use the BASE_PATH parameter with the value of "http://192.168.0.4/sqlmon" so the active report will use the local copies of the files, rather than accessing
them from the internet.

SET LONG 1000000
SET LONGCHUNKSIZE 1000000
SET LINESIZE 1000
SET PAGESIZE 0
SET TRIM ON
SET TRIMSPOOL ON
SET ECHO OFF
SET FEEDBACK OFF

SPOOL /host/report_sql_monitor.htm
SELECT DBMS_SQLTUNE.report_sql_monitor(
  sql_id       => ‘526mvccm5nfy4‘,
  type         => ‘ACTIVE‘,
  report_level => ‘ALL‘,
  base_path    => ‘http://192.168.0.4/sqlmon‘) AS report
FROM dual;
SPOOL OFF

Views

The SQL monitoring functionality accesses a number of existing views, but two new dynamic performance views have been added specifically as part of it.

For more information see:

时间: 2024-10-20 10:59:56

Real-Time SQL Monitoring using DBMS_SQLTUNE的相关文章

【SQL Performance】实时SQL监控功能(Real-Time SQL Monitoring)

概述 使用条件 监视对象 查看实时SQL监控结果的方法 DBMS_SQLTUNE包的以下子程序包 动态视图 Enterprise ManagerEM 相关参数 各版本变化 实时SQL监控使用的例子 参考 概述 实时SQL监控功能(Real-Time SQL Monitoring)是Oracle11g推出的功能,通过这个功能可以实时地监视执行中的SQL性能. 使用条件 要想使用实时SQL监控功能(Real-Time SQL Monitoring),必须满足以下几个条件 ?EE版本,购买了Diagn

Real-Time SQL Monitoring

Real-Time SQL Monitoring可以在sql运行的时候监控其性能. 缺省情况下,单个sql执行花费的CPU或I/O时间超过5秒或sql并行执行的时候,Real-Time SQL Monitoring会自动启动. 可以通过视图v$sql_monitor.v$sql_plan_monitor来查看sql语句运行时的统计信息. 在结合以下视图,可以获取更多的信息: v$active_session_history.v$session.v$session_longops.v$sql.v$

Oracle 11g Articles

发现一个比较有意思的网站,http://www.oracle-base.com/articles/11g/articles-11g.php Oracle 11g Articles Oracle Database 11g: New Features For Administrators OCP Exam Articles Oracle Database 11g Release 1: Miscellaneous Articles Oracle Database 11g Release 2: Misc

sql monitor生成不了报告& FFS hint不生效两个问题思考

事情的发生就是这么偶然,一步步的深入才能汲取到更深入的知识~~ -------------------START-------------------------------------------   来了一个query running longer than 4hours的邮件,来看看里面有哪些sql: SID    SERIAL#    INST_ID SQL_ID        Run_in_sec OS_user     MACHINE       SQL_TEXT         

EBS_DBA_优化:掌握SQL Monitor这些特性,SQL优化将如有神助! (转)

SQL分析的苦与痛 对于线上的SQL语句,很多DBA都总会有一些疑问,看着执行计划cost还不错,但是实际执行的时候效果却有天壤之别,这是为什么呢? 对于一个庞大的SQL语句,看着得到的执行计划却不知道瓶颈在哪里,SQL语句太复杂,执行计划看起来更复杂,要读明白它掌握要领也不是一件容易的事情. 偶尔会有一些朋友问我,怎么去读一个执行计划,这个无论说得怎么细,似乎都不得要领,毕竟纯文字描述和图形的效果还是有很大的差别. 如果你在11g的版本中,SQL Monitor就是一个大大的福利,上面的问题可

Oracle 11g实时SQL监控 v$sql_monitor

Oracle 11g实时SQL监控: 前面提到,在Oracle Database 11g中,v$session视图增加了一些新的字段,这其中包括SQL_EXEC_START和SQL_EXEC_ID, 这两个字段实际上代表了Oracle 11g的一个新特性:实时的SQL监控(Real Time SQL Monitoring). 在Oracle 11g之前的版本,长时间运行的SQL可以通过监控v$session_longops来观察,当某个操作执行时间超过6秒, 就会被记录在v$session_lo

SQL Profile 如何使用

这些文章是我从<基于Oracle的SQL优化> 书中找到的 可以参考一下我的实验过程 SQL Profile Oracle10G中的SQL Profile可以说是Oracle 9i中的Stored Outline的进化. Stored Outline能够实现的功能SQL Profile也完全能够实现. 与Stored Outline相比,SQL Profile具备如下优点: 更容易生成.更改和控制. 在对SQL语句的支持上做得更好,也就是说适用范围更广. 使用SQL Profile可以容易实现

使用Operations Manager监视Windows Server和SQL Server

在这个实验章节中通过监控Windows Server.SQL Server.来了解使用Operations Manager监控企业基础架构.这里需要下载 1. System Center Management Pack for Windows Server Operating System管理包 2. System Center Management Pack for SQL Server管理包 http://down.51cto.com/data/1895686 一. 监视Windows Se

11g新特性-自动sql调优(Automatic SQL Tuning)

11g新特性-自动sql调优(Automatic SQL Tuning) 在Oracle 10g中,引进了自动sql调优特性.此外,ADDM也会监控捕获高负载的sql语句. 在Oracle 11g中,通过运行sql tuning advisor加强了自动sql调优功能.默认情况下是每天夜间通过自动任务的方式运行"自动sql调优". 自动sql调优的过程: 1.识别需要调优的sql语句  根据AWR中的CPU和I/O负载来识别 2.调优,生成新的sql profile 在维护窗口(mai