Thursday, March 6, 2008

Native hash full outer join in Oracle 11g

One of the new features of Oracle 11g that does not have that much of marketing attention is the new support for native full outer joins if they can be handled by a HASH join operation, whichs means that it needs to be an equi-join. Of course you need to use the ANSI join syntax (FULL OUTER JOIN) rather than the traditional Oracle syntax for outer joins (+) in order to benefit from the new feature

In comparison to the method used so far for full outer joins the new native support should require less resource consumption as it is able to perform the full outer join in one single operation whereas the former implementation required a rewrite of the full outer join to a simple outer join (left or right) and an additional "not exists" operation, e.g. handled by a further hash-join anti which would require another access to the data, e.g. two table scans instead of a single one.

This new functionality seems to be controlled by the internal parameter "_optimizer_native_full_outer_join". It currently supports three different settings: "choose", "force" and "off". The default is "force", and setting it to any other value for me disabled the new feature, so in my tests there was no difference between "choose" and "off", but of course there might be cases where "choose" actually attempts to use the new feature.

You can modify this parameter e.g. using ALTER SESSION or the OPT_PARAM hint that has been introduced with Oracle 10g that allows to manipulate parameter values only for a specific statement execution rather than for the whole session.

Here is a small sample script that demonstrates the new feature in action.

First it creates two tables using the ALL_OBJECTS view. In a 11g default installation these tables hold approx. 53.000 records. The two tables are generated using a certain offset for the join column so that they overlap only partially. Hence a full outer join on that column is supposed to return the overlapped part and each part from each table that doesn't overlap.


SQL>
SQL> drop table native_full_outer_join_test purge;

Table dropped.

Elapsed: 00:00:00.02
SQL> drop table native_full_outer_join_test2 purge;

Table dropped.

Elapsed: 00:00:00.02
SQL>
SQL> create table native_full_outer_join_test as
2 select
3 trunc(sqrt(rownum-1)) as skewed_data,
4 rownum-1 as id,
5 lpad(rownum-1,10) id_char,
6 rpad('x',50, 'x') as filler
7 from
8 all_objects;

Table created.

Elapsed: 00:00:26.25
SQL>
SQL> create table native_full_outer_join_test2 as
2 select
3 trunc(sqrt(rownum+20000-1)) as skewed_data,
4 rownum+20000-1 as id,
5 lpad(rownum+20000-1,10) id_char,
6 rpad('x',50, 'x') as filler
7 from
8 all_objects;

Table created.

Elapsed: 00:00:37.01
SQL>
SQL> begin
2 dbms_stats.gather_table_stats(ownname=>USER, tabname=>'native_full_outer_join_test');
3 dbms_stats.gather_table_stats(ownname=>USER, tabname=>'native_full_outer_join_test2');
4 end;
5 /

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.70

Now we perform the full outer join using the default setting of force for the corresponding parameter.

SQL>
SQL> set autotrace traceonly
SQL>
SQL> select /*+ opt_param('_optimizer_native_full_outer_join', 'force') */ a.*, b.* from native_full_outer_join_test a
2 full outer join native_full_outer_join_test2 b
3 on a.id = b.id
4 order by a.id_char;

73762 rows selected.

Elapsed: 00:00:01.16

Execution Plan
----------------------------------------------------------
Plan hash value: 4125684409

---------------------------------------------------------------------------------------------------------------
|ִIdִִ|ִOperationִִִִִִִִִִִִִִ|ִNameִִִִִִִִִִִִִִִִִִִִִִִִִ|ִRowsִִ|ִBytesִ|TempSpc|ִCostִ(%CPU)|ִTimeִִִִִ|
---------------------------------------------------------------------------------------------------------------
|ִִִ0ִ|ִSELECTִSTATEMENTִִִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִ53762ִ|ִִ6667K|ִִִִִִִ|ִִ2259ִִִ(1)|ִ00:00:28ִ|
|ִִִ1ִ|ִִSORTִORDERִBYִִִִִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִ53762ִ|ִִ6667K|ִִִִ15M|ִִ2259ִִִ(1)|ִ00:00:28ִ|
|ִִִ2ִ|ִִִVIEWִִִִִִִִִִִִִִִִִ|ִVW_FOJ_0ִִִִִִִִִִִִִִִִִִִִִ|ִ53762ִ|ִִ6667K|ִִִִִִִ|ִִִ731ִִִ(1)|ִ00:00:09ִ|
|*ִִ3ִ|ִִִִHASHִJOINִFULLִOUTER|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִ53762ִ|ִִ7350K|ִִ4312K|ִִִ731ִִִ(1)|ִ00:00:09ִ|
|ִִִ4ִ|ִִִִִTABLEִACCESSִFULLִִ|ִNATIVE_FULL_OUTER_JOIN_TESTִִ|ִ53761ִ|ִִ3675K|ִִִִִִִ|ִִִ156ִִִ(1)|ִ00:00:02ִ|
|ִִִ5ִ|ִִִִִTABLEִACCESSִFULLִִ|ִNATIVE_FULL_OUTER_JOIN_TEST2ִ|ִ53762ִ|ִִ3675K|ִִִִִִִ|ִִִ156ִִִ(1)|ִ00:00:02ִ|
---------------------------------------------------------------------------------------------------------------

