Question

I was wondering what is the best performance implementation of this query:

select subscriber 
from DEGREE
where sub_type is null 
 and (subscriber like '91_9%' 
      or SUBSCRIBER like '919%' 
      or (SUBSCRIBER not in (select msisdn from MCI))) 

FYI MCI table has around 50m records with defined index on msisdn and DEGREE table has index on subscriber.

Best regards.

Was it helpful?

Solution

I would suggest writing the query like this:

select d.subscriber 
from DEGREE d
where d.sub_type is null and
      (d.subscriber like '91_9%' or
       d.subscriber like '919%' or
       not exists (select 1 from MCI where MCI.msisdn = d.subscriber)
      );

Then create indexes on degree(sub_type, subscriber) and MCI(msisdn).

EDIT:

The index on degree should cause the following to happen. Instead of a full table scan on DEGREE, the query will do an index scan on the index. I'm not sure if Oracle is smart enough to use the index for like conditions connected by or, but at least this is a covering index for the query.

The index on MCI should then result in a simple index lookup.

So, with these two indexes, only index operations will be needed to satisfy the query. That should have a significant impact on performance.

OTHER TIPS

select subscriber 
from DEGREE left outer join MCI on DEGREE.SUBSCRIBER=MCI.msisdn 
where sub_type is null 
 and (subscriber like '91_9%' 
      or SUBSCRIBER like '919%' 
      or MCI.msisdn is null) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top