Oracle技術:使用Index提示強制使用索引

來源:互聯網
上載者:User

雖然索引並不總會快於全表掃描,但是很多時候我們希望Oracle使用索引來執行某些SQL,這時候我們可以通過index hints來強制SQL使用index.

Index Hints的格式如下:

/*+ INDEX ( table [index [index]...] ) */我們簡單看一下這個提示的用法(範例為Oracle10g資料庫):

SQL> create table t as select username,password from dba_users;Table created.SQL> create index i_t on t(username);Index created.SQL> set autotrace trace explainSQL> select /*+ index(t i_t) */ * from t where username='EYGLE';Execution Plan----------------------------------------------------------Plan hash value: 2928007915------------------------------------------------------------------------------------| Id  | Operation                   | Name | Rows  | Bytes | Cost (%CPU)| Time     |------------------------------------------------------------------------------------|   0 | SELECT STATEMENT            |      |     1 |    34 |     2   (0)| 00:00:01 ||   1 |  TABLE ACCESS BY INDEX ROWID| T    |     1 |    34 |     2   (0)| 00:00:01 ||*  2 |   INDEX RANGE SCAN          | I_T  |     1 |       |     1   (0)| 00:00:01 |------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------   2 - access("USERNAME"='EYGLE')Note-----   - dynamic sampling used for this statement 這裡的查詢使用了索引.

更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/database/Oracle/

需要注意的是使用CTAS方式建立資料表,建立表會繼承原表的約束屬性:

SQL> desc t Name                                      Null?    Type ----------------------------------------- -------- ---------------------------- USERNAME                                  NOT NULL VARCHAR2(30) PASSWORD                                           VARCHAR2(30)  

如果不使用Hints,此處Oracle不會使用索引:

SQL> select * from t where username='EYGLE';Execution Plan----------------------------------------------------------Plan hash value: 1601196873--------------------------------------------------------------------------| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |--------------------------------------------------------------------------|   0 | SELECT STATEMENT  |      |     1 |    34 |     2   (0)| 00:00:01 ||*  1 |  TABLE ACCESS FULL| T    |     1 |    34 |     2   (0)| 00:00:01 |--------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------   1 - filter("USERNAME"='EYGLE')Note-----   - dynamic sampling used for this statement 索引和全表掃描的選擇和取捨並非簡單,本文不作進一步探討.

作者:51cto Oracle小混子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.