DmitryB
DmitryB

Reputation: 1179

How to optimize oracle query from partitioned table?

I have a partitioned table by date (by days) and a local index on the fields (including date field). If I make a query:

SELECT * FROM table t WHERE t.fdate = '30.06.2011'

it is fulfilled quickly, but when I make

SELECT * FROM table t WHERE EXTRACT(month from t.fdate) = 6 AND EXTRACT(year from t.fdate) = 2011

it is fulfilled approximately 200 seconds.

How to optimize this query? May be I need to create local index on EXTRACT(month from date) AND EXTRACT(year from date)?

Upvotes: 3

Views: 6033

Answers (1)

Codo
Codo

Reputation: 78855

As you have an index on the date field, you should write your query in a way that this index can be used. This is not possible with the EXTRACT functions since Oracle must go through all data and compute the condition for each row to determine if it matches.

The date index can be used if you have a specific date or a range of dates. In your case, you're looking for a range of dates. So the query could be written as:

SELECT * FROM TABLE T
WHERE T.FDATE BETWEEN TO_DATE('1.6.2011', 'DD.MM.YYYY') AND TO_DATE('30.6.2011', 'DD.MM.YYYY');

Upvotes: 7

Related Questions