iis服务器助手广告
返回顶部
首页 > 资讯 > 数据库 >How to Specify an INDEX Hint oracle官方文档
  • 927
分享到

How to Specify an INDEX Hint oracle官方文档

2024-04-02 19:04:59 927人浏览 薄情痞子
摘要

How to Specify an INDEX Hint (Doc ID 50607.1) Applies to: oracle Database - Enterprise Edition - Versio

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


     
您可能感兴趣的文档:

--结束END--

本文标题: How to Specify an INDEX Hint oracle官方文档

本文链接: https://www.lsjlt.com/news/48884.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作