Data Dictionary   «Prev  Next»
Lesson 3Customizing DBA performance queries
ObjectiveDescribe variations in performance queries.

Customizing DBA Performance Queries and their Variations

To effectively diagnose performance problems, statistics must be available. Oracle generates many types of cumulative statistics for the system, sessions, and individual SQL statements. Oracle also tracks cumulative statistics on segments and services. When analyzing a performance problem in any of these scopes, you typically look at the change in statistics (delta value) over the period of time you are interested in. Specifically, you look at the difference between
  1. the cumulative value of a statistic at the start of the period and
  2. the cumulative value at the end.

Cumulative values for statistics are generally available through dynamic performance views, such as the V$SESSTAT and V$SYSSTAT views. Note that the cumulative values in dynamic views are reset when the database instance is shutdown. The Automatic Workload Repository (AWR) automatically persists the cumulative and delta values for most of the statistics at all levels except the session level. This process is repeated on a regular time period and the result is called an AWR snapshot. The delta values captured by the snapshot represent the changes for each statistic over the time period.
  • Metric Statistic: Another type of statistic collected by Oracle is called a metric. A metric is defined as the rate of change in some cumulative statistic. That rate can be measured against a variety of units, including time, transactions, or database calls. For example, the number database calls per second is a metric. Metric values are exposed in some V$ views, where the values are the average over a fairly small time interval, typically 60 seconds. A history of recent metric values is available through V$ views, and some of the data is also persisted by AWR snapshots. A third type of statistical data collected by Oracle is sampled data. This sampling is performed by the active session history (ASH) sampler. ASH samples the current state of all active sessions. This data is collected into memory and can be accessed by a V$ view. It is also written out to persistent store by the AWR snapshot processing.

Advanced Oracle SQL Tuning

Using the SGA snapshot routine to gather Performance Statistics

