Professional Documents
Culture Documents
With the REF_CURSOR you can return a record set/cursor from a stored procedure.
There are 2 basic types: Strong ref cursor and weak ref cursor
For the strong ref cursor the returning columns with datatype and length need to be known at compile time.
For the weak ref cursor the structure does not need to be known at compile time.
The strong ref_cursor and until Oracle 9i also the weak-type need to be declared in a package structure lik this:
create or replace package REFCURSOR_PKG as
TYPE WEAK8i_REF_CURSOR IS REF CURSOR;
TYPE STRONG REF_CURSOR IS REF CURSOR RETURN EMP%ROWTYPE;
end REFCURSOR_PKG;
Since Oracle 9i you can use SYS_REFCURSOR as the type for the returning
REF_CURSOR.
/* Strong type */
1
Connection conn = getConnection();
CallableStatement cstmt = null;
ResultSet rs = null;
int deptno = 10;
Object temp;
try{
cstmt = conn.prepareCall("begin test(?,?); end;");
cstmt.setInt(1, deptno);
cstmt.registerOutParameter(2, OracleTypes.CURSOR);
cstmt.execute();
rs = (ResultSet) cstmt.getObject(2);
ResultSetMetaData rsm = rs.getMetaData();
int columnCount = rsm.getColumnCount();
while (rs.next()){
for (int j=0;j< columnCount;j++){
temp = rs.getObject(j+1);
}
}
} finally {
if (!rs==null){
rs.close();
}
if (!stmt==null){
stmt.close();
}
if (!conn==null){
conn.close();
}
}
}
HOWTO user hints. With hints one can influence the optimizer. The usage of hints (with exception of the RULE-
hint) causes Oracle to use the Cost Based optimizer.
2
Hints for Optimization Approaches and Goals
3
The ALL_ROWS hint explicitly chooses the cost-based approach to optimize a
ALL_ROWS statement block with a goal of best throughput (that is, minimum total resource
consumption).
The FIRST_ROWS hint explicitly chooses the cost-based approach to optimize
a statement block with a goal of best response time (minimum resource usage
FIRST_ROWS to return first row). In newer Oracle version you should give a parameter with
this hint: FIRST_ROWS(n) means that the optimizer will determine an
executionplan to give a fast response for returning the first n rows.
The CHOOSE hint causes the optimizer to choose between the rule-based
CHOOSE approach and the cost-based approach for a SQL statement based on the
presence of statistics for the tables accessed by the statement
The RULE hint explicitly chooses rule-based optimization for a statement block.
RULE This hint also causes the optimizer to ignore any other hints specified for the
statement block. The RULE hint does not work any more in Oracle 10g.
4
INDEX_SS Exclude range scan from query plan (from Oracle 10g)
INDEX_SS_ASC
Exclude range scan from query plan (from Oracle 10g)
INDEX_SS_DESC Exclude range scan from query plan (from Oracle 10g)
The NO_INDEX_SS hint causes the optimizer to exclude a skip scan of the
NO_INDEX_SS
specified indexes on the specified table. (from Oracle 10g)
5
Hints for Join Operations
The USE_NL hint causes Oracle to join each specified table to another row
source with a nested loops join using the specified table as the inner table. The
USE_NL
syntax of the USE_NL hint is USE_NL(table table) where table is the name or
alias of a table to be used as the inner table of a nested loops join.
NO_USE_NL Do not use nested loop (from Oracle 10g)
USE_NL_WITH_INDEX Specifies a nested loops join. (from Oracle 10g)
The USE_MERGE hint causes Oracle to join each specified table with another
row source with a sort-merge join. The syntax of the USE_MERGE hint is
USE_MERGE USE_MERGE(table table) where table is a table to be joined to the row source
resulting from joining the previous tables in the join order using a sort-merge
join.
NO_USE_MERGE Do not use merge (from Oracle 10g)
The USE_HASH hint causes Oracle to join each specified table with another
row source with a hash join. The syntax of the USE_HASH hint is
USE_HASH
USE_HASH(table table) where table is a table to be joined to the row source
resulting from joining the previous tables in the join order using a hash join.
NO_USE_HASH Do not use hash (from Oracle 10g)
Hints for Parallel Execution
The PARALLEL hint allows you to specify the desired number of concurrent
query servers that can be used for the query. The syntax is PARALLEL(table
number number). The PARALLEL hint must use the table alias if an alias is
specified in the query. The PARALLEL hint can then take two values separated
by commas after the table name. The first value specifies the degree of
PARALLEL
parallelism for the given table, the second value specifies how the table is to be
split among the instances of a parallel server. Specifying DEFAULT or no value
signifies the query coordinator should examine the settings of the initialization
parameters (described in a later section) to determine the default degree of
parallelism.
The NOPARALLEL hint allows you to disable parallel scanning of a table, even
NOPARALLEL / NO_PARALLEL if the table was created with a PARALLEL clause. In Oracle 10g this hint was
renamed to NO_PARALLEL.
The PQ_DISTRIBUTE hint improves the performance of parallel join operations.
Do this by specifying how rows of joined tables should be distributed among
PQ_DISTRIBUTE
producer and consumer query servers. Using this hint overrides decisions the
optimizer would normally make.
The NO_PARALLEL_INDEX hint overrides a PARALLEL attribute setting on an
NO_PARALLEL_INDEX
index to avoid a parallel index scan operation.
Additional Hints
When the APPEND hint is used with the INSERT statement, data is appended
to the table. Existing free space in the block is not used. If a table or an index is
APPEND
specified with nologging, this hint applied with an insert statement produces a
direct path insert which reduces generation of redo.
NOAPPEND Overrides the append mode.
The CACHE hint specifies that the blocks retrieved for the table in the hint are
placed at the most recently used end of the LRU list in the buffer cache when a
CACHE full table scan is performed. This option is useful for small lookup tables. In the
following example, the CACHE hint overrides the table default caching
specification.
The NOCACHE hint specifies that the blocks retrieved for this table are placed
at the least recently used end of the LRU list in the buffer cache when a full
NOCACHE
table scan is performed. This is the normal behavior of blocks in the buffer
cache.
PUSH_PRED The PUSH_PRED hint forces pushing of a join predicate into the view.
NO_PUSH_PRED The NO_PUSH_PRED hint prevents pushing of a join predicate into the view.
6
The PUSH_SUBQ hint causes nonmerged subqueries to be evaluated at the
PUSH_SUBQ
earliest possible place in the execution plan.
The NO_PUSH_SUBQ hint causes non-merged subqueries to be evaluated as
NO_PUSH_SUBQ
the last step in the execution plan.
QB_NAME Specifies a name for a query block. (from Oracle 10g)
Oracle can replace literals in SQL statements with bind variables, if it is safe to
do so. This is controlled with the CURSOR_SHARING startup parameter. The
CURSOR_SHARING_EXACT CURSOR_SHARING_EXACT hint causes this behavior to be switched off. In
other words, Oracle executes the SQL statement without any attempt to replace
literals by bind variables.
The DRIVING_SITE hint forces query execution to be done for the table at a
DRIVING_SITE
different site than that selected by Oracle
The DYNAMIC_SAMPLING hint lets you control dynamic sampling to improve
server performance by determining more accurate predicate selectivity and
statistics for tables and indexes. You can set the value of
DYNAMIC_SAMPLING
DYNAMIC_SAMPLING to a value from 0 to 10. The higher the level, the more
effort the compiler puts into dynamic sampling and the more broadly it is
applied. Sampling defaults to cursor level unless you specify a table.
This hint omits some of the compile time optimizations of the rules, mainly
detailed dependency graph analysis, on spreadsheets. Some optimizations
SPREAD_MIN_ANALYSIS
such as creating filters to selectively populate spreadsheet access structures
and limited rule pruning are still used. (from Oracle 10g)
Hints with unknown status
7
Oracle autonomous transaction
An autonomous transaction is an independent transaction that is initiated by another transaction (the parent
transaction). An autonomous transaction can modify data and commit or rollback independent of the state of the
parent transaction.
The autonomous transaction must commit or roll back before the autonomous transaction is ended and the parent
transaction continues.
An autonomous transaction is defined in the declaration of a pl/sql block. This can be an anonymous block,
function, procedure, object method or trigger.
This is done by adding the statement 'PRAGMA AUTONOMOUS_TRANSACTION;' anywhere in the declaration
block.
There isn't much involved in defining a PL/SQL block as an autonomous transaction. You simply include the
following statement in your declaration section:
PRAGMA AUTONOMOUS_TRANSACTION;
Sample code:
PROCEDURE test_autonomous
IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
insert ....
commit;
END test_autonomous;
Autonomous transactions can be used for logging in the database independent of the rollback/commit of the parent
transaction.