开发者

How to optimize oracle query from partitioned table?

开发者 https://www.devze.com 2023-03-29 03:25 出处:网络
I have a partitioned table by date (by days) and a local index on the fields (including date field). If I make a query:

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)?


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');
0

精彩评论

暂无评论...
验证码 换一张
取 消