PredicateִInformationִ(identifiedִbyִoperationִid):
---------------------------------------------------

ִִִ3ִ-ִaccess("A"."ID"="B"."ID")


Statistics
----------------------------------------------------------
ִִִִִִִִִִ1ִִrecursiveִcalls
ִִִִִִִִִִ0ִִdbִblockִgets
ִִִִִִִ1136ִִconsistentִgets
ִִִִִִִִִִ0ִִphysicalִreads
ִִִִִִִִִִ0ִִredoִsize
ִִִִ2704743ִִbytesִsentִviaִSQL*Netִtoִclient
ִִִִִִ54503ִִbytesִreceivedִviaִSQL*Netִfromִclient
ִִִִִִִ4919ִִSQL*Netִroundtripsִto/fromִclient
ִִִִִִִִִִ1ִִsortsִ(memory)
ִִִִִִִִִִ0ִִsortsִ(disk)
ִִִִִִ73762ִִrowsִprocessed

You can see that the database was able to perform the join operation in one single HASH JOIN FULL OUTER step. It is interesting to note that the cardinality estimate in this particular case does not consider the non-overlapping parts and just returns the cardinality of a complete overlapping set.

Now we revert to the pre-11g implementation by disabling the new feature.

SQL>
SQL> select /*+ opt_param('_optimizer_native_full_outer_join', 'off') */ a.*, b.* from native_full_outer_join_test a
2 full outer join native_full_outer_join_test2 b
3 on a.id = b.id
4 order by a.id_char;

73762 rows selected.

Elapsed: 00:00:01.26

Execution Plan
----------------------------------------------------------
Plan hash value: 4036012045

----------------------------------------------------------------------------------------------------------------
|ִIdִִ|ִOperationִִִִִִִִִִִִִִִ|ִNameִִִִִִִִִִִִִִִִִִִִִִִִִ|ִRowsִִ|ִBytesִ|TempSpc|ִCostִ(%CPU)|ִTimeִִִִִ|
----------------------------------------------------------------------------------------------------------------
|ִִִ0ִ|ִSELECTִSTATEMENTִִִִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִ87522ִ|ִִִִ10M|ִִִִִִִ|ִִ3529ִִִ(1)|ִ00:00:43ִ|
|ִִִ1ִ|ִִSORTִORDERִBYִִִִִִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִ87522ִ|ִִִִ10M|ִִִִ25M|ִִ3529ִִִ(1)|ִ00:00:43ִ|
|ִִִ2ִ|ִִִVIEWִִִִִִִִִִִִִִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִ87522ִ|ִִִִ10M|ִִִִִִִ|ִִ1043ִִִ(1)|ִ00:00:13ִ|
|ִִִ3ִ|ִִִִUNION-ALLִִִִִִִִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִִִִִִִ|ִִִִִִִ|ִִִִִִִ|ִִִִִִִִִִִִ|ִִִִִִִִִִ|
|*ִִ4ִ|ִִִִִHASHִJOINִOUTERִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִ53761ִ|ִִ7350K|ִִ4312K|ִִִ731ִִִ(1)|ִ00:00:09ִ|
|ִִִ5ִ|ִִִִִִTABLEִACCESSִFULLִִ|ִNATIVE_FULL_OUTER_JOIN_TESTִִ|ִ53761ִ|ִִ3675K|ִִִִִִִ|ִִִ156ִִִ(1)|ִ00:00:02ִ|
|ִִִ6ִ|ִִִִִִTABLEִACCESSִFULLִִ|ִNATIVE_FULL_OUTER_JOIN_TEST2ִ|ִ53762ִ|ִִ3675K|ִִִִִִִ|ִִִ156ִִִ(1)|ִ00:00:02ִ|
|*ִִ7ִ|ִִִִִHASHִJOINִRIGHTִANTI|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִ33761ִ|ִִ2472K|ִִִִִִִ|ִִִ312ִִִ(1)|ִ00:00:04ִ|
|ִִִ8ִ|ִִִִִִTABLEִACCESSִFULLִִ|ִNATIVE_FULL_OUTER_JOIN_TESTִִ|ִ53761ִ|ִִִ262K|ִִִִִִִ|ִִִ156ִִִ(1)|ִ00:00:02ִ|
|ִִִ9ִ|ִִִִִִTABLEִACCESSִFULLִִ|ִNATIVE_FULL_OUTER_JOIN_TEST2ִ|ִ53762ִ|ִִ3675K|ִִִִִִִ|ִִִ156ִִִ(1)|ִ00:00:02ִ|
----------------------------------------------------------------------------------------------------------------

