Sei sulla pagina 1di 26

Oracle 10g New Features

Automatic SQL Tuning in Oracle Database 10g


This article the discusses the new features which automate the tuning of SQL statements in
Oracle 10g:

• Overview
• SQL Tuning Advisor
• Managing SQL Profiles
• SQL Tuning Sets
• Useful Views

Overview

In its normal mode the query optimizer needs to make decisions about execution plans in a very
short time. As a result it may not always be able to obtain enough information to make the best
decision. Oracle 10g allows the optimizer to run in tuning mode where it can gather additional
information and make recommendations about how specific statements can be tuned further. This
process may take several minutes for a single statement so it is intended to be used on high-load
resource-intensive statements.

In tuning mode the optimizer performs the following analysis:

• Statistics Analysis - The optimizer recommends the gathering of statistics on objects with
missing or stale statistics. Additional statistics for these objects are stored in an SQL
profile.

• SQL Profiling - The optimizer may be able to improve performance by gathering


additional statistics and altering session specific parameters such as the
OPTIMIZER_MODE. If such improvements are possible the information is stored in an
SQL profile. If accepted this information can then used by the optimizer when running in
normal mode. Unlike a stored outline which fixes the execution plan, an SQL profile may
still be of benefit when the contents of the table alter drastically. Even so, it's sensible to
update profiles periodically. The SQL profiling is not performed when the tuining optimizer
is run in limited mode.

• Access Path Analysis - The optimizer investigates the effect of new or modified indexes
on the access path. It's index recommendations relate to a specific statement so where
necessary it will also suggest the use of the SQL Access Advisor to check the impact of
these indexes on a representative SQL workload.

• SQL Structure Analysis - The optimizer suggests alternatives for SQL statements that
contain structures that may impact on performance. The implementation of these
suggestions requires human intervention to check their validity.

The automatic SQL tuning features are accessible from Enterprise Manager on the "Advisor
Central" page these or from PL/SQL using the DBMS_SQLTUNE package. This article will focus on
the PL/SQL API as the Enterprise Manager interface is reasonably intuative.
SQL Tuning Advisor

In order to access the SQL tuning advisor API a user must be granted the ADVISOR privilege:
CONN sys/password AS SYSDBA
GRANT ADVISOR TO scott;
CONN scott/tiger

2) Overview

The Automatic Database Diagnostic Monitor (ADDM) analyzes data in the Automatic Workload
Repository (AWR) to identify potential performance bottlenecks. For each of the identified issues
it locates the root cause and provides recommendations for correcting the problem. An ADDM
analysis task is performed and its findings and recommendations stored in the database every
time an AWR snapshot is taken provided the STATISTICS_LEVEL parameter is set to TYPICAL
or ALL. The ADDM analysis includes:

• CPU load
• Memory usage
• I/O usage
• Resource intensive SQL
• Resource intensive PL/SQL and Java
• RAC issues
• Application issues
• Database configuration issues
• Concurrency issues
• Object contention

3) Overview of Automatic Storage Management (ASM)

Automatic Storage Management (ASM) simplifies administration of Oracle related files by


allowing the administrator to reference disk groups rather than individual disks and files, which
are managed by ASM. The ASM functionality is an extention of the Oracle Managed Files (OMF)
functionality that also includes striping and mirroring to provide balanced and secure storage. The
new ASM functionality can be used in combination with existing raw and cooked file systems,
along with OMF and manually managed files.

The ASM functionality is controlled by an ASM instance. This is not a full database instance, just
the memory structures and as such is very small and lightweight.

The main components of ASM are disk groups, each of which comprise of several physical disks
that are controlled as a single unit. The physical disks are known as ASM disks, while the files
that reside on the disks are know as ASM files. The locations and names for the files are
controlled by ASM, but user-friendly aliases and directory structures can be defined for ease of
reference.

The level of redundancy and the granularity of the striping can be controlled using templates.
Default templates are provided for each file type stored by ASM, but additional templates can be
defined as needed.

Failure groups are defined within a disk group to support the required level of redundancy. For
two-way mirroring you would expect a disk group to contain two failure groups so individual files
are written to two locations.

In summary ASM provides the following functionality:

• Manages groups of disks, called disk groups.


• Manages disk redundancy within a disk group.
• Provides near-optimal I/O balancing without any manual tuning.
• Enables management of database objects without specifying mount points and
filenames.
• Supports large files.

4) Automatic Workload Repository (AWR) in Oracle Database 10g

AWR Features

The AWR is used to collect performance statistics including:

• Wait events used to identify performance problems.


• Time model statistics indicating the amount of DB time associated with a process from
the V$SESS_TIME_MODEL and V$SYS_TIME_MODEL views.
• Active Session History (ASH) statistics from the V$ACTIVE_SESSION_HISTORY view.
• Some system and session statistics from the V$SYSSTAT and V$SESSTAT views.
• Object usage statistics.
• Resource intensive SQL statements.

The repository is a source of information for several other Oracle 10g features including:

• Automatic Database Diagnostic Monitor


• SQL Tuning Advisor
• Undo Advisor
• Segment Advisor

Snapshots

By default snapshots of the relevant data are taken every hour and retained for 7 days. The
default values for these settings can be altered using:
BEGIN
DBMS_WORKLOAD_REPOSITORY.modify_snapshot_settings(
retention => 43200, -- Minutes (= 30 Days). Current value
retained if NULL.
interval => 30); -- Minutes. Current value retained if
NULL.
END;
/

5) DBMS_FILE_TRANSFER Package in Oracle Database


10g
Oracle 10g has introduced the DBMS_FILE_TRANSFER package which provides an API for
copying binary files between database servers.

• Common Usage Notes


• COPY_FILE
• GET_FILE
• PUT_FILE

Common Usage Notes

All of the the currently supported procedures have some common usage notes listed below:

