欢迎投稿

今日深度:

Real-Time SQL Monitoring using DBMS_SQLTUNE,dbmssqltune

Real-Time SQL Monitoring using DBMS_SQLTUNE,dbmssqltune


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.

  • Introduction
  • MONITOR Hint
  • REPORT_SQL_MONITOR
  • REPORT_SQL_MONITOR_LIST
  • REPORT_SQL_DETAIL
  • Active HTML Reports Offline
  • Views

Related articles.

  • Explain Plan Usage
  • DBMS_XPLAN : Display Oracle Execution Plans
  • SQL trace, 10046, trcsess and tkprof in Oracle

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.

  • XML
  • ACTIVE - Active HTML is default type.

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.

  • V$SQL_MONITOR
  • V$SQL_PLAN_MONITOR

For more information see:

  • Oracle Database 11g: Real-Time SQL Monitoring
  • Real-Time SQL Monitoring
  • DBMS_SQLTUNE
  • MONITOR Hint
  • Explain Plan Usage
  • DBMS_XPLAN : Display Oracle Execution Plans
  • SQL trace, 10046, trcsess and tkprof in Oracle

教一个oracle的问题本地动态SQL执行,与DBMS_SQL包执行这两种分别是什? DBMS_SQL包又是什

本地执行EXECUTE IMMEIDIATE和DBMS_SQL都是用执行SQL语句的
前者方便,好理解。。适用于一些简单的,不是特别长,不需要多次循环执行的SQL
后者在多次循环执行方面效率更高。。但使用起来复杂些,不好理解。。但DBMS_SQL支持超长SQL,在复杂的动态SQL语句方面也比EXECUTE IMMEIDIATE好。
EXECUTE IMMEIDIATE对执行的SQL语句有长度限制,过长就不行了。。
 

error CS0246: 找不到类型或命名空间名称“SqlConnection”(是否缺少 using 指令或程序集引用?)

是要连接数据库吧
在那页代码上面添加个
using System.Data.SqlClient; 引用就行了
 

www.htsjk.Com true http://www.htsjk.com/shujukunews/4272.html NewsArticle Real-Time SQL Monitoring using DBMS_SQLTUNE,dbmssqltune 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 d...
相关文章
    暂无相关文章
评论暂时关闭