如何优化分区表的oracle查询?
我有一个按日期(按天)分区的表和字段(包括日期字段)的本地索引。如果我进行查询:
SELECT * FROM 表 t WHERE t.fdate = '30.06.2011'
它很快就完成了,但是当我做
SELECT * FROM 表 t WHERE EXTRACT(t.fdate 中的月份) = 6 AND EXTRACT(从 t.fdate 算起的年份)= 2011
大约需要 200 秒。
如何优化这个查询?我可能需要在 EXTRACT(从日期开始的月份)和 EXTRACT(从日期开始的年份)上创建本地索引吗?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您在日期字段上有索引,因此您应该以可以使用该索引的方式编写查询。这对于 EXTRACT 函数来说是不可能的,因为 Oracle 必须遍历所有数据并计算每行的条件以确定其是否匹配。
如果您有特定日期或日期范围,则可以使用日期索引。就您而言,您正在寻找一系列日期。所以查询可以写成:
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: