Your query performs basically two access, so (provided you select only a small number of orders) you need two indexes to reflect it.
The first index select the orders
with propper action
and object
in a given time range
("ACTION","OBJECT",datetimecreated)
The second index then queries all records for those orders
with requested action
and object
("ORDER", "ACTION" ,"OBJECT")
To demostrate the principal, I created a test table with 2M rows and 100K orders (see script below).
Note, that I don't use reserved names (e.g. "ORDER" is order_id) and I use "neutral" names for action and object.
select order_id,
max(case when action_id = 'A1' then datetimecreated end) as A1_date,
max(case when action_id = 'A2' then datetimecreated end) as A2_date,
max(case when action_id = 'A3' then datetimecreated end) as A3_date,
max(case when action_id = 'A4' then datetimecreated end) as A4_date,
max(case when action_id = 'A5' then datetimecreated end) as A5_date
from test
where order_id in (
select order_id
from test
where datetimecreated >= DATE'2018-02-05' and datetimecreated <= DATE'2018-02-06' and
action_id = 'A1' and object_id = 'O1')
AND
(action_id = 'A1' and object_id = 'O1' or
action_id = 'A2' and object_id = 'O2' or
action_id = 'A3' and object_id = 'O3' or
action_id = 'A4' and object_id = 'O4' or
action_id = 'A5' and object_id = 'O1')
group by order_id;
ORDER_ID A1_DATE A2_DATE A3_DATE A4_DATE A5_DATE
---------- -------- -------- -------- -------- --------
826 05.02.18 10.02.18 15.02.18 20.02.18 21.02.18
833 05.02.18 10.02.18 15.02.18 20.02.18 21.02.18
.....
823 05.02.18 10.02.18 15.02.18 20.02.18 21.02.18
838 05.02.18 10.02.18 15.02.18 20.02.18 21.02.18
25 rows selected.
Elapsed: 00:00:00.10
You expect this execution plan - the inner NESTED LOOP performs the two access described above - see the
access condition 5 and 7.
Plan hash value: 1930696803
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 223 | 13380 | 12 (9)| 00:00:01 |
| 1 | HASH GROUP BY | | 223 | 13380 | 12 (9)| 00:00:01 |
| 2 | NESTED LOOPS | | | | | |
| 3 | NESTED LOOPS | | 223 | 13380 | 11 (0)| 00:00:01 |
| 4 | TABLE ACCESS BY INDEX ROWID| TEST | 1 | 30 | 4 (0)| 00:00:01 |
|* 5 | INDEX RANGE SCAN | TEST_IDX1 | 1 | | 3 (0)| 00:00:01 |
| 6 | INLIST ITERATOR | | | | | |
|* 7 | INDEX RANGE SCAN | TEST_IDX2 | 1 | | 6 (0)| 00:00:01 |
| 8 | TABLE ACCESS BY INDEX ROWID | TEST | 502 | 15060 | 7 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
5 - access("ACTION_ID"='A1' AND "OBJECT_ID"='O1' AND
"DATETIMECREATED">=TO_DATE(' 2018-02-05 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
"DATETIMECREATED"<=TO_DATE(' 2018-02-06 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
7 - access("ORDER_ID"="ORDER_ID" AND ("ACTION_ID"='A1' AND "OBJECT_ID"='O1' OR
"ACTION_ID"='A2' AND "OBJECT_ID"='O2' OR "ACTION_ID"='A3' AND "OBJECT_ID"='O3' OR
"ACTION_ID"='A4' AND "OBJECT_ID"='O4' OR "ACTION_ID"='A5' AND "OBJECT_ID"='O1'))
Here the script to produce test data to play with:
create table test as
with ord as (
select rownum order_id, date'2018-01-01' + rownum / 24 datetimecreated from dual connect by level <= 100000),
act as (
select 'A1' action_id, 'O1' object_id, 1 offset from dual union all
select 'A1' action_id, 'O2' object_id, 2 offset from dual union all
select 'A1' action_id, 'O3' object_id, 3 offset from dual union all
select 'A1' action_id, 'O4' object_id, 4 offset from dual union all
select 'A2' action_id, 'O1' object_id, 5 offset from dual union all
select 'A2' action_id, 'O2' object_id, 6 offset from dual union all
select 'A2' action_id, 'O3' object_id, 7 offset from dual union all
select 'A2' action_id, 'O4' object_id, 8 offset from dual union all
select 'A3' action_id, 'O1' object_id, 9 offset from dual union all
select 'A3' action_id, 'O2' object_id, 10 offset from dual union all
select 'A3' action_id, 'O3' object_id, 11 offset from dual union all
select 'A3' action_id, 'O4' object_id, 12 offset from dual union all
select 'A4' action_id, 'O1' object_id, 13 offset from dual union all
select 'A4' action_id, 'O2' object_id, 14 offset from dual union all
select 'A4' action_id, 'O3' object_id, 15 offset from dual union all
select 'A4' action_id, 'O4' object_id, 16 offset from dual union all
select 'A5' action_id, 'O1' object_id, 17 offset from dual union all
select 'A5' action_id, 'O2' object_id, 18 offset from dual union all
select 'A5' action_id, 'O3' object_id, 19 offset from dual union all
select 'A5' action_id, 'O4' object_id, 20 offset from dual)
select order_id, datetimecreated + offset datetimecreated, action_id,object_id from ord
cross join act;
create index test_idx1 on test(action_id, object_id,datetimecreated);
create index test_idx2 on test(order_id,action_id, object_id);