There are many public-domain scripts that can be used to get a snapshot of Oracle performance. As a review, examine the use of V$ views below. The DBA will use a script like this one to look inside a running Oracle database. Your specific needs may not require a full snapshot of all possible values. For example, if you have little freelist contention, you may choose not to run the buffer busy wait statistics. As we have discussed, there are many SQL methods for extracting performance information from Oracle. Each may return identical results, but with different levels of performance.
V$SYSSTAT displays system statistics. To find the name of the statistic associated with each statistic number (STATISTIC#), query the V$STATNAME view.

V$SYSSTAT is a Dynamic Performance View

Question: How is the Buffer Hit Ratio related to V$SYSSTAT when conducting performance tuning in Oracle?
V$SYSSTAT is a dynamic performance view in the Oracle RDBMS that displays system statistics. It includes statistics on user activity, redo log activity, enqueue activity, cache activity, operating system activity, Real Application Clusters (RAC) activity, SQL activity, and debug activity.
The columns in the V$SYSSTAT view are:
  1. STATISTIC#: The number of the statistic.
  2. NAME: The name of the statistic.
  3. CLASS: A number representing one or more statistics classes. The following class numbers are additive:
    1. 1 - User
    2. 2 - Redo
    3. 4 - Enqueue
    4. 8 - Cache
    5. 16 - OS
    6. 32 - RAC
    7. 64 - SQL
    8. 128 - Debug
  4. VALUE: The value of the statistic.
  5. STAT_ID: The identifier of the statistic.
  6. CON_ID: The ID of the container to which the data pertains.

  • Associated Statistic
    To find the name of the statistic associated with each statistic number, you can query the V$STATNAME view. The V$SYSSTAT view is a valuable tool for monitoring the performance of an Oracle database. It can be used to identify potential problems and bottlenecks.
    Examples:
    Here are some examples of how the V$SYSSTAT view can be used:
    1. To monitor user activity, you can query the V$SYSSTAT view for statistics such as the number of active sessions, the number of logical reads, and the number of physical reads.
    2. To monitor redo log activity, you can query the V$SYSSTAT view for statistics such as the number of redo entries generated, the number of redo blocks written, and the number of redo blocks flushed.
    3. To monitor enqueue activity, you can query the V$SYSSTAT view for statistics such as the number of enqueues granted, the number of enqueues denied, and the number of enqueues in the wait list.
    4. To monitor cache activity, you can query the V$SYSSTAT view for statistics such as the number of cache hits, the number of cache misses, and the number of cache cold misses.
    5. To monitor operating system activity, you can query the V$SYSSTAT view for statistics such as the number of CPU seconds used, the number of physical reads, and the number of physical writes.
    6. To monitor RAC activity, you can query the V$SYSSTAT view for statistics such as the number of RAC transactions, the number of RAC blocks transferred, and the number of RAC deadlocks.
    7. To monitor SQL activity, you can query the V$SYSSTAT view for statistics such as the number of SQL statements executed, the number of SQL rows processed, and the number of SQL errors.
    8. To monitor debug activity, you can query the V$SYSSTAT view for statistics such as the number of debug events, the number of debug messages, and the number of debug errors.

    The V$SYSSTAT view is a powerful tool that can be used to monitor the performance of an Oracle database. By understanding the statistics in this view, you can identify potential problems and bottlenecks, and take steps to improve the performance of your database.
  • Computing Data buffer hit ratio in Oracle
    Consider the following two queries to compute and display the data buffer hit ratio. This is an excellent example of how the DBA can write the same query in two different ways.
    Buffer Hit Ratio
    • The first query sums the various statistics value in a single select from `v$sysstat`.
    • The second query joins the `v$sysstat` table against itself (4 times).
    • Both queries use the `v$sysstat` view and look at physical reads as a percentage of `consistent_gets` plus `db_block_gets`, but do this in different ways. In this example, the second query will run faster than the first query because it joins the `v$sysstat` table against itself.


First Query sums the various statistics value in a single select from `v$sysstat`.
SELECT a.value + b.value "logical_reads",
       c.value "phys_reads",
       d.value "phys_writes",
       ROUND(100 * ((a.value + b.value) - c.value) / (a.value + b.value))
       "BUFFER HIT RATIO"
FROM v$sysstat a, v$sysstat b, v$sysstat c, v$sysstat d
WHERE a.statistic# = 37
  AND b.statistic# = 38
  AND c.statistic# = 39
  AND d.statistic# = 40;

  • Compatibility: This query is valid for Oracle 19c. The v$sysstat dynamic performance view and its statistic# column remain available in Oracle 19c.
  • Potential issues: Ensure that the statistics IDs (statistic#) provided (37, 38, 39, 40) are valid in the Oracle 19c environment. These IDs map to specific metrics, and if the mapping has changed in 19c, the query may not return the expected results.
This query calculates the buffer hit ratio, which is an important metric for database performance.

Second Query: The second query joins the `v$sysstat` table against itself (4 times).
prompt ***********************
prompt Hit Ratio Section
prompt ***********************
prompt ========================
prompt BUFFER HIT RATIO
prompt ========================
prompt (should be > 70, else increase db_block_buffers in init.ora)

SELECT TRUNC(
  (1 - (SUM(DECODE(name, 'physical reads', value, 0)) /
        (SUM(DECODE(name, 'db block gets', value, 0)) +
         SUM(DECODE(name, 'consistent gets', value, 0)))))
  ) * 100
  "Buffer Hit Ratio"
FROM v$sysstat;

  • Compatibility: This query uses the DECODE function and performs aggregate operations (SUM). These functions and the v$sysstat view are fully supported in Oracle 19c.
  • Potential issues: Ensure the name column in v$sysstat contains the exact names ('physical reads', 'db block gets', 'consistent gets') used in this query. Any changes in metric naming conventions (though unlikely) could impact the results.
This script calculates buffer hit ratios and uses the v$sysstat view to determine logical reads, physical writes, and other relevant metrics.
Both queries use the v$sysstat 1) view and 2) look at physical reads as a percentage of consistent_gets plus db_block_gets, but do this in different ways. In this example, the second query will run faster than the first query, because it joins the V$SYSSTAT table against itself.
Both queries use the V$SYSSTAT view and look at physical reads as a percentage of consistent_gets plus db_block_gets, but do this in different ways. It is common to see identical results come from different coding techniques. Note that this is the accumulated buffer hit ratio since database start time, so it does not indicate the current hit ratio. In this example, the second query runs faster than the first query, because it joins the V$SYSSTAT table against itself.
The next lesson looks at a technique to easily apply what we've learned about query performance, and create an automated alert monitor.

Customizing DBA Performance -Exercise

Before you move on to, click the Exercise link below to write an SGA snapshot routine using the V$ views.
Customizing DBA Performance - Exercise

SEMrush Software 3 SEMrush Banner 3