尽管有提示,Oracle仍不使用不可见索引

时间:2018-12-13 12:42:12

标签: oracle indexing oracle12c hint

我似乎在弄乱索引提示语法,但是我尝试了所有我能想到的模式/表/索引组合。

表和索引与SYS用户(我正在使用其测试索引提示的用户)处于不同的架构中

这是没有提示的语句

select id from car.event where dat < sysdate and type != 0

这些是我尝试在dat_type模式中为索引car实现索引提示的方式

select /*+ index(car.event car.dat_type) */ id from car.event where dat < sysdate and type != 0
select /*+ index(event car.dat_type) */ id from car.event where dat < sysdate and type != 0
select /*+ index(car.event dat_type) */ id from car.event where dat < sysdate and type != 0
select /*+ index(event dat_type) */ id from car.event where dat < sysdate and type != 0
select /*+ index(event (dat, type)) */ id from car.event where dat < sysdate and type != 0

因此,对于这五个语句,我查找了五个不同的sql_id,并查看了这样的执行计划

select * from table(dbms_xplan.display_awr([sql_id]));

但是它们都不显示索引的用法。它们都使用20的DoP。是否必须显式禁用并行性才能使用索引?或者有人可以更正我的索引提示的语法吗?

这是dat_type索引的定义

create index car.dat_type on car.event(dat, type) online;

编辑:索引设置为不可见,因此其他语句无法使用该索引,但是我想与索引提示一起显式使用它。在我的理解中,隐含性应该不是索引提示的问题。如果我错了,请纠正我。

3 个答案:

答案 0 :(得分:3)

我偶然发现this article,这表明实际上不可能仅使用索引提示来使用不可见索引。但是,不可见索引可以与附加提示USE_INVISIBLE_INDEXES一起使用。

这就是我的工作方式:

select /*+ use_invisible_indexes index(car dat_type) */ id from car.event where dat < sysdate and type != 0

答案 1 :(得分:0)

您忘记了在示例中选择ID列。您必须指定架构名称和没有架构“点”的索引。

您可以这样做:

select /*+ index(car dat_type) */ id from car.event where dat < sysdate and type != 0

select /*+ index(car, dat_type) */ id from car.event where dat < sysdate and type != 0

这并不总是强制索引,但是正确使用语法是一个好的开始。有时基于成本的优化工具(CBO)固执己见...

答案 2 :(得分:0)

我还没有真正使用过不可见的索引,但是我对文档的阅读表明仅凭提示不会启用它们。您必须在系统或会话级别使用初始化参数:

  

不可见索引是优化程序忽略的索引,除非   您可以显式设置OPTIMIZER_USE_INVISIBLE_INDEXES初始化   参数在会话或系统级别为TRUE。

(来自https://docs.oracle.com/cd/B28359_01/server.111/b28310/indexes003.htm#ADMIN12317

根据我对带有 visible 索引的测试,提示中的以下两个选项均按预期工作:

/*+ index(event) */
/*+ index(event dat_type) */

可能其他的替代方法也可以使用,但是这些是最简单的方法,并且效果很好。问题不在于您的索引提示语法。这是您需要采取另一步骤来启用不可见索引的使用。

编辑后添加:如发现的OP所示,启用不可见索引的另一种方法是USE_INVISIBLE_INDEX提示。要仅通过提示来完成OP所需的功能,必须同时指定USE_INVISIBLE_INDEX提示和INDEX提示。