2013-09-27 138 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

上面的鏈接正是我需要的!謝謝! –