oracle-databaseindexingfull-table-scan

Using index to speed up child <> parent query


I have query similar to this:

select *
from table1
where status = 'ACTV'
and child_id <> parent_id

The problem is that this table is quite and large and Oracle is doing full table scan. I was trying to create an index (with status, child_id, parent_id columns) that would speed up this query but Oracle is not using this index even with hint.

Is there a way to speed up this query ?


Solution

  • You can use index with function:

    CREATE INDEX child_parent ON table1(DECODE(child_id,parent_id,1, 0))
    

    And then use it in your select:

    select *
    from table1
    where status = 'ACTV'
      and DECODE(child_id,parent_id,1, 0) = 0
    

    Only cons for this solution - it will slow down insert and update operations a bit more than regular index. Also if potentially returnable record count is large Oracle can do table full scan