We have two tables like so:
Event
id
type
... a bunch of other columns
ProcessedEvent
event_id
process
There are indexes defined for
- Event(id) (PK)
- ProcessedEvent (event_id, process)
The first represents events in an application.
The second represents the fact that a certain event got processes by a certain process. There are many processes that need to process a certain event, so there are multiple entries in the second table for each entry in the first.
In order to find all the events that need processing we execute the following query:
select * // of course we do name the columns in the production code
from Event
where type in ( 'typeA', 'typeB', 'typeC')
and id not in (
select event_id
from ProcessedEvent
where process = :1
)
Statistics are up to date
Since most events are processed, I think the best execution plan should look something like this
- full index scan on the ProcessedEvent Index
- full index scan on the Event Index
- anti join between the two
- table access with the rest
- filter
Instead Oracle does the following
- full index scan on the ProcessedEvent Index
- full table scan on the Event table
- filter the Event table
- anti join between the two sets
With an index hint I get Oracle to do the following:
- full index scan on the ProcessedEvent Index
- full index scan on the Event Index
- table acces on the Event table
- filter the Event table
- anti join between the two sets
which is really stupid IMHO.
So my question is: what might be the reason for oracle to insist on the early table access?
Addition: The performance is bad. We are fixing the performance problem by selecting just the Event.IDs and then fetching the needed rows 'manually'. But of course that is just a work around.