I am working on a sql query and trying to optimize it because it takes too long.
I have several select and UNION between them. Each selection is in one table, but with a different condition in the WHERE clause. Basically, I always have something like:
select * from A
where field1 <=TO_DATE ('01/01/2010', 'DD/MM/YYYY')
AND field1 >= TO_DATE(some date)
and field2 IN (...)
UNION
select * from A
where field1 <=TO_DATE ('01/01/2010', 'DD/MM/YYYY')
AND field1 >= TO_DATE(some date2)
and field2 =(...)
UNION
....
I have an index in field1 (this is a date field, and field2 is a number). Now, when I make a choice, and if I bet only
WHERE field1 <TO_DATE ('01/01/2010', 'DD/MM/YYYY')
It does not use an index. I use Toad to see an explanation, and he said:
SELECT STAITEMENT Optimiser Mode = CHOOSE
TABLE ACCESS FULL
This is a huge table, and there is an index in this column.
Any ideas on this optimizer? And why doesn't he use an index?
Another question: if I have a where clause in field1 and field2, do I need to create only one index or one index for each field?