eygle.com   eygle.com
eygle.com  
 

« 如何才能防止拉网页? | Blog首页 | 索引与Null值对于Hints及执行计划的影响 »

使用Index提示 强制使用索引

作者:eygle |【转载时请以超链接形式标明文章和作者信息及本声明
链接:

虽然索引并不总会快于全表扫描,但是很多时候我们希望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 explain
SQL> 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 

这里的查询使用了索引.

需要注意的是使用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 

索引和全表扫描的选择和取舍并非简单,本文不作进一步探讨.


历史上的今天...
      >> 2008-02-21文章:
      >> 2005-02-21文章:
             自己动手,丰衣足食
------
这篇 【使用Index提示 强制使用索引】来自 www.eygle.com | CSDN技术网摘| del.icio.us|365Key

By eygle on 2006-02-21 22:44 | Comments (4) | Posted to SQL.PLSQL | Edit |Pageviews:

相关文章 随机文章
  • 索引与Null值对于Hints及执行计划的影响
  • 基于自定义函数的Function-Based索引创建
  • 什么是ABM、ABC、ABP - 学习
  • Linux + Oracle 数据库系统启动能有多快?
  • Oracle Patch Set Note And Bug List 参考
  • streams流复制中如何初始化复制对象
    Oracle还是IBM的中文推广力度大?
    一个工作机会PDM/DBA/上海
    My answer for-9个动态性能视图
    DBA警示录:安装Oracle勿忘防火墙
    搜索本站:

    留言 (4)

    在执行完 set autotrace trace explain
    我为什么不能出现文章中所贴内容中的横线和竖线哪些分隔线呢,而且内容也稍稍有不同,我是在9I上做的。是版本的问题吗?

    Posted by: peso at October 1, 2006 5:31 PM

    5959

    Posted by: 5959 at January 21, 2008 2:51 PM

    我测的的都能用到索引。为什么会用不到呢?

    Posted by: 双629 at August 19, 2008 2:36 PM

    在oracle10g上运行都可以种到index,这个是不是因为10g做了优化?

    Posted by: xinmans at December 6, 2008 10:24 PM

    发表留言:



    Remember Me?
    (输入验证码后方可评论,谢谢支持)



    CopyRight © 2004 eygle.com, All rights reserved.