• The user must have read privilege on the source directory object and write privilege on
the destination directory object.
• The procedure converts directory object names to uppercase unless they are surrounded
by double quotes.
• Files to be copied must be multiples of 512 bytes in size.
• Files to be copied must be equal to or less than 2 terabytes in size.
• File transfers are not transactional.
• Files are copied as binary, so no character conversions are performed.
• File copies can be monitored using the V$SESSION_LONGOPS view.

COPY_FILE

The COPY_FILE procedure allows you to copy binary files from one location to another on the
same server.

GET_FILE

The GET_FILE procedure allows you to copy binary files from a remote server to the local server.

PUT_FILE

The PUT_FILE procedure allows you to copy binary files from the local server to a remote server.

Flashback New Features and Enhancements in Oracle


6)
Database 10g
Oracle9i introduced the DBMS_FLASHBACK package to allow queries to reference older versions
of the database. Oracle 10g has taken this technology a step further making it simpler to use and
much more flexible.

Note: Internally Oracle uses SCNs to track changes so any flashback operation that uses a
timestamp must be translated into the nearest SCN which can result in a 3 second error.

• Flashback Query
• Flashback Version Query
• Flashback Transaction Query
• Flashback Table
• Flashback Drop (Recycle Bin)
• Flashback Database
• Flashback Query Functions

Flashback Query

Flashback Query allows the contents of a table to be queried with reference to a specific point in
time, using the AS OF clause. Essentially it is the same as the DBMS_FLASHBACK functionality
or Oracle9i, but in a more convenient form. For example:
CREATE TABLE flashback_query_test (
id NUMBER(10)
);

SELECT current_scn, TO_CHAR(SYSTIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') FROM


v$database;

CURRENT_SCN TO_CHAR(SYSTIMESTAM
----------- -------------------
722452 2004-03-29 13:34:12

Flashback Version Query

Flashback version query allows the versions of a specific row to be tracked during a specified
time period using the VERSIONS BETWEEN clause

Flashback Transaction Query

Flashback transaction query can be used to get extra information about the transactions listed by
flashback version queries. The VERSIONS_XID column values from a flashback version query
can be used to query the FLASHBACK_TRANSACTION_QUERY view like:

Flashback Table

The FLASHBACK TABLE command allows point in time recovery of individual tables subject to
the following requirements:

• You must have either the FLASHBACK ANY TABLE system privilege or have FLASHBACK
object privilege on the table.
• You must have SELECT, INSERT, DELETE, and ALTER privileges on the table.
• There must be enough information in the undo tablespace to complete the operation.
• Row movement must be enabled on the table (ALTER TABLE tablename ENABLE
ROW MOVEMENT;).

Flashback Drop (Recycle Bin)

In Oracle 10g the default action of a DROP TABLE command is to move the table to the recycle
bin (or rename it), rather than actually dropping it. The PURGE option can be used to permanently
drop a table.
The recycle bin is a logical collection of previously dropped objects, with access tied to the DROP
privilege. The contents of the recycle bin can be shown using the SHOW RECYCLEBIN command
and purged using the PURGE TABLE command. As a result, a previously dropped table can be
recovered from the recycle bin:

Flashback Database

The FLASHBACK DATABASE command is a fast alternative to performing an incomplete recovery.


In order to flashback the database you must have SYSDBA privilege and the flash recovery area
must have been prepared in advance.

If the database is in NOARCHIVELOG it must be switched to ARCHIVELOG mode:


CONN sys/password AS SYSDBA
ALTER SYSTEM SET
log_archive_dest_1='location=d:\oracle\oradata\DB10G\archive\'
SCOPE=SPFILE;
ALTER SYSTEM SET log_archive_format='ARC%S_%R.%T' SCOPE=SPFILE;
ALTER SYSTEM SET log_archive_start=TRUE SCOPE=SPFILE;
SHUTDOWN IMMEDIATE
STARTUP MOUNT
ARCHIVE LOG START
ALTER DATABASE ARCHIVELOG;
ALTER DATABASE OPEN;
Flashback must be enabled before any flashback operations are performed:
CONN sys/password AS SYSDBA
SHUTDOWN IMMEDIATE
STARTUP MOUNT EXCLUSIVE
ALTER DATABASE FLASHBACK ON;
ALTER DATABASE OPEN;

7) Database Security Enhancements in Oracle Database


10g
• Virtual Private Database (VPD) Enhancements
o Column-Level VPD Policy
o Column Masking
o Policy Types
o Application Context Support for Parallel Queries
• Fine-Grained Auditing Enhancements
• Uniform Audit Trail
• Audit Trail Contents
• DBMS_CRYPTO

Virtual Private Database (VPD) Enhancements

Column-Level VPD Policy


In conventional Virtual Private Database the VPD Policy is applied to the whole row. By default a
Column-Level VPD Policy allows you to restrict the rows displayed only if specified columns are
accessed:
Fine-Grained Auditing Enhancements

Fine-grained auditing now includes support for DML statements in addition to queries:

Uniform Audit Trail

The DBA_COMMON_AUDIT_TRAIL view has been added to display the complete audit trail:

• DBA_AUDIT_TRAIL - Standard auditing only (from AUD$).


• DBA_FGA_AUDIT_TRAIL - Fine-grained auditing only (from FGA_LOG$).
• DBA_COMMON_AUDIT_TRAIL - Both standard and fine-grained auditing.

Audit Trail Contents

Several fields have been added to both the standard and fine-grained audit trails:

• EXTENDED_TIMESTAMP - A more precise value than the exising TIMESTAMP column.


• PROXY_SESSIONID - Proxy session serial number when an enterprise user is logging in
via the proxy method.
• GLOBAL_UID - Global Universal Identifier for an enterprise user.
• INSTANCE_NUMBER - The INSTANCE_NUMBER value from the actioning instance.
• OS_PROCESS - Operating system process id for the oracle process.
• TRANSACTIONID - Transaction identifier for the audited transaction. This column can be
used to join to the XID column on the FLASHBACK_TRANSACTION_QUERY view.
• SCN - System change number of the query. This column can be used in flashback
queries.
• SQL_BIND - The values of any bind variables if any.
• SQL_TEXT - The SQL statement that initiated the audit action.

The SQL_BIND and SQL_TEXT columns are only populated when the
AUDIT_TRAIL=DB_EXTENDED initialization parameter is set:

DBMS_CRYPTO

The DBMS_CRYPTO package is a replacement for the DBMS_OBFUSCATION_TOOLKIT package


available in Oracle 8i and 9i. The new package is easier to use and contains more cryptographic
algorithms:

• Cryptographic algorithms - DES, 3DES, AES, RC4, 3DES_2KEY


• Padding forms - PKCS5, zeroes
• Block cipher chaining modes - CBC, CFB, ECB, OFB
• Cryptographic hash algorithms - MD5, SHA-1, MD4
• Keyed hash (MAC) algorithms - HMAC_MD5, HMAC_SH1
• Cryptographic pseudo-random number generator - RAW, NUMBER, BINARY_INTEGER
• Database types - RAW, CLOB, BLOB
8) MERGE Statement Enhancements in Oracle Database
10g
Oracle 10g includes a number of amendments to the MERGE statement making it more flexible.

