使用Index提示 强制使用索引
时间:2016-05-29 03:25 来源:linux.it.net.cn 作者:IT
虽然索引并不总会快于全表扫描,但是很多时候我们希望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
索引和全表扫描的选择和取舍并非简单,本文不作进一步探讨.
(责任编辑:IT)
虽然索引并不总会快于全表扫描,但是很多时候我们希望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 索引和全表扫描的选择和取舍并非简单,本文不作进一步探讨. (责任编辑:IT) |