2013-09-27 136 views
1

我有表索引范围扫描VS索引跳跃式扫描VS索引快速全扫描

test_A(
    id1 number, 
    id2 number, 
    id3 number, 
    name varchar2(10), 
    create_dt date 
) 

我有两个指标一个综合指数indx1(id1,id2)indx2(id3)。现在,当我查询此表作为test_A

select * from test_A where id2=123 and 
create_dt=(select max(create_dt) from test_A where test_A.id2=id2); 

我跑了解释计划,这上面的SQL,它是使用“索引跳跃式扫描”。如果我在create_dt上创建了另一个索引,那么它使用索引快速全面扫描以及全部成本和%cpu显示的比带索引跳过扫描的计划更高。在create_dt上创建索引后,它也使用索引范围扫描。

我不能得出结论应该可以吗?我是否需要在create_dt上创建另一个索引或索引跳过扫描是否良好?我相信索引跳过是Oracle运行多索引范围扫描的一项功能吗?

回答

2

,我建议你使用这个链接了解:http://docs.oracle.com/cd/E16655_01/server.121/e15858/tgsql_optop.htm#CHDFJIJA
据甲骨文12C相关,但它是非常有用的,以取得理解甲骨文在所有DBMS版本如何使用不同的索引访问pathes。


你的查询是ambigous:

select max(create_dt) from test_A where test_A.id2=id2 

相同的test_A.id2都test_A.id2和ID2引用和查询是相同的:

select * from test_A where id2=123 and 
create_dt=(select max(create_dt) from test_A where id2=id2); 

或者干脆:

select * from test_A where id2=123 and 
create_dt=(select max(create_dt) from test_A where id2 is not null); 



我想你想是这样的:

select * from test_A where id2=123 and 
create_dt=(select max(create_dt) 
      from test_A ALIAS 
      where test_A.id2=ALIAS.id2); 

以上查询上ID2 + create_dt一个综合指数最有可能提供最好的结果,试试吧:

CREATE INDEX index_name ON test_A(id2, create_dt); 
+0

感谢上述溶液。我在索引中创建了合成索引,这些索引在解释计划中降低了成本。我还有一个问题,假设我在(id1,id2)上的另一个表上创建了唯一约束,并且我们是否需要在外部创建索引或者是否使用唯一约束来处理索引。我已经在很多论坛上看到了这个说法,有人说它会照顾Index创建索引的一些需要,不能就此作出结论。请让我知道你是否有这方面的任何信息。 – user2824874

+0

只需亲自尝试一下。看到这个链接:http://www.sqlfiddle.com/#!2/180be/1那里有三个不同的表格,第一个没有索引,第二个索引用'create index'创建,第三个用'约束..唯一'在表格定义内。运行附加的查询,然后单击“查看执行计划”链接并比较个人解释计划,以查看他们是否使用索引。 – krokodilko

+0

上面的链接正是我需要的!谢谢! –