• Test Table
• Optional Clauses
• Conditional Operations
• DELETE Clause

Test Table

The following examples use the table defined below.


CREATE TABLE test1 AS
SELECT *
FROM all_objects
WHERE 1=2;

Optional Clauses

The MATCHED and NOT MATCHED clauses are now optional making all of the following examples
valid.
-- Both clauses present.
MERGE INTO test1 a
USING all_objects b
ON (a.object_id = b.object_id)
WHEN MATCHED THEN
UPDATE SET a.status = b.status
WHEN NOT MATCHED THEN
INSERT (object_id, status)
VALUES (b.object_id, b.status);

9) Oracle Data Pump in Oracle Database 10g


Oracle Data Pump is a newer, faster and more flexible alternative to the "exp" and "imp" utilities
used in previous Oracle versions. In addition to basic import and export functionality data pump
provides a PL/SQL API and support for external tables.

• Getting Started
• Table Exports/Imports
• Schema Exports/Imports
• Database Exports/Imports
• Miscellaneous Information
• Data Pump API
• External Tables
• Help

Getting Started
For the examples to work we must first unlock the SCOTT account and create a directory object it
can access:
CONN sys/password@db10g AS SYSDBA
ALTER USER scott IDENTIFIED BY tiger ACCOUNT UNLOCK;
GRANT CREATE ANY DIRECTORY TO scott;

CREATE OR REPLACE DIRECTORY test_dir AS '/u01/app/oracle/oradata/';


GRANT READ, WRITE ON DIRECTORY test_dir TO scott;

Table Exports/Imports

The TABLES parameter is used to specify the tables that are to be exported. The following is an
example of the table export and import syntax:
expdp scott/tiger@db10g tables=EMP,DEPT directory=TEST_DIR
dumpfile=EMP_DEPT.dmp logfile=expdpEMP_DEPT.log

impdp scott/tiger@db10g tables=EMP,DEPT directory=TEST_DIR


dumpfile=EMP_DEPT.dmp logfile=impdpEMP_DEPT.log
For example output files see expdpEMP_DEPT.log and impdpEMP_DEPT.log.

The TABLE_EXISTS_ACTION=APPEND parameter allows data to be imported into existing tables.

Schema Exports/Imports

The OWNER parameter of exp has been replaced by the SCHEMAS parameter which is used to
specify the schemas to be exported. The following is an example of the schema export and
import syntax:
expdp scott/tiger@db10g schemas=SCOTT directory=TEST_DIR
dumpfile=SCOTT.dmp logfile=expdpSCOTT.log

impdp scott/tiger@db10g schemas=SCOTT directory=TEST_DIR


dumpfile=SCOTT.dmp logfile=impdpSCOTT.log

10) Upgrading to Oracle Database 10g


This article provides a brief overview of the areas involved in upgrading existing databases to
Oracle 10g including:

• Supported Upgrade Paths


• Pre-Upgrade Validation Checks
• Database Upgrade Assistant (DBCA)
• STARTUP UPGRADE

The whole migration process is beyond the scope of this article so please refer to the Upgrading
a Database to the New Oracle Database 10g Release document for further information.

Supported Upgrade Paths


Direct upgrades to 10g are possible from existing databases with versions listed in the table
below. Upgrades from other versions are supported only via intermediate upgrades to a
supported upgrade version.

Original Version Upgrade Script


8.0.6 u0800060.sql
8.1.7 u0801070.sql
9.0.1 u0900010.sql
9.2.0 u0902000.sql

The preferred upgrade method is to use the Database Upgrade Assistant (DBUA), a GUI tool that
performs all necessary prerequisite checks and operations before upgrading the specified
instances. The DBUA can be started directly from the Oracle Universal Installer (OUI) or
separately after the software installation is complete.

Alternatively you may which to perform a manual upgrade which involves the following steps:

• Analyze the existing instance using the utlu101i.sql script, explained below.
• Backup the database.
• Start the original database in the new upgrade mode (see below) and proceed with the
upgrade. The majority of the upgrade work is done by running the appropriate upgrade
script for the current database version.
• Recompile invalid objects.
• Troubleshoot any issues or abort the upgrade.

Pre-Upgrade Validation Checks

Oracle 10g includes a script ($ORACLE_HOME/rdbms/admin/utlu101i.sql) which performs pre-


update validation checks on an existing instance. The script checks a number of areas to make
sure the instance is suitable for upgrade including:

• Database version.
• Log file sizes.
• Tablespace sizes.
• Server options.
• Initialization parameters (updated, depercated and obsolete).
• Database components.
• SYSAUX tablespace present.
• Cluster information.

Database Upgrade Assistant (DBCA)