PredicateִInformationִ(identifiedִbyִoperationִid):
---------------------------------------------------

ִִִ4ִ-ִaccess("A"."ID"="B"."ID"(+))
ִִִ7ִ-ִaccess("A"."ID"="B"."ID")


Statistics
----------------------------------------------------------
ִִִִִִִִִִ1ִִrecursiveִcalls
ִִִִִִִִִִ0ִִdbִblockִgets
ִִִִִִִ2272ִִconsistentִgets
ִִִִִִִִִִ0ִִphysicalִreads
ִִִִִִִִִִ0ִִredoִsize
ִִִִ2705047ִִbytesִsentִviaִSQL*Netִtoִclient
ִִִִִִ54503ִִbytesִreceivedִviaִSQL*Netִfromִclient
ִִִִִִִ4919ִִSQL*Netִroundtripsִto/fromִclient
ִִִִִִִִִִ1ִִsortsִ(memory)
ִִִִִִִִִִ0ִִsortsִ(disk)
ִִִִִִ73762ִִrowsִprocessed

It is obvious that in this particular case the database had to do more work. It required two operations, an outer join and a second anti join to perform the full outer join operation. It can also be seen that the "consistent gets" are doubled due to that second operation. On the other hand, the cardinality estimate looks closer to reality, as it considers at least partially the non-overlapping part represented by the anti join operation.

For the sake of completeness the following query shows that the new feature is only available for equi-joins, since this is the only operation supported by a hash join.

SQL>
SQL> set autotrace traceonly explain
SQL>
SQL> select a.*, b.* from native_full_outer_join_test a
2 full outer join native_full_outer_join_test2 b
3 on a.id < b.id
4 order by a.id_char;


Elapsed: 00:00:00.01

Execution Plan
----------------------------------------------------------
Plan hash value: 4239385412

