How to Specify an INDEX Hint oracle官方文件
How to Specify an INDEX Hint (Doc ID 50607.1)
Applies to:
Oracle Database - Enterprise Edition - Version 9.2.0.1 to 11.2.0.2 [Release 9.2 to 11.2]
Information in this document applies to any platform.
Purpose
This article explains how to specify index hints successfully.
Troubleshooting Steps
The format for an index hint is:
select /*+ index(TABLE_NAME INDEX_NAME) */ col1...
There are a number of rules that need to be applied to this hint:
The TABLE_NAME is mandatory in the hint
The table alias MUST be used if the table is aliased in the query
If TABLE_NAME or alias is spelled incorrectly then the hint will not be used.
The INDEX_NAME is optional.
If an INDEX_NAME is entered without a TABLE_NAME then the hint will not be applied.
If a TABLE_NAME is supplied on its own then the optimizer will decide which index to use based on statistics.
If the INDEX_NAME is spelt incorrectly but the TABLE_NAME is spelled correctly then the hint will not be applied even though the TABLE_NAME is correct.
If there are multiple index hints to be applied, then the simplest way of addressing this is to repeat the index hint syntax for each index e.g.:
SELECT /*+ index(TABLE_NAME1 INDEX_NAME1) index(TABLE_NAME2 INDEX_NAME2) */ col1...
Remember that the parser/optimizer may have transformed/rewritten the query or may have chosen an access path which make the use of the index invalid and this may result in the index not being used.
Legacy Note: As long as the index() hint structure is correct this will force the use of the Cost Based Optimizer (CBO). This will happen even if the alias or table name is incorrect.
Examples
The examples below use a table CBOTAB with a unique single column index called CBOTAB1 on column COL1.
Correct hint to force use of the index:
explain plan for select /*+ index(cbotab) */ col1 from cbotab;
explain plan for select /*+ index(cbotab cbotab1) */ col1 from cbotab;
explain plan for select /*+ index(a cbotab1) */ col1 from cbotab a;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=151
INDEX FULL SCAN CBOTAB1 [ANALYZED] Cost=151 Card=10000 Bytes=100000
The TABLE_NAME is mandatory in the hint.
In the following example the TABLE_NAME was omitted so the index was not used.
SQL> explain plan for select /*+ index() */ col1 from cbotab;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=10
TABLE ACCESS FULL CBOTAB [ANALYZED] Cost=10 Card=10000 Bytes=100000
The INDEX_NAME is optional.
Both of the following examples use the index:
explain plan for select /*+ index(cbotab) */ col1 from cbotab;
explain plan for select /*+ index(cbotab cbotab1) */ col1 from cbotab;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=151
INDEX FULL SCAN CBOTAB1 [ANALYZED] Cost=151 Card=10000 Bytes=100000
The table alias MUST be used if the table is aliased in the query
SQL> explain plan for select /*+ index(cbotab) */ col1 from cbotab mytable;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=10
TABLE ACCESS FULL CBOTAB [ANALYZED] Cost=10 Card=10000 Bytes=100000
Correct use of alias in hint:
SQL> explain plan for select /*+ index(mytable) */ col1 from cbotab mytable;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=151
INDEX FULL SCAN CBOTAB1 [ANALYZED] Cost=151 Card=10000 Bytes=100000
If TABLE_NAME or alias is spelled incorrectly then the hint will not be used.
SQL> explain plan for select /*+ index(COBTAB) */ col1 from cbotab;
SQL> explain plan for select /*+ index(MITABLE) */ col1 from cbotab mytable;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=10
TABLE ACCESS FULL CBOTAB [ANALYZED] Cost=10 Card=10000 Bytes=100000
If an INDEX_NAME is entered without a TABLE_NAME then the hint will not be applied.
SQL> explain plan for select /*+ index(cbotab1) */ col1 from cbotab;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=10
TABLE ACCESS FULL CBOTAB [ANALYZED] Cost=10 Card=10000 Bytes=100000
If a TABLE_NAME is supplied on its own then the optimizer will decide which index to use based on statistics.
explain plan for select /*+ index(cbotab) */ col1 from cbotab;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=151
INDEX FULL SCAN CBOTAB1 [ANALYZED] Cost=151 Card=10000 Bytes=100000
If the INDEX_NAME is spelt incorrectly but the TABLE_NAME is spelt correctly then the hint will not be applied even though the TABLE_NAME is correct.
SQL> explain plan for select /*+ index(cbotab COBTAB1) */ col1 from cbotab;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=10
TABLE ACCESS FULL CBOTAB [ANALYZED] Cost=10 Card=10000 Bytes=100000
Applies to:
Oracle Database - Enterprise Edition - Version 9.2.0.1 to 11.2.0.2 [Release 9.2 to 11.2]
Information in this document applies to any platform.
Purpose
This article explains how to specify index hints successfully.
Troubleshooting Steps
The format for an index hint is:
select /*+ index(TABLE_NAME INDEX_NAME) */ col1...
There are a number of rules that need to be applied to this hint:
The TABLE_NAME is mandatory in the hint
The table alias MUST be used if the table is aliased in the query
If TABLE_NAME or alias is spelled incorrectly then the hint will not be used.
The INDEX_NAME is optional.
If an INDEX_NAME is entered without a TABLE_NAME then the hint will not be applied.
If a TABLE_NAME is supplied on its own then the optimizer will decide which index to use based on statistics.
If the INDEX_NAME is spelt incorrectly but the TABLE_NAME is spelled correctly then the hint will not be applied even though the TABLE_NAME is correct.
If there are multiple index hints to be applied, then the simplest way of addressing this is to repeat the index hint syntax for each index e.g.:
SELECT /*+ index(TABLE_NAME1 INDEX_NAME1) index(TABLE_NAME2 INDEX_NAME2) */ col1...
Remember that the parser/optimizer may have transformed/rewritten the query or may have chosen an access path which make the use of the index invalid and this may result in the index not being used.
Legacy Note: As long as the index() hint structure is correct this will force the use of the Cost Based Optimizer (CBO). This will happen even if the alias or table name is incorrect.
Examples
The examples below use a table CBOTAB with a unique single column index called CBOTAB1 on column COL1.
Correct hint to force use of the index:
explain plan for select /*+ index(cbotab) */ col1 from cbotab;
explain plan for select /*+ index(cbotab cbotab1) */ col1 from cbotab;
explain plan for select /*+ index(a cbotab1) */ col1 from cbotab a;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=151
INDEX FULL SCAN CBOTAB1 [ANALYZED] Cost=151 Card=10000 Bytes=100000
The TABLE_NAME is mandatory in the hint.
In the following example the TABLE_NAME was omitted so the index was not used.
SQL> explain plan for select /*+ index() */ col1 from cbotab;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=10
TABLE ACCESS FULL CBOTAB [ANALYZED] Cost=10 Card=10000 Bytes=100000
The INDEX_NAME is optional.
Both of the following examples use the index:
explain plan for select /*+ index(cbotab) */ col1 from cbotab;
explain plan for select /*+ index(cbotab cbotab1) */ col1 from cbotab;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=151
INDEX FULL SCAN CBOTAB1 [ANALYZED] Cost=151 Card=10000 Bytes=100000
The table alias MUST be used if the table is aliased in the query
SQL> explain plan for select /*+ index(cbotab) */ col1 from cbotab mytable;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=10
TABLE ACCESS FULL CBOTAB [ANALYZED] Cost=10 Card=10000 Bytes=100000
Correct use of alias in hint:
SQL> explain plan for select /*+ index(mytable) */ col1 from cbotab mytable;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=151
INDEX FULL SCAN CBOTAB1 [ANALYZED] Cost=151 Card=10000 Bytes=100000
If TABLE_NAME or alias is spelled incorrectly then the hint will not be used.
SQL> explain plan for select /*+ index(COBTAB) */ col1 from cbotab;
SQL> explain plan for select /*+ index(MITABLE) */ col1 from cbotab mytable;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=10
TABLE ACCESS FULL CBOTAB [ANALYZED] Cost=10 Card=10000 Bytes=100000
If an INDEX_NAME is entered without a TABLE_NAME then the hint will not be applied.
SQL> explain plan for select /*+ index(cbotab1) */ col1 from cbotab;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=10
TABLE ACCESS FULL CBOTAB [ANALYZED] Cost=10 Card=10000 Bytes=100000
If a TABLE_NAME is supplied on its own then the optimizer will decide which index to use based on statistics.
explain plan for select /*+ index(cbotab) */ col1 from cbotab;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=151
INDEX FULL SCAN CBOTAB1 [ANALYZED] Cost=151 Card=10000 Bytes=100000
If the INDEX_NAME is spelt incorrectly but the TABLE_NAME is spelt correctly then the hint will not be applied even though the TABLE_NAME is correct.
SQL> explain plan for select /*+ index(cbotab COBTAB1) */ col1 from cbotab;
Query Plan
--------------------------------------------------------------------------------
SELECT STATEMENT [CHOOSE] Cost=10
TABLE ACCESS FULL CBOTAB [ANALYZED] Cost=10 Card=10000 Bytes=100000
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/6126/viewspace-2144466/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle hint之hint_index_ffs,index_joinOracleIndex
- oracle hint_no_indexOracleIndex
- oracle hint_no_parallel_no_parallel_indexOracleParallelIndex
- oracle hint_parallel_parallel_indexOracleParallelIndex
- oracle hint_skip scan_index_ssOracleIndex
- oracle 官方文件Oracle
- oracle hint之full,index,index_asc,index_desc,index_combile示例OracleIndex
- LOCAL INDEX和HINT的使用【DO BE USED LOCAL INDEX IN HINT】薦Index
- oracle hint_use_concat_use_nl_with_indexOracleIndex
- Oracle OCP(33):官方文件Oracle
- Oracle 線上官方文件Oracle
- Oracle 官方文件下載Oracle
- ORACLE官方文件歸類Oracle
- Oracle官方中文支援文件Oracle
- oracle不同版本的官方文件Oracle
- 轉:oracle官方文件介紹Oracle
- oracle的官方文件連結Oracle
- 【HINT】使用“NO_INDEX ”Hint提示避免SQL使用特定索引IndexSQL索引
- 【SQL 提示 之二】index_ss Index Skip HintSQLIndex
- Oracle 官方文件 結構說明Oracle
- Oracle官方文件結構說明Oracle
- Oracle 官方文件閱讀順序Oracle
- ORACLE 11g 官方文件 地址Oracle
- 如何全文搜尋oracle官方文件Oracle
- How to Find Out How Much Space an Index is UsingIndex
- oracle hintOracle
- hint IGNORE_ROW_ON_DUPKEY_INDEXIndex
- Oracle 12C 官方文件地圖Oracle地圖
- Oracle 如何高效的檢視官方文件Oracle
- Oracle官方文件及個人書籤地址Oracle
- oracle 官方文件及個人書籤地址Oracle
- How to Determine When an Index Should be Rebuilt?IndexUI
- Oracle 12C 單例項安裝文件( 官方文件)Oracle單例
- Oracle Maximum Availability Architecture(MAA)官方文件OracleAI
- Oracle 官方文件檢視及下載方法Oracle
- ORACLE 部分HINTOracle
- oracle hint (續)Oracle
- Oracle Hint 精華Oracle