Thursday, April 20, 2006

get database parameter

You do not need to have DBA priviledge to view all the database parameters

A simple script will do

set serveroutput on size 1000000

declare
x number;
y long;
d number;
begin
if ( dbms_utility.get_parameter_value( '&1', x, y ) = 1 )
then
dbms_output.put_line( y );
else
dbms_output.put_line( x );
end if;
end;
/

Unfortunately, the following parameters are not supported

shared_pool_size
sga_max_size
shared_pool_reserved_size
large_pool_size
java_pool_size
db_keep_cache_size
db_recycle_cache_size
db_2k_cache_size
db_4k_cache_size
db_8k_cache_size
db_16k_cache_size
db_32k_cache_size
db_cache_size
pga_aggregate_target

Thursday, April 13, 2006

Join Methods

Abstracted from Oracle Online Document
http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96533/optimops.htm#56299

1. Nested Loop

The optimizer uses nested loop joins when joining small number of rows, with a good driving condition between the two tables. You drive from the outer loop to the inner loop, so the order of tables in the execution plan is important.
The outer loop is the driving row source. It produces a set of rows for driving the join condition. The row source can be a table accessed using an index scan or a full table scan. Also, the rows can be produced from any other operation. For example, the output from a nested loop join can be used as a row source for another nested loop join.
The inner loop is iterated for every row returned from the outer loop, ideally by an index scan. If the access path for the inner loop is not dependent on the outer loop, then you can end up with a Cartesian product; for every iteration of the outer loop, the inner loop produces the same set of rows. Therefore, you should use other join methods when two independent row sources are joined together.

2. Hash Join

The optimizer uses a hash join to join two tables if they are joined using an equijoin and if either of the following conditions are true:
A large amount of data needs to be joined.
A large fraction of the table needs to be joined.

(Note: The smaller one will be used to build the in-memory hash)

3. Sort Merge

The optimizer can choose a sort merge join over a hash join for joining large amounts of data if any of the following conditions are true:
The join condition between two tables is not an equi-join.
OPTIMIZER_MODE is set to RULE.
HASH_JOIN_ENABLED is false.
Because of sorts already required by other operations, the optimizer finds it is cheaper to use a sort merge than a hash join.
The optimizer thinks that the cost of a hash join is higher, based on the settings of HASH_AREA_SIZE and SORT_AREA_SIZE.

Note:
Oracle does not recommend using the HASH_AREA_SIZE and SORT_AREA_SIZE parameters unless the instance is configured with the shared server option. Oracle recommends instead that you enable automatic sizing of SQL work areas by setting PGA_AGGREGATE_TARGET. HASH_AREA_SIZE and SORT_AREA_SIZE are retained only for backward compatibility.

Monday, April 03, 2006

Reverse a multi-byte string

reverse() reverses a single byte string but it does not work well with multi-byte characters

SQL> select reverse('abc') from dual;
REVERS
------
cba

SQL> exec :mystr := '国中cba';
1 SELECT max(replace(SYS_CONNECT_BY_PATH (c, '/'), '/')) keep (dense_rank last order by level)
2 FROM (SELECT LEVEL lvl, SUBSTR (:mystr, LEVEL, 1) c
3 FROM (SELECT LENGTH (:mystr) len
4 FROM DUAL) x
5 CONNECT BY LEVEL <= x.len
6 ORDER BY 1 DESC)
7* CONNECT BY PRIOR lvl = lvl + 1

MAX(REPLACE(SYS_CONNECT_BY_PATH(C,'/'),'/'))KEEP(DENSE_RANKLASTORDERBYLEVEL)
----------------------------------------------------------------------------
abc中国