---------------------------------------------------------------------------------------------------------------
|ִIdִִ|ִOperationִִִִִִִִִִִִִִ|ִNameִִִִִִִִִִִִִִִִִִִִִִִִִ|ִRowsִִ|ִBytesִ|TempSpc|ִCostִ(%CPU)|ִTimeִִִִִ|
---------------------------------------------------------------------------------------------------------------
|ִִִ0ִ|ִSELECTִSTATEMENTִִִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִִִ144M|ִִִִ17G|ִִִִִִִ|ִִ4107Kִִ(1)|ִ13:41:36ִ|
|ִִִ1ִ|ִִSORTִORDERִBYִִִִִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִִִ144M|ִִִִ17G|ִִִִ41G|ִִ4107Kִִ(1)|ִ13:41:36ִ|
|ִִִ2ִ|ִִִVIEWִִִִִִִִִִִִִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִִִ144M|ִִִִ17G|ִִִִִִִ|ִִ4107ִִ(17)|ִ00:00:50ִ|
|ִִִ3ִ|ִִִִUNION-ALLִִִִִִִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִִִִִִִ|ִִִִִִִ|ִִִִִִִ|ִִִִִִִִִִִִ|ִִִִִִִִִִ|
|ִִִ4ִ|ִִִִִMERGEִJOINִOUTERִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִִִ144M|ִִִִ18G|ִִִִִִִ|ִִ2740ִִ(25)|ִ00:00:33ִ|
|ִִִ5ִ|ִִִִִִSORTִJOINִִִִִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִ53761ִ|ִִ3675K|ִִ8888K|ִִ1044ִִִ(1)|ִ00:00:13ִ|
|ִִִ6ִ|ִִִִִִִTABLEִACCESSִFULL|ִNATIVE_FULL_OUTER_JOIN_TESTִִ|ִ53761ִ|ִִ3675K|ִִִִִִִ|ִִִ156ִִִ(1)|ִ00:00:02ִ|
|*ִִ7ִ|ִִִִִִSORTִJOINִִִִִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִ53762ִ|ִִ3675K|ִִ8888K|ִִ1044ִִִ(1)|ִ00:00:13ִ|
|ִִִ8ִ|ִִִִִִִTABLEִACCESSִFULL|ִNATIVE_FULL_OUTER_JOIN_TEST2ִ|ִ53762ִ|ִִ3675K|ִִִִִִִ|ִִִ156ִִִ(1)|ִ00:00:02ִ|
|ִִִ9ִ|ִִִִִMERGEִJOINִANTIִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִ51074ִ|ִִ3740K|ִִִִִִִ|ִִ1366ִִִ(1)|ִ00:00:17ִ|
|ִִ10ִ|ִִִִִִSORTִJOINִִִִִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִ53762ִ|ִִ3675K|ִִ8888K|ִִ1044ִִִ(1)|ִ00:00:13ִ|
|ִִ11ִ|ִִִִִִִTABLEִACCESSִFULL|ִNATIVE_FULL_OUTER_JOIN_TEST2ִ|ִ53762ִ|ִִ3675K|ִִִִִִִ|ִִִ156ִִִ(1)|ִ00:00:02ִ|
|*ִ12ִ|ִִִִִִSORTִUNIQUEִִִִִִִ|ִִִִִִִִִִִִִִִִִִִִִִִִִִִִִִ|ִ53761ִ|ִִִ262K|ִִ1288K|ִִִ322ִִִ(1)|ִ00:00:04ִ|
|ִִ13ִ|ִִִִִִִTABLEִACCESSִFULL|ִNATIVE_FULL_OUTER_JOIN_TESTִִ|ִ53761ִ|ִִִ262K|ִִִִִִִ|ִִִ156ִִִ(1)|ִ00:00:02ִ|
---------------------------------------------------------------------------------------------------------------

PredicateִInformationִ(identifiedִbyִoperationִid):
---------------------------------------------------

ִִִ7ִ-ִaccess("A"."ID"<"B"."ID"(+))
ִִִִִִִfilter("A"."ID"<"B"."ID"(+))
ִִ12ִ-ִaccess(INTERNAL_FUNCTION("A"."ID")
<INTERNAL_FUNCTION("B"."ID"))
ִִִִִִִfilter(INTERNAL_FUNCTION("A"."ID")<INTERNAL_FUNCTION("B"."ID"))

SQL>
SQL> spool off

Friday, February 15, 2008

Nasty bug introduced with patch set 10.2.0.3 regarding SORT_AREA_SIZE

While testing an database application that is being migrated from 9.2 to 10.2 we came across a quite nasty bug that was introduced by a "bug fix" which was meant to speed up the "ALTER SESSION" command when propagating parameter changes to the parallel slaves. Unfortunately the "fix" introduced another severe issue regarding the handling of the SORT_AREA_SIZE parameter.

Metalink has documented the issue in the document 452466.1 and the corresponding bug 6113096.

Our own tests have shown that the bug description unfortunately is incomplete resp. inaccurate. It is not only that the propagation of the via ALTER SESSION modified SORT_AREA_SIZE setting to the parallel slaves is not working, but the modified SORT_AREA_SIZE setting of the session is not used at all, even in serial execution.

