文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

How to Specify an INDEX Hint oracle官方文档

2024-04-02 19:55

关注
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 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 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 col1 from cbotab;
 explain plan for select col1 from cbotab;
 explain plan for select 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 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 col1 from cbotab;
     explain plan for select 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 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 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 col1 from cbotab;
    SQL>  explain plan for select 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 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 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 col1 from cbotab;
    Query Plan
    --------------------------------------------------------------------------------
    SELECT STATEMENT   [CHOOSE] Cost=10
      TABLE ACCESS FULL CBOTAB [ANALYZED]  Cost=10 Card=10000 Bytes=100000


     
阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