本文总结了访问索引的几种方法。
1、索引唯一扫描(INDEX UNIQUE SCAN)
(1)结果至多只返回一条记录
示例:
SQL> create table test.t as select * from dba_objects;
Table created
SQL> create unique index t_ius on t(object_id);
Index created.
SQL> select * from t where object_id=99338;
Execution Plan
----------------------------------------------------------
Plan hash value: 3945981348
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 207 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 207 | 2 (0)| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | T_IUS | 1 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
2、索引范围扫描(INDEX RANGE SCAN)
(1)索引范围扫描的结果可能会返回多条记录
示例:
SQL> create index t_irs on t(object_id);
Index created.
SQL> select * from t where object_id=99338;
Execution Plan
----------------------------------------------------------
Plan hash value: 735526888
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 207 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 207 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | T_IRS | 1 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
3、索引全扫描(INDEX FULL SCAN)
(1)扫描目标索引所有叶子块的所有索引行。
(2)索引全扫描的执行结果是有序的,避免了真正排序操作。
(3)通常情况下索引全扫面使用的是单块读。
(4)索引全扫面的前提条件是目标索引键值列的属性是not null.
示例:
SQL> select object_id from t where object_id is not null order by object_id;
72920 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3775890202
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 92167 | 1170K| 176 (0)| 00:00:03 |
|* 1 | INDEX FULL SCAN | T_IRS | 92167 | 1170K| 176 (0)| 00:00:03 |
--------------------------------------------------------------------------
4、索引快速全扫面(NDEX FAST FULL SCAN)
(1)和索引全扫面一样,扫描目标索引所有叶子块的所有索引行。
(2)索引快速全扫描只适用于CBO。
(3)索引快速全扫面可以多块读,也可以并行执行。
(4)索引快速全扫描的执行结果不一定是有序的。
示例:
SQL> select object_id from t where object_id is not null;
72920 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 723523614
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 92167 | 1170K| 49 (0)| 00:00:01 |
|* 1 | INDEX FAST FULL SCAN| T_IRS | 92167 | 1170K| 49 (0)| 00:00:01 |
———————————————————————————————————————d
5、索引跳跃式扫描(INDEX SKIP SCAN)
(1)适用于复合B树索引
(2) where条件中没有对目标索引的前导列指定查询条件同时又对该索引的非前导列指定了查询条件的sql依然可以用上该索引。
示例:
SQL> create index t_isc on t(object_id,object_name);
Index created.
SQL> select /*+ index_ss(t t_isc) */ object_name from t where object_name is not null;
72920 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 2656528524
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 92167 | 5940K| 429 (1)| 00:00:06 |
|* 1 | INDEX SKIP SCAN | T_ISC | 92167 | 5940K| 429 (1)| 00:00:06 |
--------------------------------------------------------------------------
end!
1、索引唯一扫描(INDEX UNIQUE SCAN)
(1)结果至多只返回一条记录
示例:
SQL> create table test.t as select * from dba_objects;
Table created
SQL> create unique index t_ius on t(object_id);
Index created.
SQL> select * from t where object_id=99338;
Execution Plan
----------------------------------------------------------
Plan hash value: 3945981348
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 207 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 207 | 2 (0)| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | T_IUS | 1 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
2、索引范围扫描(INDEX RANGE SCAN)
(1)索引范围扫描的结果可能会返回多条记录
示例:
SQL> create index t_irs on t(object_id);
Index created.
SQL> select * from t where object_id=99338;
Execution Plan
----------------------------------------------------------
Plan hash value: 735526888
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 207 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 207 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | T_IRS | 1 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
3、索引全扫描(INDEX FULL SCAN)
(1)扫描目标索引所有叶子块的所有索引行。
(2)索引全扫描的执行结果是有序的,避免了真正排序操作。
(3)通常情况下索引全扫面使用的是单块读。
(4)索引全扫面的前提条件是目标索引键值列的属性是not null.
示例:
SQL> select object_id from t where object_id is not null order by object_id;
72920 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3775890202
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 92167 | 1170K| 176 (0)| 00:00:03 |
|* 1 | INDEX FULL SCAN | T_IRS | 92167 | 1170K| 176 (0)| 00:00:03 |
--------------------------------------------------------------------------
4、索引快速全扫面(NDEX FAST FULL SCAN)
(1)和索引全扫面一样,扫描目标索引所有叶子块的所有索引行。
(2)索引快速全扫描只适用于CBO。
(3)索引快速全扫面可以多块读,也可以并行执行。
(4)索引快速全扫描的执行结果不一定是有序的。
示例:
SQL> select object_id from t where object_id is not null;
72920 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 723523614
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 92167 | 1170K| 49 (0)| 00:00:01 |
|* 1 | INDEX FAST FULL SCAN| T_IRS | 92167 | 1170K| 49 (0)| 00:00:01 |
———————————————————————————————————————d
5、索引跳跃式扫描(INDEX SKIP SCAN)
(1)适用于复合B树索引
(2) where条件中没有对目标索引的前导列指定查询条件同时又对该索引的非前导列指定了查询条件的sql依然可以用上该索引。
示例:
SQL> create index t_isc on t(object_id,object_name);
Index created.
SQL> select /*+ index_ss(t t_isc) */ object_name from t where object_name is not null;
72920 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 2656528524
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 92167 | 5940K| 429 (1)| 00:00:06 |
|* 1 | INDEX SKIP SCAN | T_ISC | 92167 | 5940K| 429 (1)| 00:00:06 |
--------------------------------------------------------------------------
end!
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25923810/viewspace-2142755/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/25923810/viewspace-2142755/