So in case you have a 9i application that is mainly batch driven and therefore has control over the concurrent usage of the database, you can speed up large sorts and joins by using the WORKAREA_SIZE_POLICY setting of MANUAL and setting SORT_AREA_SIZE and HASH_AREA_SIZE to suitable large values. Indications of a good setting can be obtained e.g. by looking at V$SQL_WORKAREA or V$SQL_WORKAREA_ACTIVE. Since in 9i the upper limit of the automatic tuning for the sort area size using PGA_AGGREGATE_TARGET is fixed to 100M (if you do not start to fiddle around with undocumented parameters, e.g. _pga_max_size or _smm_max_size), you can sometimes achieve significant benefits when using the MANUAL setting for large sorts and joins. Note that starting with 10gR2 a more aggressive approach is used when performing automatic tuning and the upper limit of 100M does not apply anymore (when using a PGA_AGGREGATE_TARGET > 1000M, for a detailed description see e.g. Joze Senegacnik's PGA memory management presentation) . So in 10gR2 and higher the automatic tuning can also be used for mainly batch driven processes, but still there are cases where the MANUAL setting performs better if you know your data well and do not overallocate memory.

Now if you perform in a 10.2.0.3 session an ALTER SESSION SET SORT_AREA_SIZE = x, this does not take any effect and the actual value used will be the value of the instance setting, either from pfile or spfile, regardless of the individual value set in the session. This applies also to serial execution, not only parallel executions as described in the Metalink documents.

This affects all sort-related operations, for instance, creation or rebuild of an index, ORDER BY sorts, unions, intersects, etc.

You can confirm this by monitoring a sort operation e.g. using V$SQL_WORKAREA_ACTIVE. In MANUAL mode the WORK_AREA_SIZE column shows always 0 in >= 10.2.0.3 (in 9i the actual SORT_AREA_SIZE setting was shown) and the MAX_MEMORY_USED column will correspond to the instance setting of SORT_AREA_SIZE in case a sufficient large sort operation takes place. SORT_AREA_SIZE has a quite low default setting of 64k in 10gR2 if not stated otherwise in the instance configuration. You'll probably also detect quite a lot usage of the temporary tablespace since most sort operations won't fit into memory when using the default setting and therefore will spill to disk.

It is even does not work to use an ALTER SYSTEM SET SORT_AREA_SIZE = x DEFERRED which is possible since 10g. Although the V$PARAMETER view shows the modified value set for SORT_AREA_SIZE when establishing a new session (similar as it does when using ALTER SESSION) the actual value used is still the one from the instance wide setting.

The only known workaround so far is to set the instance setting of SORT_AREA_SIZE to a sufficient high value, although this is in my opinion not a really neat solution as you have to live with this "one size fits all" approach. So in case you sometimes want to allocate less memory since multiple processes will run at the same time and sometimes really use a huge allocation for a single process this is not possible in 10.2.0.3 and later. You have to use either the automatic tuning or live with the restriction of a single possible value used as instance setting.

UPDATE: As shown e.g. by Jonathan Lewis here there seems to be a kind of workaround at least when executing in serial mode, by simply issuing the ALTER SESSION twice with the value for SORT_AREA_SIZE requested. This doesn't seem to apply to the propagation of the value to parallel slaves, though.

Further tests using release 11.1.0.6.0 on Windows have shown that - as described in the document - the bug is still not fixed in 11g and according to the documentation it won't be fixed in the 1o.2.0.4 patch set either. A one-off patch does not seem to be available either. You can only use the workarounds offered so far.

So if you do something like that using MANUAL workarea_size_policy:

alter index cbo_test.test_mem rebuild nologging;

you will be affected by the bug, or more precisely the sort operation required to rebuild the index will be affected. It doesn't matter if the operation is executed in serial or parallel mode.

If you do something like that using MANUAL workarea_size_policy:

insert /*+ append noparallel(x) */ into sh.sales x
select /*+ noparallel(a) noparallel(b) */ a.* from sh.sales a, sh.sales b
where a.prod_id = b.prod_id
and a.cust_id = b.cust_id
and a.time_id = b.time_id
and a.channel_id = b.channel_id
and a.promo_id = b.promo_id;

you can also be affected, depending on if indexes do exist and the kind of join operation. In case the table being populated has indexes that need to be maintained the corresponding sort operation will be affected. The join operation itself will be able to use the MANUAL settings when using a HASH JOIN as the HASH_AREA_SIZE parameter seems not to be affected by the bug (the WORK_AREA_SIZE column shows the expected value of HASH_AREA_SIZE in MANUAL mode), whereas a SORT MERGE JOIN will be affected since it in most cases requires a SORT operation that again will be affected. Again it does not matter if serial or parallel execution is used, the serial execution will be affected as well.

So the following operation represents basically a worst case as it is affected by the bug in several ways:

insert /*+ append noparallel(x) */ into sh.sales x
select /*+ noparallel(a) noparallel(b) use_merge(a, b) */ a.* from sh.sales a, sh.sales b
where a.prod_id = b.prod_id
and a.cust_id = b.cust_id
and a.time_id = b.time_id
and a.channel_id = b.channel_id
and a.promo_id = b.promo_id
order by a.prod_id, a.cust_id, a.time_id, a.channel_id, a.promo_id;

1. It needs to maintain indexes on the SALES table (provided the demo indexes are in place and usable) by sorting
2. It needs to sort to perform the SORT MERGE JOIN
3. It needs to sort the result set

Of course there might be even worse scenarios, e.g. additional sorts caused by analytic functions etc.

So in summary check thoroughly any application that is supposed to use MANUAL workarea size policy before applying 10.2.0.3 patchset or even using 11g.