The Database Upgrade Assistant (DBUA) is a GUI tool that guides the user through the whole
upgrade process, including all the steps listed in the manual upgrade process. The assistant is
started using the dbua command in UNIX and Linux environments or from the Start menu (Start
> Programs > Oracle - HOME_NAME > Configuration and Migration Tools > Database Upgrade
Assistant) in Windows environments.

Once the assistant has started it leads the user through the several steps including:
• Selecting the instance to upgrade.
• Analyzing the existing database to make sure it is suitable for upgrade.
• Creating the SYSAUX tablespace which is required for 10g.
• Deciding whether to recompile all invalid objects when the upgrade is complete.
• Selecting a backup option for the database.
• Deciding how the database should be managed (OEM Console or Grid Control) and
defining the appropriate authentication.
• Defining the flash recovery area.
• Performing any necessary network configuration.
• Performing the upgrade process.
• Checking the upgrade results.
• Listing the changes in default behaviour between the old and new versionsof the
database.
• Completing the upgrade procedure.

The DBUA can also be started in silent mode provided all the necessary parameters are
provided.

STARTUP UPGRADE

The is a new startup mode associated with the upgrade procedure in Oracle 10g.
SQL> STARTUP UPGRADE;

12) Scheduler in Oracle Database 10g


Oracle 10g includes a comprehensive scheduler (DBMS_SCHEDULER) to replace and extend the
functionality provided by the DBMS_JOB package. Jobs form the core of the functionality, but there
are several other components available:

• Programs
• Schedules
• Jobs
• Job Classes
• Windows
• Windows Groups
• Enable, Disable and Attributes

Programs

