如果我查询1,2,3,4或5天的数据,我有一个使用日期索引的查询。对于这些查询,数据会在不到10秒的时间内恢复。 6天或更长时间,它决定不使用索引和查询需要永远,如果它回来了。如果我尝试使用索引帮助它似乎没有使用索引。我尝试对索引和表格进行收集统计。这是查询(在这种情况下,9天的数据):
select
r.range_text as duration_range,
nvl(count(c.call_duration),0) as calls,
nvl(SUM(call_duration),0) as total_duration
from
duration_ranges r
left join
big_table c
on c.call_duration >= r.range_lbound AND c.call_duration <= r.range_ubound
where calltimestamp_local >= to_date('20-02-2014 00:00:00','dd-MM-yyyy HH24:mi:ss')
and calltimestamp_local <= to_date('28-02-2014 23:59:59' ,'dd-MM-yyyy HH24:mi:ss')
and c.destinationnumber = 'sip:1000@company.com:5060;user=phone'
group by
r.range_text
order by
r.range_text;
这是没有索引的解释计划:
Without Index
SELECT STATEMENT ALL_ROWSCost: 827,605 Bytes: 1,344 Cardinality: 14
8 SORT GROUP BY Cost: 827,605 Bytes: 1,344 Cardinality: 14
7 MERGE JOIN Cost: 827,486 Bytes: 246,552,768 Cardinality: 2,568,258
2 SORT JOIN Cost: 4 Bytes: 308 Cardinality: 14
1 TABLE ACCESS FULL TABLE MYDB.DURATION_RANGES Cost: 3 Bytes: 308 Cardinality: 14
6 FILTER
5 SORT JOIN Cost: 827,471 Bytes: 14,164,118 Cardinality: 191,407
4 PARTITION RANGE ALL Cost: 824,134 Bytes: 14,164,118 Cardinality: 191,407 Partition #: 7 Partitions accessed #1 - #1653
3 TABLE ACCESS FULL TABLE MYDB.BIG_TABLE Cost: 824,134 Bytes: 14,164,118 Cardinality: 191,407 Partition #: 7 Partitions accessed #1 - #1653
使用索引:
With Index
Plan
SELECT STATEMENT ALL_ROWSCost: 822,732 Bytes: 1,344 Cardinality: 14
8 SORT GROUP BY Cost: 822,732 Bytes: 1,344 Cardinality: 14
7 MERGE JOIN Cost: 822,635 Bytes: 205,460,736 Cardinality: 2,140,216
2 SORT JOIN Cost: 4 Bytes: 308 Cardinality: 14
1 TABLE ACCESS FULL TABLE MYDB.DURATION_RANGES Cost: 3 Bytes: 308 Cardinality: 14
6 FILTER
5 SORT JOIN Cost: 822,621 Bytes: 11,803,444 Cardinality: 159,506
4 TABLE ACCESS BY GLOBAL INDEX ROWID TABLE MYDB.BIG_TABLE Cost: 819,841 Bytes: 11,803,444 Cardinality: 159,506 Partition #: 7 Partition access computed by row location
3 INDEX RANGE SCAN INDEX MYDB.IDX_CDR_CALLTIMESTAMP_LOCAL Cost: 2,744 Cardinality: 850,691
任何想法为什么它不能使用索引如果在5天的数据中它会快速恢复?它似乎并没有优化,因为它回来的速度要慢得多。
答案 0 :(得分:0)
也许它的工作速度较慢,因为数据和索引不会有太大变化?您可以通过强制Oracle使用索引来轻松检查它 - 指定INDEX
提示,然后将时间与索引进行比较,而不使用它来进行相同的查询。
您还可以指定NOINDEX
提示,以比较5天或更短天数的时间。