This chapter describes how to monitor database operations.
This chapter contains the following topics:
A database operation is a user-defined logical object that includes session activity between two points in time. A database operation contains a set of database tasks, for example, a batch job or Extraction, Transformation, and Loading (ETL) processing job.
A simple database operation is a single SQL statement or PL/SQL procedure or function. A composite database operation is activity between two points in time in a database session, with each session defining its own beginning and end points. A session can participate in at most one composite database operation at a time.
A database operation is uniquely identified by its name and execution ID. Each operation can be executed many times. Each execution is uniquely identifiable.
Real-Time Database Operations provides the ability to monitor composite operations automatically. The database automatically monitors parallel queries, DML, and DDL statements as soon as execution begins. By default, Real-Time SQL Monitoring automatically starts when a SQL statement runs in parallel, or when it has consumed at least 5 seconds of CPU or I/O time in a single execution.
The SQL monitoring feature is enabled by default when the STATISTICS_LEVEL
initialization parameter is either set to TYPICAL
(the default value) or ALL
. Using the DBMS_SQL_MONITOR
package, you can start and stop, monitor, and report on database operations.
This section contains the following topics:
See Also:
Oracle Database Concepts for a brief conceptual overview of database operations
Real-time SQL Monitoring enables you to monitor a single SQL statement or PL/SQL program unit. Database operations extend this functionality by enabling you to treat a set of statements or procedures as a named, uniquely identified, and re-executable unit.
In general, monitoring database operations is useful for the following users:
DBAs whose responsibilities include identifying expensive (high response time) SQL statements and PL/SQL functions
DBAs who manage batch jobs in a data warehouse or OLTP system
Application or database developers who need to monitor the activities related to particular operations, for example, Oracle Data Pump operations
Monitoring database operations is useful for performing the following tasks:
Tracking and reporting
Tracking requires first defining a database operation, for example, though DBMS_SQL_MONITOR
, OCI, or JDBC APIs. You can define an operation from a different session from the one that you are currently using. The database infrastructure determines what to track on behalf of the defined operation.
You can generate reports on the operation. For example, your tuning task may involve determining which SQL statements run on behalf of a specific batch job, what their execution statistics were, what was occurring in the database when the operation was executing, and so on.
Monitoring execution progress
This task involves monitoring a currently executing database operation. The information is particularly useful when you are investigating why an operation is taking a long time to complete.
Monitoring resource usage
You may want to detect when a SQL execution uses excessive CPU, issues an excessive amount of I/O, or takes a long time to complete. With Oracle Database Resource Manager (the Resource Manager), you can configure thresholds for each consumer group that specify the maximum resource usage for all SQL executions in the group. When a SQL operation reaches a specified threshold, the Resource Manager can switch the operation into a lower-priority consumer group, terminate the session or call, or log the event. You can then monitor these SQL operations.
Tuning for response time
When tuning a database operation, you typically aim to improve the response time. Often the database operation performance issues are mainly SQL performance issues.
For simple operations, Real-Time SQL Monitoring helps determine where a currently executing SQL statement is in its execution plan and where the statement is spending its time. You can also see the breakdown of time and resource usage for recently completed statements. In this way, you can better determine why a particular operation is expensive.
Typical use cases for Real-Time SQL Monitoring include the following:
A frequently executed SQL statement is executing more slowly than normal. You must identify the root cause of this problem.
A database session is experiencing slow performance.
A parallel SQL statement is taking a long time. You want to determine how the server processes are dividing the work.
In OLTP and data warehouse environments, a job often logically groups related SQL statements. The job can span multiple concurrent sessions.
Typical use cases for monitoring composite operations include the following:
A periodic batch job containing many SQL statements must complete in a certain number of hours, but took twice as long as expected.
After a database upgrade, the execution time of an important batch job doubled. To resolve this problem, you must collect enough relevant statistical data from the batch job before and after the upgrade, compare the two sets of data, and then identify the changes.
Packing a SQL tuning set (STS) took far longer than anticipated. To diagnose the problem, you need to know what was being executed over time. Because this issue cannot be easily reproduced, you need to monitor the process while it is running.
See Also:
This topic provides an overview of the architecture and attributes of database operations.
This section contains the following topics:
Real-Time SQL Monitoring is a feature of the Oracle Database Tuning Pack. Database operations are enabled when the CONTROL_MANAGEMENT_PACK_ACCESS
initialization parameter is set to DIAGNOSTIC+TUNING
(default).
The following figure gives an overview of the architecture for database operations.
Figure 21-1 Architecture for Database Operations
As shown in the preceding graphic, the DBMS_SQL_MONITOR
PL/SQL package defines database operations. After monitoring is initiated, the database stores metadata about the database operations in ASH and AWR. The database refreshes monitoring statistics in close to real time as each monitored statement executes, typically once every second. The database stores the operational data (the statements and metadata about the statements) in the SGA, and then periodically writes it to disk.
Every monitored database operation has an entry in the V$SQL_MONITOR
view. This entry tracks key performance metrics collected for the execution, including the elapsed time, CPU time, number of reads and writes, I/O wait time, and various other wait times. The V$SQL_PLAN_MONITOR
view includes monitoring statistics for each operation in the execution plan of the SQL statement being monitored. You can access reports by using DBMS_SQL_MONITOR.REPORT_SQL_MONITOR
, which has an Oracle Enterprise Manager Cloud Control (Cloud Control) interface.
See Also:
Oracle Database Reference to learn about V$SQL_MONITOR
, V$SQL_MONITOR
, and CONTROL_MANAGEMENT_PACK_ACCESS
Oracle Database PL/SQL Packages and Types Reference to learn about DBMS_SQLTUNE
and DBMS_SQL_MONITOR
packages
A composite database operation consists of the activity of one or more concurrent sessions between two points in time. SQL statements or PL/SQL procedures that execute within these two points in time are part of the composite operation.
Composite database operations can also be defined in the database kernel. Typical composite operations include SQL*Plus scripts, batch jobs, and ETL processing.
The DBMS_SQL_MONITOR.BEGIN_OPERATION
function defines a database operation.
A database operation is uniquely identified by the following information:
Database operation name
This is a user-created name such as daily_sales_report
. The operation name is the same for a job even if it is executed concurrently by different sessions or on different databases. Database operation names do not reside in different namespaces.
Database operation execution ID
Two or more occurrences of the same database operation can run at the same time, with the same name but different execution IDs. This numeric ID uniquely identifies different executions of the same database operation.
The database automatically creates an execution ID when you begin a database operation. You can also specify a user-created execution ID.
Optionally, can specify the session ID and session serial number in which to start the database operations. Thus, one database session can start a database operation defined in a different database session.
The database uses the following triplet of values to identify each SQL and PL/SQL statement monitored in the V$SQL_MONITOR
view, regardless of whether the statement is included in a database operation:
SQL identifier to identify the SQL statement (SQL_ID
)
Start execution timestamp (SQL_EXEC_START
)
An internally generated identifier to ensure that this primary key is truly unique (SQL_EXEC_ID
)
You can use zero or more additional attributes to describe and identify the characteristics of a database operation. Each attribute has a name and value. For example, for operation daily_sales_report
, you might define the attribute db_name
and assign it the value prod
.
See Also:
Oracle Database Reference to learn about the V$SQL_MONITOR
view
Oracle Database PL/SQL Packages and Types Reference to learn about the DBMS_SQL_MONITOR.BEGIN_OPERATION
function
This section contains the following topics:
The Monitored SQL Executions shows information such as the SQL ID, database time, and I/O requests.
To access the Monitored SQL Executions page:
Access the Database Home page, as described in "Accessing the Database Home Page in Cloud Control."
From the Performance menu, select SQL Monitoring.
The Monitored SQL Executions page appears.
The DBMS_SQL_MONITOR
package defines the beginning and ending of a database operation, and generates a report of the database operations.
Table 21-1 DBMS_SQL_MONITOR
Program Unit | Description |
---|---|
|
This function associates a session with a database operation. Starting in Oracle Database 12c Release 2 (12.2), you can use |
|
This function disassociates a session from the specified database operation execution. |
|
This function accepts several input parameters to specify the execution, the level of detail in the report, and the report type. If no parameters are specified, then the function generates a text report for the last execution that was monitored. |
COMPARE_DATABASE_OPERATIONS |
This function compares two database operations and produces a report. The report_level parameter indicate the level of comparison. BASIC compares the overall activity of the two operations. TYPICAL (the default) includes the top SQL or PL/SQL statements within the two operations. ALL include all SQL statements within the operations. |
TREND_DATABASE_OPERATION |
This function uses all executions of the database operation within begin_date and end_date to generate a trend line for the various statistics. If you do not specify a list of statistics, then the trend line uses all top-level statistics, including BUFFER_GETS , ELAPSED_TIME , and CPU_TIME . |
See Also:
Oracle Database PL/SQL Packages and Types Reference to learn about the DBMS_SQL_MONITOR
package
You can monitor the statistics for SQL statement execution using the V$SQL_MONITOR
, V$SQL_PLAN_MONITOR
, and V$SQL_MONITOR_SESSTAT
views.
Table 21-2 summarizes these views.
Table 21-2 Views for Database Operations Monitoring
View | Description |
---|---|
This view contains global, high-level information about the top SQL statements in a database operation. Each monitored SQL statement has an entry in this view. Each row contains a SQL statement whose statistics are accumulated from multiple sessions and all of its executions in the operation. The primary key is the combination of the columns The
|
|
This view contains the statistics for all sessions involved in the database operation. Most of the statistics are cumulative. The database stores the statistics in XML format instead of using each column for each statistic. This view is primarily intended for the report generator. Oracle recommends that you use |
|
This view contains monitoring statistics for each step in the execution plan of the monitored SQL statement. The database updates statistics in |
You can use the preceding views with the following views to get additional information about the monitored execution:
V$ACTIVE_SESSION_HISTORY
V$SESSION
V$SESSION_LONGOPS
V$SQL
V$SQL_PLAN
See Also:
Oracle Database Reference to learn about the V$
views for database operations monitoring
This section explains the basic tasks in database operations monitoring. Basic tasks are as follows:
"Enabling and Disabling Monitoring of Database Operations"
This task explains how you can enable automatic monitoring of database operations at the system and statement level.
"Creating a Database Operation"
This section explains how you can define the beginning and end of a database operation using PL/SQL.
"Reporting on Database Operations Using SQL Monitor"
This section explains how you can generate and interpret reports on a database operation.
Use initialization parameters to enable or disable monitoring.
This section contains the following topics:
The SQL monitoring feature is enabled by default when the STATISTICS_LEVEL
initialization parameter is either set to TYPICAL
(the default value) or ALL
. SQL monitoring starts automatically for all long-running queries.
Prerequisites
Because SQL monitoring is a feature of the Oracle Database Tuning Pack, the CONTROL_MANAGEMENT_PACK_ACCESS
initialization parameter must be set to DIAGNOSTIC+TUNING
(the default value).
Assumptions
This tutorial assumes the following:
The STATISTICS_LEVEL
initialization parameter is set to BASIC
.
You want to enable automatic monitoring of database operations.
To enable monitoring of database operations:
Connect SQL*Plus to the database with the appropriate privileges, and then query the current database operations settings.
For example, run the following SQL*Plus command:
SQL> SHOW PARAMETER statistics_level NAME TYPE VALUE ----------------------------------- ----------- ----- statistics_level string BASIC
Set the statistics level to TYPICAL
.
For example, run the following SQL statement:
SQL> ALTER SYSTEM SET STATISTICS_LEVEL='TYPICAL';
See Also:
Oracle Database Reference to learn about the STATISTICS_LEVEL
and CONTROL_MANAGEMENT_PACK_ACCESS
initialization parameter
When the CONTROL_MANAGEMENT_PACK_ACCESS
initialization parameter is set to DIAGNOSTIC+TUNING
, you can use hints to enable or disable monitoring of specific SQL statements.
The MONITOR
hint enables monitoring, whereas the NO_MONITOR
hint disables monitoring.
Two statement-level hints are available to force or prevent the database from monitoring a SQL statement. To force SQL monitoring, use the MONITOR
hint:
SELECT /*+ MONITOR */ SYSDATE FROM DUAL;
This hint is effective only when the CONTROL_MANAGEMENT_PACK_ACCESS
parameter is set to DIAGNOSTIC+TUNING
. To prevent the hinted SQL statement from being monitored, use the NO_MONITOR
reverse hint.
Assumptions
This tutorial assumes the following:
Database monitoring is currently enabled at the system level.
You want to disable automatic monitoring for the statement SELECT * FROM sales ORDER BY time_id
.
To disable monitoring of database operations for a SQL statement:
Execute the query with the NO_MONITOR
hint.
For example, run the following statement:
SQL> SELECT * /*+NO_MONITOR*/ FROM sales ORDER BY time_id;
See Also:
Oracle Database SQL Language Reference for information about using the MONITOR
and NO_MONITOR
hints
Creating a database operation involves supplying a name and defining its beginning and end times. Start a database operation by using the DBMS_SQL_MONITOR.BEGIN_OPERATION
function, and end it by using the DBMS_SQL_MONITOR.END_OPERATION
procedure.
To begin the operation in a different session, specify the combination of session_id
and serial_num
. The BEGIN_OPERATION
function returns the database operation execution ID. If dbop_exec_id
is null, then the database generate a unique value.
Guidelines
A single namespace exists for database operations. Thus, name collisions are possible. Oracle recommends the following naming convention: component_name.subcomponent_name.operation name. For operations inside the database, Oracle recommends using ORA
for the component name. For example, a materialized view refresh could be named ORA.MV.refresh
. An E-Business Suite payroll function could be named EBIZ.payroll
.
Assumptions
This example assumes the following:
You are an administrator and want to monitor statements in a session started by user sh
.
You want to monitor queries of the sh.sales
table and sh.customers
table.
You want these two queries to be monitored as a database operation named sh_count
.
The following example illustrates how to use the DBMS_SQL_MONITOR
package to begin and end the database operation.
Table 21-3 Creating a Database Operation
SYSTEM Session | SH Session | DESCRIPTION |
---|---|---|
SQL> CONNECT SYSTEM Enter password: ********* Connected. |
n/a |
Start SQL*Plus and connect as a user with the administrator privileges. |
n/a |
SQL> CONNECT sh Enter password: ****** Connected. |
In a different terminal, start SQL*Plus and connect as a user as user |
SELECT SID, SERIAL# FROM V$SESSION WHERE USERNAME = 'SH'; SID SERIAL# ---------- ---------- 121 13397 |
n/a |
In the |
VARIABLE eid NUMBER BEGIN :eid:=DBMS_SQL_MONITOR.BEGIN_OPERATION ('sh_count', null, null, null, '121', '13397'); END; / PRINT eid EID ---------- 2 |
n/a |
In the |
n/a |
SELECT count(*) FROM sh.sales; COUNT(*) ---------- 918843 SELECT COUNT(*) FROM sh.customers; COUNT(*) ---------- 55500 |
In the |
BEGIN DBMS_SQL_MONITOR.END_OPERATION ('sh_count',:eid); END; / |
n/a |
End the database operation by specifying the operation name and execution ID. |
COL DBOP_NAME FORMAT a10 COL STATUS FORMAT a10 COL ID FORMAT 999 SELECT DBOP_NAME, DBOP_EXEC_ID AS ID, STATUS, CPU_TIME, BUFFER_GETS FROM V$SQL_MONITOR WHERE DBOP_NAME IS NOT NULL ORDER BY DBOP_EXEC_ID; DBOP_NAME ID STATUS CPU_TIME GETS ---------- -- ---------- -------- ---- sh_count 1 EXECUTING 24997 65 |
n/a |
Query the metadata for the |
See Also:
Oracle Database PL/SQL Packages and Types Reference to learn about the DBMS_SQL_MONITOR
package
By default, AWR automatically captures SQL monitoring reports in XML format.
The reports capture only SQL statements that are not executing or queued and have finished execution since the last capture cycle. AWR captures reports only for the most expensive statements according to elapsed execution time.
The Monitored SQL Executions page in Enterprise Manager Cloud Control (Cloud Control) summarizes the activity for monitored statements. You can use this page to drill down and obtain additional details about particular statements. The Monitored SQL Executions Details page uses data from several views, including the following:
GV$SQL_MONITOR
GV$SQL_PLAN_MONITOR
GV$SQL_MONITOR_SESSTAT
GV$SQL
GV$SQL_PLAN
GV$ACTIVE_SESSION_HISTORY
GV$SESSION_LONGOPS
DBA_HIST_REPORTS
DBA_HIST_REPORTS_DETAILS
Assumptions
This tutorial assumes the following:
The user sh
is executing the following long-running parallel query of the sales made to each customer:
SELECT c.cust_id, c.cust_last_name, c.cust_first_name, s.prod_id, p.prod_name, s.time_id FROM sales s, customers c, products p WHERE s.cust_id = c.cust_id AND s.prod_id = p.prod_id ORDER BY c.cust_id, s.time_id;
You want to ensure that the preceding query does not consume excessive resources. While the statement executes, you want to determine basic statistics about the database operation, such as the level of parallelism, the total database time, and number of I/O requests.
You use Cloud Control to monitor statement execution.
Note:
To generate the SQL monitor report from the command line, run the REPORT_SQL_MONITOR
function in the DBMS_SQLTUNE
package, as in the following sample SQL*Plus script:
VARIABLE my_rept CLOB BEGIN :my_rept :=DBMS_SQLTUNE.REPORT_SQL_MONITOR(); END; / PRINT :my_rept
To monitor SQL executions:
Access the Monitored SQL Executions page, as described in "Monitored SQL Executions Page in Cloud Control".
In the following graphic, the top row shows the parallel query.
In this example, the query has been executing for 1.4 minutes.
Click the value in the SQL ID column to see details about the statement.
The Monitored SQL Details page appears.
The preceding report shows the execution plan and statistics relating to statement execution. For example, the Timeline column shows when each step of the execution plan was active. Times are shown relative to the beginning and end of the statement execution. The Executions column shows how many times an operation was executed.
In the Overview section, click the link next to the SQL text.
A message shows the full text of the SQL statement.
In the Time & Wait Statistics section, next to Database Time, move the cursor over the largest portion on the bar graph.
A message shows that user I/O is consuming over half of database time.
Database Time measures the amount of time the database has spent working on this SQL statement. This value includes CPU and wait times, such as I/O time. The bar graph is divided into several color-coded portions to highlight CPU resources, user I/O resources, and other resources. You can move the cursor over any portion to view the percentage value of the total.
In the Details section, in the IO Requests column, move the cursor over the I/O requests bar to view the percentage value of the total.
A message appears.
In the preceding graphic, the IO Requests message shows the total number of read requests issued by the monitored SQL. The message shows that read requests form 80% of the total I/O requests.
See Also:
Cloud Control Online Help for descriptions of the elements on the Monitored SQL Executions Details page, and for complete descriptions of all statistics in the report.
Oracle Database Reference to learn about V$SQL_MONITOR
and related views for database operations monitoring