The scheduler allows you to optionally create programs which hold metadata about a task, but no
schedule information. A program may related to a PL/SQL block, a stored procedure or an OS
executable file. Programs are created using the CREATE_PROGRAM procedure:
-- Create the test programs.
BEGIN
-- PL/SQL Block.
DBMS_SCHEDULER.create_program (
program_name => 'test_plsql_block_prog',
program_type => 'PLSQL_BLOCK',
program_action => 'BEGIN DBMS_STATS.gather_schema_stats(''SCOTT'');
END;',
enabled => TRUE,
comments => 'Program to gather SCOTT''s statistics using a
PL/SQL block.');

Services in Oracle Database 10g


In Real Application Cluster (RAC) environments it is sometimes desirable to run applications on a
subset of RAC nodes, or have preferred nodes for specific applications. In Oracle 10g this is
accomplished using services.

• Cluster Configuration
• Service Creation
• Jobs and Services
• Connections and Services

Cluster Configuration

For services to function correctly the GSD daemon must be running on each node in the cluster.
The GSD daemons are started using the gsdctl utility, which is part of the Cluster Ready
Services (CRS) installation, so they must be started from that environment as follows.
# Set environment.
export ORACLE_HOME=/u01/app/oracle/product/10.1.0/crs
export PATH=$ORACLE_HOME/bin:$PATH

# Start GSD daemon.


gsdctl start

SQL*Plus Enhancements in Oracle Database 10g


• Whitespace Support in Windows Path and File Names
• Glogin, Login and Predefined Variables
• APPEND, CREATE and REPLACE extensions to SPOOL and SAVE
• SHOW RECYCLEBIN
• Miscellaneous Enhancements

Whitespace Support in Windows Path and File Names

Support for whitespaces in file names has been added to the START, @, @@, RUN, SPOOL,
SAVE and EDIT commands. Names containing whitespaces must be quoted for them to be
recognised correctly:
SPOOL "My Report.txt"
@"My Report.sql"

Glogin, Login and Predefined Variables


The user profile files, glogin.sql and login.sql are now run after each successful connection in
addition to SQL*Plus startup. This is particularly useful when the login.sql file is used to set the
SQLPROMPT to the current connection details.

Three new predefined variables have been added to SQL*Plus:

• _DATE - Contains the current date or a user defined fixed string.


• _PRIVILEGE - Contains privilege level such as AS SYSDBA, AS SYSOPER or blank.
• _USER - Contains the current username (like SHOW USER).

An example of their use would be:


SET SQLPROMPT "_USER'@'_CONNECT_IDENTIFIER _PRIVILEGE _DATE> "
The values of the variables can be viewed using the DEFINE command with no parameters.

APPEND, CREATE and REPLACE extensions to SPOOL and SAVE

The following extentions have been added to the SPOOL and SAVE commands:

• REPLACE - (Default) This option replaces an existing file or creates it if it is not already
present.
• CREATE - This option creates a new file or produces an error if the file already exists.
• APPEND - This option appends to an existing file, or creates a new file if it's not already
present.
Performance Tuning Enhancements in Oracle Database
10g
Oracle 10g includes many performance tuning enhancements including:

• Automatic Performance Diagnostic and Tuning Features


• Automatic Shared Memory Management
• Wait Model Improvements
• Automatic Optimizer Statistics Collection
• Dynamic Sampling
• CPU Costing
• Optimizer Hints
• Rule Based Optimizer Obsolescence
• Tracing Enhancements
• SAMPLE Clause Enhancements
• Hash Partitioned Global Indexes

Automatic Performance Diagnostic and Tuning Features

Oracle 10g includes several features related to automatic performance diagnostics and tuning
including:

• Automatic Optimizer Statistics Collection - The name says it all.


• Automatic Workload Repository (AWR) - An extended version of the STATSPACK
repository that is the heart of all the new diagnostics and tuning features.
• Automatic Database Diagnostic Monitoring (ADDM) - An automatic diagnostics and
tuning tool which uses the information stored in the AWR.
• Automatic SQL Tuning Advisor - A built in SQL tuning feature.

Most of these features are beyond the scope of this article and as such will be dealt with in
separate aticles.

Automatic Shared Memory Management

Automatic Shared Memory Management puts Oracle in control of allocating memory within the
SGA. The SGA_TARGET parameter sets the amount of memory available to the SGA. This
parameter can be altered dynamically up to a maximum of the SGA_MAX_SIZE parameter value.
Provided the STATISTICS_LEVEL is set to TYPICAL or ALL and the SGA_TARGET is set to a
value other than "0" Oracle will control the memory pools which would otherwise be controlled by
the following parameters:

• DB_CACHE_SIZE (default block size)


• SHARED_POOL_SIZE
• LARGE_POOL_SIZE
• JAVA_POOL_SIZE

If these parameters are set to a non-zero value they represent the minimum size for the pool.
These minimum values may be necessary if you experience application errors when certain pool
sizes drop below a specific threshold.

The following parameters must be set manually and take memory from the quota allocated by the
SGA_TARGET parameter:

• DB_KEEP_CACHE_SIZE
• DB_RECYCLE_CACHE_SIZE
• DB_nK_CACHE_SIZE (non-default block size)
• STREAMS_POOL_SIZE
• LOG_BUFFER

Wait Model Improvements

A number of views have been updated and added to improve the wait model. The updated views
include:

• V$EVENT_NAME
• V$SESSION
• V$SESSION_WAIT

The new views include:

• V$ACTIVE_SESSION_HISTORY
• V$SYSTEM_WAIT_HISTORY
• V$SESS_TIME_MODEL
• V$SYS_TIME_MODEL
• V$SYSTEM_WAIT_CLASS
• V$SESSION_WAIT_CLASS
• V$EVENT_HISTOGRAM
• V$FILE_HISTOGRAM
• V$TEMP_HISTOGRAM

The following are some examples of how these updates can be used.

The V$EVENT_NAME view has had three new columns added (WAIT_CLASS_ID, WAIT_CLASS#
and WAIT_CLASS) which indicate the class of the event. This allows easier aggregation of event
details:
User I/O .109552

9 rows selected.
The V$SESSION view has had several columns added that include blocking session and wait
information. The wait information means it's no longer necessary to join to V$SESSION_WAIT to
get wait information for a session:
-- Display blocked session and their blocking session details.
SELECT sid, serial#, blocking_session_status, blocking_session
FROM v$session
WHERE blocking_session IS NOT NULL;
no rows selected

-- Display the resource or event the session is waiting for.


SELECT sid, serial#, event, (seconds_in_wait/1000000) seconds_in_wait
FROM v$session
ORDER BY sid;

The V$SYSTEM_WAIT_HISTORY view shows historical wait information which allows you to
identify issues after the session has ended.

Automatic Optimizer Statistics Collection

By default Oracle 10g automatically gathers optimizer statistics using a scheduled job called
GATHER_STATS_JOB. By default this job runs within a maintenance windows between 10 P.M. to
6 A.M. week nights and all day on weekends. The job calls the
DBMS_STATS.GATHER_DATABASE_STATS_JOB_PROC internal procedure which gathers
statistics for tables with either empty or stale statistics, similar to the
DBMS_STATS.GATHER_DATABASE_STATS procedure using the GATHER AUTO option. The main
difference is that the internal job prioritizes the work such that tables most urgently requiring
statistics updates are processed first.

In some cases automatically gathering statistics can cause problems. Highly volatile tables and
load tables may have their statistics gathered when there is an unrepresentative number of rows
present. These situations can be avoided by using one of two methods:

• The current statistics can be deleted and locked to prevent DBMS_STATS from gathering
new statistics. If the OPTIMIZER_DYNAMIC_SAMPLING parameter is set to 2 (the
default) or higher the necessary statistics will be gathered as part of the query
optimization stage (See Dynamic Sampling):
• BEGIN
• DBMS_STATS.delete_table_stats('MY_SCHEMA','LOAD_TABLE');
• DBMS_STATS.lock_table_stats('MY_SCHEMA','LOAD_TABLE');
• END;
/

• The statistics can be gathered then locked at a time when the table contains the
appropriate data:
• BEGIN
• DBMS_STATS.gather_table_stats('MY_SCHEMA','LOAD_TABLE');
• DBMS_STATS.lock_table_stats('MY_SCHEMA','LOAD_TABLE');
• END;
/
System statistics and statistics for fixed object, such as dynamic performance tables, are not
gathered automatically.

Dynamic Sampling

Dynamic sampling enables the server to improve performance by:


• Estimate single-table predicate selectivities where available statistics are missing or may
lead to bad estimations.
• Estimate statatistics for tables and indexes with missing statistics.
• Estimate statatistics for tables and indexes with out of date statistics.

Dynamic sampling is controled by the OPTIMIZER_DYNAMIC_SAMPLING parameter which


accepts values from "0" (off) to "10" (agressive sampling) with a default value of "2". At compile-
time Oracle determines if dynamic sampling would improve query performance. If so it issues
recursive statements to estimate the necessary statistics. Dynamic sampling can be beneficial
when:

• The sample time is small compared to the overall query execution time.
• Dynamic sampling results in a better performing query.
• The query may be executed multiple times.

In addition to the OPTIMIZER_DYNAMIC_SAMPLING system parameter the dynamic sampling


level can be set using the DYNAMIC_SAMPLING optimizer hint for specific queries like:
SELECT /*+ dynamic_sampling(emp 10) */
empno, ename, job, sal
FROM emp
WHERE deptno = 30;
The results of dynamic sampling are repeatable provided no rows are inserted, updated or
deleted from the sampled table. The OPTIMIZER_FEATURES_ENABLE parameter will turns off
dynamic sampling if it is set to a version earlier than 9.2.0.

CPU Costing

By default the cost model for the optimizer is now CPU+I/O, with the cost unit as time.

Optimizer Hints

New hints:

• SPREAD_MIN_ANALYSIS - Specifies analysis options for spreadsheets.


• USE_NL_WITH_INDEX - Specifies a nested loops join.
• QB_NAME - Specifies a name for a query block.
• NO_QUERY_TRANSFORMATION - Prevents the optimizer performing query
transformations.
• NO_USE_NL, NO_USE_MERGE, NO_USE_HASH, NO_INDEX_FFS, NO_INDEX_SS and
NO_STAR_TRANSFORMATION - Excludes specific operations from the query plan.
• INDEX_SS, INDEX_SS_ASC, INDEX_SS_DESC - Excludes range scans from the query
plan.

Updated hints:

• Hints that specify table names have been expanded to accept Global Table Hints. This
allows a base table within a view to be specified using the "view-name.table-name"
syntax.
• Hints that specify index names have been expanded to accept Complex Index Hints. This
allows an index to be specified using the "(table-name.column-name)" syntax instead of
the index name.
• Some hints can now optionally accept a query block parameter.

Renamed hints:

• NO_PARALLEL - Formally NOPARALLEL.


• NO_PARALLEL_INDEX - Formally NOPARALLEL_INDEX.
• NO_REWRITE - Formally NOREWRITE.

Deprecated hints:

• AND_EQUAL
• HASH_AJ
• MERGE_AJ
• NL_AJ
• HASH_SJ
• NL_SJ
• EXPAND_GSET_TO_UNION
• ORDERED_PREDICATES
• ROWID
• STAR

Rule Based Optimizer Obsolescence

The Rule Based Optimizer (RBO) is now obsolete in Oracle 10g. The functionality is still present
but no new functionality has been included in it and it is no longer supported by Oracle. It is only
present to provide backwards compatibility during the migration to the query optimizer (Cost
Based Optimizer). The results of this osolescence are:

• The CHOOSE and RULE options for the OPTIMIZER_MODE parameter still exist but are no
longer supported.
• The default value for the OPTIMIZER_MODE parameter is ALL_ROWS.
• The CHOOSE and RULE optimizer hints still exist but are no longer supported.
• Code requiring the RBO must be migrated to use the query optimizer.

Tracing Enhancements

The Oracle Trace functionality has been removed from Oracle 10g. Instead the SQL Trace and
TKPROF functionality should be used.

In multi-tier environments where statements are passed to different sessions by the application
server it can become difficult to trace an individual process from start to finish. To solve this
problem Oracle have introduced End to End Application Tracing which allows a client process to
be identified via the client identifier rather than the typical session id. Each piece of trace
information is linked to the following information:

• Client Identifier - Specifies the "real" end user. Set using the
DBMS_SESSION.SET_IDENTIFIER procedure.
• Service - Specifies a group of related applications. Created using the
DBMS_SERVICE.CREATE_SERVICE procedure.
• Module - Specifies a functional area or feature of an application. Set using the
DBMS_APPLICATION_INFO.SET_MODULE procedure.
• Action - Specifies the current action (INSERT, UPDATE, DELETE etc.) within the current
module. Set using the DBMS_APPLICATION_INFO.SET_ACTION procedure.

End to end tracing can be managed via Enterprise Manager or a set of APIs and views. Here are
some examples of how to enable and disable to various types of tracing:
BEGIN
-- Enable/Disable Client Identifier Trace.

Once the trace files are produced the trcsess command line utility can be used to filter out the
relevant data from multiple files. The utility accepts the following parameters:

• OUTPUT - Specifies the name of the consolidated trace file.


• SESSION - Consolidates the file based on the specified session id (SID.SERIAL#
columns from V$SESSION).
• CLIENT_ID - Consolidates the file based on the specified client identifier
(CLIENT_IDENTIFIER column from V$SESSION).
• SERVICE - Consolidates the file based on the specified service (SERVICE_NAME column
from V$SESSION).
• MODULE - Consolidates the file based on the specified module (MODULE column from
V$SESSION).
• ACTION - Consolidates the file based on the specified action (ACTION column from
V$SESSION).
• TRACE_FILES - A space separated list of trace files to be searched. If omitted all files in
the local directory are searched.

At lease one of the search criteria must be specified. If more than one is specified only trace that
matches all the criteria is consolidated. Examples of trcsess usage are:
# Search all files for this session.
trcsess output=session.trc session=144.2274

# Search the specified files for this client identifier.


trcsess output=client.trc client_id=my_id db10g_ora_198.trc
db10g_ora_206.trc

# Search the specified files for this service, module and action
combination.
trcsess output=client.trc service=my_service module=my_module
action=INSERT db10g_ora_198.trc db10g_ora_206.trc
Once the consolidated trace file is produced it can be processed by the TKPROF utility like any
other SQL Trace file.

By default statistics are gathered at the session level. The DBMS_MONITOR package allows this to
be altered to follow the client identifier, service or combinations of the service, module and action:
BEGIN

The gathered statistics can be displayed using the following views:


• DBA_ENABLED_AGGREGATIONS - Accumulated global statistics.
• V$CLIENT_STATS - Accumulated statistics for the specified client identifier.
• V$SERVICE_STATS - Accumulated statistics for the specified service.
• V$SERV_MOD_ACT_STATS - Accumulated statistics for the specified service, module and
action combination.
• V$SVCMETRIC - Accumulated statistics for elapsed time of database calls and CPU
usage.

SAMPLE Clause Enhancements

The SAMPLE clause allows a query to return a limited sample of data by specifying a percentage
of rows or blocks to scan. This clause can now be present in complex queries:
-- Query 10% or rows.
SELECT e.empno, e.ename, d.dname
FROM emp SAMPLE (10) e
JOIN dept d ON e.deptno = d.deptno;

-- Query 10% of blocks.


SELECT e.empno, e.ename, d.dname
FROM emp SAMPLE BLOCK (10) e
JOIN dept d ON e.deptno = d.deptno;

Hash Partitioned Global Indexes

Support for hash partitioned global indexes has been added in Oracle 10g which can improve
performance when a small number of leaf blocks are experiencing high levels of contention. The
syntax for creating of a hash paritioned global index is:
CREATE INDEX hgidx ON tab (c1,c2,c3) GLOBAL
PARTITION BY HASH (c1,c2)
(PARTITION p1 TABLESPACE tbs_1,
PARTITION p2 TABLESPACE tbs_2,
PARTITION p3 TABLESPACE tbs_3,
PARTITION p4 TABLESPACE tbs_4);

PL/SQL Enhancements in Oracle Database 10g


Oracle 10g includes many PL/SQL enhancements including:

• PL/SQL Native Compilation


• FORALL Support for Non-Consecutive Indexes
• New IEEE Floating-Point Types
• Improved Overloading With Numeric Types
• Nested Table Enhancements
• Compile-Time Warnings
• Quoting Mechanism for String Literals
• Implicit Conversion Between CLOB and NCLOB
• Regular Expressions
• Flashback Query Functions
• UTL_COMPRESS
• UTL_MAIL

PL/SQL Native Compilation

The process of PL/SQL native compilation has been simplified in Oracle 10g. The compiled
shared libraries are now stored in the database and extracted as necessary. This means they
form part of the normal backup and recovery process, require no manual maintenance and are
available in Real Application Cluster (RAC) configurations. Native compliation of the package
specification and body are independant of each other, meaning either one, the other or both can
be natively compiled.

The PLSQL_NATIVE_LIBRARY_DIR parameter is the only one which must be set to use native
compilation. All other parameters have been obsoleted. The associated compiler commands are
stored in the $ORACLE_HOME/plsql/spnc_commands file which should not need to be
modified.

Native compilation is switched on and off using the PLSQL_CODE_TYPE parameter which can be
set at instance and session level using the ALTER SYSTEM and ALTER SESSION commands
respectively. The following is an example of native PL/SQL compilation:
-- Set the PLSQL_NATIVE_LIBRARY_DIR parameter.
CONN / AS SYSDBA
ALTER SYSTEM SET PLSQL_NATIVE_LIBRARY_DIR='/u01/app/oracle/native/'
SCOPE=SPFILE;
SHUTDOWN IMMEDIATE
STARTUP

-- Switch on native compilation and compile a procedure.


CONN scott/tiger
ALTER SESSION SET PLSQL_CODE_TYPE='NATIVE';

CREATE OR REPLACE PROCEDURE test_speed AS


v_number NUMBER;
BEGIN
FOR i IN 1 .. 10000000 LOOP
v_number := i / 1000;
END LOOP;
END;
/

SET TIMING ON
EXEC test_speed;

PL/SQL procedure successfully completed.

Elapsed: 00:00:07.19

-- Switch off native compilation and recompile the procedure.


ALTER SESSION SET PLSQL_CODE_TYPE='INTERPRETED';
ALTER PROCEDURE test_speed COMPILE;
EXEC test_speed;

PL/SQL procedure successfully completed.

Elapsed: 00:00:08.03
-- Clean up.
DROP PROCEDURE test_speed;

FORALL Support for Non-Consecutive Indexes

Oracle 10g introduces support for the FORALL syntax with non-consecutive indexes in collections.
The INDICES OF clause allows the FORALL syntax to be used with sparse collections, while the
VALUE OF clause is used for collections of indexes pointing to other collections. The following are
examples of their usage:
/

New IEEE Floating-Point Types

Oracle 10g introduces the new IEEE floating-point types BINARY_FLOAT and BINARY_DOUBLE.
The types are extremely efficient for heavy floating point computations as the work is passed
directly to the operating system. Literal assignments can be perfomed using the "f" and "d"
suffixes or conversion functions TO_BINARY_FLOAT and TO_BINARY_DOUBLE:
DECLARE
l_binary_float BINARY_FLOAT;
l_binary_double BINARY_DOUBLE;
BEGIN
l_binary_float := 1.1f;
l_binary_double := 1.00001d;

l_binary_float := TO_BINARY_FLOAT(1.1);
l_binary_double := TO_BINARY_DOUBLE(1.00001);
END;
/
Rather than raise exceptions, the resulting values of computations may equate to the following
constants that can be tested for:

• [BINARY_FLOAT|BINARY_DOUBLE]_NAN
• [BINARY_FLOAT|BINARY_DOUBLE]_INFINITY
• [BINARY_FLOAT|BINARY_DOUBLE]_MAX_NORMAL
• [BINARY_FLOAT|BINARY_DOUBLE]_MIN_NORMAL
• [BINARY_FLOAT|BINARY_DOUBLE]_MAX_SUBNORMAL
• [BINARY_FLOAT|BINARY_DOUBLE]_MIN_SUBNORMAL

The constants for NaN and infinity are also available in SQL.

Improved Overloading With Numeric Types

Oracle 10g includes improved overloading of numeric types like:


-- Create package specification.
CREATE OR REPLACE PACKAGE numeric_overload_test AS
PROCEDURE go (p_number NUMBER);
PROCEDURE go (p_number BINARY_FLOAT);
PROCEDURE go (p_number BINARY_DOUBLE);
END;
/

-- Create package body.


CREATE OR REPLACE PACKAGE BODY numeric_overload_test AS
PROCEDURE go (p_number NUMBER) AS
BEGIN
DBMS_OUTPUT.put_line('Using NUMBER');
END;

PROCEDURE go (p_number BINARY_FLOAT) AS


BEGIN
DBMS_OUTPUT.put_line('Using BINARY_FLOAT');
END;

PROCEDURE go (p_number BINARY_DOUBLE) AS


BEGIN
DBMS_OUTPUT.put_line('Using BINARY_DOUBLE');
END;
END;
/

-- Test it.
SET SERVEROUTPUT ON
BEGIN
numeric_overload_test.go(10);
numeric_overload_test.go(10.1f);
numeric_overload_test.go(10.1d);
END;
/
It is important to check that the correct overload is being used at all times. The appropriate suffix
or conversion function will make the engine to pick the correct overload.

Nested Table Enhancements

Nested tables in PL/SQL now support more operations than before. Collections can be assigned
directly to the value of another collection of the same type, or to the result of a set expression:
SET SERVEROUTPUT ON
DECLARE
TYPE t_colors IS TABLE OF VARCHAR2(10);
l_col_1 t_colors := t_colors('Red', 'Green', 'Blue');
l_col_2 t_colors := t_colors('Red', 'Green', 'Yellow');
l_col_3 t_colors;

PROCEDURE display (p_text IN VARCHAR2,


p_col IN t_colors) IS
BEGIN
DBMS_OUTPUT.put_line(CHR(10) || p_text);
FOR i IN p_col.first .. p_col.last LOOP
DBMS_OUTPUT.put_line(p_col(i));
END LOOP;
END;
BEGIN
-- Basic assignment.
l_col_3 := l_col_1;
display('Direct Assignment:', l_col_3);

-- Expression assignments.
l_col_3 := l_col_1 MULTISET UNION l_col_2;
display('MULTISET UNION:', l_col_3);

l_col_3 := l_col_1 MULTISET UNION DISTINCT l_col_2;


display('MULTISET UNION DISTINCT:', l_col_3);

l_col_3 := l_col_1 MULTISET INTERSECT l_col_2;


display('MULTISET INTERSECT:', l_col_3);

l_col_3 := l_col_1 MULTISET INTERSECT DISTINCT l_col_2;


display('MULTISET INTERSECT DISTINCT:', l_col_3);

l_col_3 := l_col_1 MULTISET EXCEPT l_col_2;


display('MULTISET EXCEPT:', l_col_3);

l_col_3 := l_col_1 MULTISET EXCEPT DISTINCT l_col_2;


display('MULTISET EXCEPT DISTINCT:', l_col_3);
END;
/

Compile-Time Warnings

Oracle can now produce compile-time warnings when code is ambiguous or inefficient be setting
the PLSQL_WARNINGS parameter at either instance or session level. The categories ALL,
SEVERE, INFORMATIONAL and PERFORMANCE can be used to alter the type of warnings that
are produced. Examples of their usage include:
-- Instance and session level.
ALTER SYSTEM SET PLSQL_WARNINGS='ENABLE:ALL';
ALTER SESSION SET PLSQL_WARNINGS='DISABLE:PERFORMANCE';

Quoting Mechanism for String Literals

Oracle 10g allows you to define your own string delimiters to remove the need to double up any
single quotes. Any character that is not present in the string can be used as the delimeter:
SET SERVEROUTPUT ON
BEGIN
-- Orginal syntax.
DBMS_OUTPUT.put_line('This is Tim''s string!');

-- New syntax.
DBMS_OUTPUT.put_line(q'#This is Tim's string!#');
DBMS_OUTPUT.put_line(q'[This is Tim's string!]');
END;
/

This is Tim's string!


This is Tim's string!
This is Tim's string!

PL/SQL procedure successfully completed.

Implicit Conversion Between CLOB and NCLOB


Oracle 10g now supports implicit conversions between CLOBs and NCLOBs and vice-versa. As
with all type conversions it is still better to be explicit and use the conversion functions TO_CLOB
and TO_NCLOB for clarity.

Regular Expressions

Oracle 10g supports regular expressions in SQL and PL/SQL with the following functions:

• REGEXP_INSTR - Similar to INSTR except it uses a regular expression rather than a


literal as the search string.
• REGEXP_LIKE - Similar to LIKE except it uses a regular expression as the search string.
• REGEXP_REPLACE - Similar to REPLACE except it uses a regular expression as the
search string.
• REGEXP_SUBSTR - Returns the string matching the regular expression. Not really similar
to SUBSTR.

/
Building regular expressions to match your requirements can get a little confusing and this is
beyond the scope of this article.

Flashback Query Functions

The TIMESTAMP_TO_SCN and SCN_TO_TIMESTAMP functions have been added to SQL and
PL/SQL to simplify flashback operations:
SELECT *
FROM emp AS OF SCN TIMESTAMP_TO_SCN(SYSTIMESTAMP - 1/24);

SELECT *
FROM emp AS OF TIMESTAMP SCN_TO_TIMESTAMP(993240);

DECLARE
l_scn NUMBER;
l_timestamp TIMESTAMP;
BEGIN
l_scn := TIMESTAMP_TO_SCN(SYSTIMESTAMP - 1/24);
l_timestamp := SCN_TO_TIMESTAMP(l_scn);
END;
/

UTL_COMPRESS

The UTL_COMPRESS package provides an API to allow compression and decompression of


binary data (RAW, BLOB and BFILE). It uses the Lempel-Ziv compression algorithm which is
equivalent to functionality of the gzip utility. A simple example of it's usage would be:
/

UTL_MAIL

The UTL_MAIL package provides a simple API to allow email to be sent from PL/SQL. In prior
versions this was possible using the UTL_SMTP package, but this required knowledge of the
SMTP protocol.

The package is loaded by running the following scripts:


CONN sys/password AS SYSDBA
@$ORACLE_HOME/rdbms/admin/utlmail.sql
@$ORACLE_HOME/rdbms/admin/prvtmail.plb
In addition the SMTP_OUT_SERVER parameter must be set to identify the SMTP server:
CONN sys/password AS SYSDBA
ALTER SYSTEM SET smtp_out_server='smtp.domain.com' SCOPE=SPFILE;
SHUTDOWN IMMEDIATE
STARTUP
With the configuration complete we can now send a mail using:
BEGIN
UTL_MAIL.send(sender => 'me@domain.com',
recipients => 'person1@domain.com,person2@domain.com',
cc => 'person3@domain.com',
bcc => 'myboss@domain.com',
subject => 'UTL_MAIL Test',
message => 'If you get this message it worked!');
END;
/
The package also supports sending mails with RAW and VARCHAR2 attachments.

Potrebbero piacerti anche