In contrast to what the title seems suggesting, the OR-Expansion is not a new 12cR2 transformation. It has existed before and has been nicely explained in this article by the Oracle optimizer group. However, let me invite you to observe with me the following simple demonstration:
create table t1 (n1 number, n2 number, c1 varchar2(10));
create index t1_idx1 on t1(n1, n2);
create index t1_idx2 on t1(n2);
explain plan for
select *
from t1
where (n1 =1 or n2 = 42);
select * from table(dbms_xplan.display);
The above query has two different execution plans in 12cR1 and 12cR2 as shown below respectively:
-- 12cR1 : 12.1.0.2.0
---------------------------------------------------------------
| Id | Operation | Name | Rows |
----------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 |
| 1 | CONCATENATION | | |
| 2 | TABLE ACCESS BY INDEX ROWID BATCHED| T1 | 1 |
|* 3 | INDEX RANGE SCAN | T1_IDX2 | 1 |
| 4 | TABLE ACCESS BY INDEX ROWID BATCHED| T1 | 1 |
|* 5 | INDEX RANGE SCAN | T1_IDX1 | 1 |
----------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("N2"=42)
5 - access("N1"=1)
filter(LNNVL("N2"=42))
-- 12cR2 : 12.2.0.1.0
------------------------------------------------------------------------
| Id | Operation | Name | Rows |
-------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 |
| 1 | VIEW | VW_ORE_BA8ECEFB | 2 |
| 2 | UNION-ALL | | |
| 3 | TABLE ACCESS BY INDEX ROWID BATCHED| T1 | 1 |
|* 4 | INDEX RANGE SCAN | T1_IDX1 | 1 |
|* 5 | TABLE ACCESS BY INDEX ROWID BATCHED| T1 | 1 |
|* 6 | INDEX RANGE SCAN | T1_IDX2 | 1 |
-------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - access("N1"=1)
5 - filter(LNNVL("N1"=1))
6 – access("N2"=42)
As you can see starting from Oracle 12cR2 Oracle has enhanced the OR expansion from a simple concatenation operator to a slightly more sophisticated union-all operation. The new OR-Expansion has by now been given a new name recognizable in the execution plan via the word: VW_ORE_###.
But what performance added value this transformation is supposed to bring?
Hopefully one of my real life queries gave me the answer to the above question: in contrast to its old implementation, the new enhanced 12cR2 OR-Expansion can be combined with other transformations opening, as such, a new path to the Oracle optimizer that is simply impossible to happen with the pre-12cR2 OR-Expansion.
A picture being worth a thousand of words here’s below a simple demonstration of how such a double transformation can be combined (the model comes from the previous article):
--12cR1
explain plan for
select
t1.*
,t2.*
from
t1
left outer join
t2
on
(t1.id1 = t2.product_t1
and
(t2.start_date <= date'2012-06-12' and t2.end_date >= date'2012-06-07'
or (t2.id1 > 100)
)
);
-------------------------------------------------------
| Id | Operation | Name | Rows |
-------------------------------------------------------
| 0 | SELECT STATEMENT | | 14949 |
|* 1 | HASH JOIN OUTER | | 14949 |
| 2 | TABLE ACCESS FULL | T1 | 10000 |
| 3 | VIEW | VW_DCL_C83A7ED5 | 9950 |
|* 4 | TABLE ACCESS FULL| T2 | 9950 |
-------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("T1"."ID1"="ITEM_2"(+))
4 - filter("T2"."ID1">100 OR "T2"."START_DATE"<=TO_DATE(' 2012-06-12
00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "T2"."END_DATE">=TO_DATE(' 2012-06-07
00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
If you have read my previous article you will recognize in the above execution plan the Decorrelated Lateral view transformation via its name at line 3 (VW_DCL_C83A7ED5). At my client site this transformation was systematically triggering a full table scan so that I was obliged to cancel it as explained in the corresponding article. I would have instead loved if Oracle had gone a step forward and or-expanded this VW_DCL_C83A7ED5 lateral view in order to eliminate the OR disjunctive predicate to open the index access path possibility. Unfortunately combining these two transformation seems to be impossible by then.
That was until the arrival of the 12cR2 and its new enhanced OR-Expansion.
-- 12cR2 : 12.2.0.1.0
explain plan for
select /*+ OR_EXPAND(@"SEL$6226B99A" (1) (2)) */
t1.*
,t2.*
from
t1
left outer join
t2
on
(t1.id1 = t2.product_t1
and
(t2.start_date <= date'2012-06-12' and t2.end_date >= date'2012-06-07'
or (t2.id1 > 100)
)
);
---------------------------------------------------------
| Id | Operation | Name | Rows |
---------------------------------------------------------
| 0 | SELECT STATEMENT | | 14950 |
|* 1 | HASH JOIN OUTER | | 14950 |
| 2 | TABLE ACCESS FULL | T1 | 10000 |
| 3 | VIEW | VW_DCL_C83A7ED5 | 9951 |
| 4 | VIEW | VW_ORE_37EAC9F1 | 9951 |
| 5 | UNION-ALL | | |
|* 6 | TABLE ACCESS FULL| T2 | 9901 |
|* 7 | TABLE ACCESS FULL| T2 | 50 |
---------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("T1"."ID1"="ITEM_2"(+))
6 - filter("T2"."ID1">100)
7 - filter("T2"."START_DATE"<=TO_DATE(' 2012-06-12 00:00:00', 'syyyy-mm-dd
hh24:mi:ss') AND "T2"."END_DATE">=TO_DATE(' 2012-06-07 00:00:00', 'syyyy-mm-dd
hh24:mi:ss') AND LNNVL("T2"."ID1">100))
Spot now how the 12cR2 Or-Expansion(VW_ORE_37EAC9F1) has been combined with the 12cR1 Decorrelated Lateral view(VW_DCL_C83A7ED5) opening, as such, the possibility to visit t2 table via an index access path since the disjunctive or predicate has been removed from the refactored query (the index has not been used in my case but that’s only a question of cost).
Since the 12cR2 Or-Expansion is a cost based transformation (see below in the 10053 trace file) it happens that Oracle has decided to do not use it. This is why I used the hint OR_EXPAND in order to force it kicking in for the pedagogic purpose of this article.
In order to produce the above execution plan, Oracle has gone through the following SQL refactoring steps:
--first it has OR-Expanded the t2 table query block and named it VW_ORE_37EAC9F1
(select
t2.id1 item_1,
t2.product_t1 item_2,
t2.start_date item_3,
t2.end_date item_4,
t2.padding item_5
from
t2 t2
where t2.id1 >100
union all
select
t2.id1 item_1,
t2.product_t1 item_2,
t2.start_date item_3,
t2.end_date item_4,
t2.padding item_5
from
t2 t2
where
t2.start_date <=to_date('2012-06-12 00:00:00', 'syyyy-mm-dd hh24:mi:ss')
and t2.end_date >=to_date('2012-06-07 00:00:00', 'syyyy-mm-dd hh24:mi:ss')
and lnnvl(t2.id1 >100) VW_ORE_37EAC9F1
--it have then Decorrelated the OR-Expanded query block and named it VW_DCL_C83A7ED5
((select
t2.id1 item_1,
t2.product_t1 item_2,
t2.start_date item_3,
t2.end_date item_4,
t2.padding item_5
from
t2 t2
where t2.id1 >100
union all
select
t2.id1 item_1,
t2.product_t1 item_2,
t2.start_date item_3,
t2.end_date item_4,
t2.padding item_5
from
t2 t2
where
t2.start_date <=to_date('2012-06-12 00:00:00', 'syyyy-mm-dd hh24:mi:ss')
and t2.end_date >=to_date('2012-06-07 00:00:00', 'syyyy-mm-dd hh24:mi:ss')
and lnnvl(t2.id1 >100)
) VW_ORE_37EAC9F1
) VW_DCL_C83A7ED5
--and finally it joined the Decorrelated OR-Expanded t2 table query with t1 table
select
t1.id1 id1,
t1.flag1 flag1,
t1.flag2 flag2,
t1.n1 n1,
t1.v1 v1,
vw_dcl_c83a7Ed5.item_1_0 id1,
vw_dcl_c83a7Ed5.item_2_1 product_t1,
vw_dcl_c83a7Ed5.item_3_2 start_date,
vw_dcl_c83a7Ed5.item_4_3 end_date,
vw_dcl_c83a7Ed5.item_5_4 padding
from t1 t1,
(select
vw_ore_37eac9f1.item_1 item_1_0,
vw_ore_37eac9f1.item_2 item_2_1,
vw_ore_37eac9f1.item_3 item_3_2,
vw_ore_37eac9f1.item_4 item_4_3,
vw_ore_37eac9f1.item_4 item_5_4
from
(select
t2.id1 item_1,
t2.product_t1 item_2,
t2.start_date item_3,
t2.end_date item_4,
t2.padding item_5
from
t2 t2
where t2.id1 >100
union all
select
t2.id1 item_1,
t2.product_t1 item_2,
t2.start_date item_3,
t2.end_date item_4,
t2.padding item_5
from
t2 t2
where
t2.start_date <=to_date('2012-06-12 00:00:00', 'syyyy-mm-dd hh24:mi:ss')
and t2.end_date >=to_date('2012-06-07 00:00:00', 'syyyy-mm-dd hh24:mi:ss')
and lnnvl(t2.id1 >100)
) vw_ore_37eac9f1
) vw_dcl_c83a7Ed5
where t1.id1 = vw_dcl_c83a7ed5.item_2_1(+);
The OR-Expansion appears first at the Legend section of the CBO 10053 trace file:
Legend
The following abbreviations are used by optimizer trace
CBQT – cost-based query transformation
ORE - CBQT OR-Expansion
It can be disabled locally using the hint NO_OR_EXPAND:
explain plan for
select /*+ NO_OR_EXPAND(@"SEL$6226B99A" (1) (2)) */
t1.*
,t2.*
from
t1
left outer join
t2
on
(t1.id1 = t2.product_t1
and
(t2.start_date <= date'2012-06-12' and t2.end_date >= date'2012-06-07'
or (t2.id1 > 100)
)
);
-------------------------------------------------------
| Id | Operation | Name | Rows |
-------------------------------------------------------
| 0 | SELECT STATEMENT | | 14999 |
|* 1 | HASH JOIN OUTER | | 14999 |
| 2 | TABLE ACCESS FULL | T1 | 10000 |
| 3 | VIEW | VW_DCL_C83A7ED5 | 10000 |
|* 4 | TABLE ACCESS FULL| T2 | 10000 |
-------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("T1"."ID1"="ITEM_2"(+))
4 - filter("T2"."ID1">100 OR "T2"."START_DATE"<=TO_DATE(' 2012-06-12
00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "T2"."END_DATE">=TO_DATE(' 2012-06-07
00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
And globally via the hidden param _optimizer_cbqt_or_expansion which defaults to on:
SQL> alter system set "_optimizer_cbqt_or_expansion"= off;
explain plan for
select /*+ OR_EXPAND(@"SEL$6226B99A" (1) (2)) */
t1.*
,t2.*
from
t1
left outer join
t2
on
(t1.id1 = t2.product_t1
and
(t2.start_date <= date'2012-06-12' and t2.end_date >= date'2012-06-07'
or (t2.id1 > 100)
)
);
select * from table(dbms_xplan.display);
--------------------------------------------------------
| Id | Operation | Name | Rows |
---------------------------------------------------------
| 0 | SELECT STATEMENT | | 14999 |
|* 1 | HASH JOIN RIGHT OUTER| | 14999 |
| 2 | VIEW | VW_DCL_C83A7ED5 | 10000 |
|* 3 | TABLE ACCESS FULL | T2 | 10000 |
| 4 | TABLE ACCESS FULL | T1 | 10000 |
---------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("T1"."ID1"="ITEM_2"(+))
3 - filter("T2"."ID1">100 OR "T2"."START_DATE"<=TO_DATE(' 2012-06-12
00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "T2"."END_DATE">=TO_DATE(' 2012-06-07
00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
SUMMARY
Starting from Oracle 12cR2 the OR-Expansion has been enhanced from the classical concatenation to a more flexible union-all operation. This article shows that one of the advantages brought by this enhancement resides in the possibility it offers to the CBO to combine the new OR-Expansion with other transformations like with the Decorrelated Lateral view. This might increase the chance to have optimal execution plan in queries using disjunctive predicates.