Oracle 索引扫描的五种类型


根据索引的类型与where限制条件的不同,有4种类型的Oracle索引扫描:    

(1)       索引唯一扫描(index unique scan)

(2)       索引范围扫描(index range scan)

(3)       索引全扫描(index full scan)

(4)       索引快速扫描(index fast full scan)

5     索引跳跃扫描(INDEX SKIP SCAN

 

 

一. 索引唯一扫描(index unique scan)

通过唯一索引查找一个数值经常返回单个ROWID。如果该唯一索引有多个列组成(即组合索引),则至少要有组合索引的引导列参与到该查询中,如创建一个索引:create index idx_test on emp(ename, deptno, loc)。则select ename from emp where ename = ‘JACK’ and deptno = ‘DEV’语句可以使用该索引。如果该语句只返回一行,则存取方法称为索引唯一扫描。而select ename from emp where deptno = ‘DEV’语句则不会使用该索引,因为where子句种没有引导列。如果存在UNIQUE PRIMARY KEY 约束(它保证了语句只存取单行)的话,Oracle经常实现唯一性扫描。

 

如:

SQL> set autot traceonly exp;   -- 只显示执行计划

SQL> select * from scott.emp t where t.empno=10;

执行计划

----------------------------------------------------------

Plan hash value: 2949544139

--------------------------------------------------------------------------------

| Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time

--------------------------------------------------------------------------------

|   0 | SELECT STATEMENT            |        |     1 |    38 |     1   (0)| 00:0

|   1 |  TABLE ACCESS BY INDEX ROWID| EMP    |     1 |    38 |     1   (0)| 00:0

|*  2 |   INDEX UNIQUE SCAN         | PK_EMP |     1 |       |     0   (0)| 00:0

--------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   2 - access("T"."EMPNO"=10)

 

 

二.索引范围扫描(index range scan)

使用一个索引存取多行数据,同上面一样,如果索引是组合索引,而且select ename from emp where ename = ‘JACK’ and deptno = ‘DEV’语句返回多行数据,虽然该语句还是使用该组合索引进行查询,可此时的存取方法称为索引范围扫描

在唯一索引上使用索引范围扫描的典型情况下是在谓词(where限制条件)中使用了范围操作符(><<>>=<=between)

 

使用索引范围扫描的例子:

 

SQL> select empno,ename from scott.emp  where empno > 7876 order by empno;

执行计划

----------------------------------------------------------

Plan hash value: 169057108

--------------------------------------------------------------------------------

| Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time

--------------------------------------------------------------------------------

|   0 | SELECT STATEMENT            |        |     1 |    10 |     2   (0)| 00:0

|   1 |  TABLE ACCESS BY INDEX ROWID| EMP    |     1 |    10 |     2   (0)| 00:0

|*  2 |   INDEX RANGE SCAN          | PK_EMP |     1 |       |     1   (0)| 00:0

--------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   2 - access("EMPNO">7876)

 

在非唯一索引上,谓词可能返回多行数据,所以在非唯一索引上都使用索引范围扫描。

 

使用index rang scan3种情况:

(a) 在唯一索引列上使用了range操作符(> < <> >= <= between)
(b)
在组合索引上,只使用部分列进行查询,导致查询出多行。
(c)
对非唯一索引列上进行的任何查询。

 

 

三.索引全扫描(index full scan)

与全表扫描对应,也有相应的全Oracle索引扫描。在某些情况下,可能进行全Oracle索引扫描而不是范围扫描,需要注意的是Oracle索引扫描只在CBO模式下才有效 CBO根据统计数值得知进行全Oracle索引扫描比进行全表扫描更有效时,才进行全Oracle索引扫描,而且此时查询出的数据都必须从索引中可以直接得到。

 

Oracle索引扫描的例子:

 

SQL> create index big_emp on scott.emp(empno,ename);

索引已创建。

SQL> select empno, ename from scott.emp order by empno,ename;

执行计划

----------------------------------------------------------

Plan hash value: 322359667

----------------------------------------------------------------------------

| Id  | Operation        | Name    | Rows  | Bytes | Cost (%CPU)| Time     |

----------------------------------------------------------------------------

|   0 | SELECT STATEMENT |         |    14 |   140 |     1   (0)| 00:00:01 |

|   1 |  INDEX FULL SCAN | BIG_EMP |    14 |   140 |     1   (0)| 00:00:01 |

----------------------------------------------------------------------------

 

 

四. 索引快速扫描(index fast full scan)

扫描索引中的所有的数据块,与 index full scan很类似,但是一个显著的区别就是它不对查询出的数据进行排序,即数据不是以排序顺序被返回。在这种存取方法中,可以使用多块读功能,也可以使用并行读入,以便获得最大吞吐量与缩短执行时间。

 

索引快速扫描的例子:

SQL> select /*+ index_ffs(dave index_dave) */ id from dave where id>0;

执行计划

----------------------------------------------------------

Plan hash value: 674200218

--------------------------------------------------------------------------------

| Id  | Operation            | Name       | Rows  | Bytes | Cost (%CPU)| Time

--------------------------------------------------------------------------------

|   0 | SELECT STATEMENT     |            |     8 |    24 |     2   (0)| 00:00:0

|*  1 |  INDEX FAST FULL SCAN| INDEX_DAVE |     8 |    24 |     2   (0)| 00:00:0

--------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   1 - filter("ID">0)

 

为了实现这个效果,折腾了半天,最终还是用hint来了 

  • 1
  • 2
  • 下一页

相关内容