Oracle technology: Using index hints to force indexes

Source: Internet
Author: User
Tags create index hash

Although indexes are not always faster than full table scans, many times we want Oracle to use indexes to execute certain SQL, at which point we can force SQL to use index through the index hints.

The format of the Index hints is as follows:

/*+ index (table [index [index] ...]) * * Let's take a quick look at the use of this hint (the example is the oracle10g database):

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----------------------------------------------------------The 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 &NBSp        | 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 the query here uses the index.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/database/Oracle/

Note that you create a datasheet using the CTAs method, and the new table inherits the constraint properties of the original table:

Sql> desc t Name Null?                                  Type-----------------------------------------------------------------------------USERNAME Not NULL VARCHAR2 (a) PASSWORD VARCHAR2 (30)

If you do not use hints, the index is not used here by Oracle:

Sql> select * from t where username= ' eygle '; Execution Plan----------------------------------------------------------The 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 the selection and trade-offs of the statement index and full table scan are not simple, this article does not discuss further.

Author: 51cto Oracle small-mixed son

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.