之前在讨论CBO和RBO的时候提到了索引扫描的几种类型。

Oracle Optimizer CBO RBO

http://blog.csdn.net/tianlesoftware/archive/2010/08/19/5824886.aspx

Oracle 索引 详解

http://blog.csdn.net/tianlesoftware/archive/2010/03/05/5347098.aspx

Oracle Explain Plan

http://blog.csdn.net/tianlesoftware/archive/2010/08/20/5827245.aspx

根据索引的类型与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 scan的3种情况:

(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来了.

Oracle Hint

http://blog.csdn.net/tianlesoftware/archive/2010/03/05/5347098.aspx

五. 索引跳跃扫描(INDEX SKIP SCAN)

INDEX SKIP SCAN,发生在多个列建立的复合索引上,如果SQL中谓词条件只包含索引中的部分列,并且这些列不是建立索引时的第一列时,就可能发生INDEX SKIP SCAN。这里SKIP的意思是因为查询条件没有第一列或前面几列,被忽略了。

Oracle 10g的文档如下:

Index skip scans improve index scans by nonprefix columns. Often, scanning index blocks is faster than scanning table data blocks.

Skip scanning lets a composite index be split logically into smaller subindexes. In skip scanning, the initial column of the composite index is not specified in the query. In other words, it is skipped.

--skip scan 让组合索引(composite index)逻辑的split 成几个子索引。如果在在查询时,第一个列没有指定,就跳过它。

The number of logical subindexes is determined by the number of distinct values in the initial column. Skip scanning is advantageous if there are few distinct values in the leading column of the composite index and many distinct values in the nonleading key of the index.

-- 建议将distinct 值小的列作为组合索引的引导列,即第一列。

Example 13-5 Index Skip Scan

Consider, for example, a table employees (sex, employee_id, address) with a composite index on (sex, employee_id). Splitting this composite index would result in two logical subindexes, one for M and one for F.

For this example, suppose you have the following index data:

('F',98)

('F',100)

('F',102)

('F',104)

('M',101)

('M',103)

('M',105)

The index is split logically into the following two subindexes:

(1)The first subindex has the keys with the value F.

(2)The second subindex has the keys with the value M.

Figure 13-2 Index Skip Scan Illustration

The column sex is skipped in the following query:

SELECT *

FROM employees

WHERE employee_id = 101;

A complete scan of the index is not performed, but the subindex with the value F is searched first, followed by a search of the subindex with the value M.

测试:

创建表:

SQL> create table dave_test as select owner,object_id,object_type,created from dba_objects;

Table created.

创建组合索引

SQL> create index idx_dave_test_com on dave_test(owner,object_id,object_type);

Index created.

--收集表的统计信息

SQL> exec dbms_stats.gather_table_stats('SYS','DAVE_TEST');

PL/SQL procedure successfully completed.

SQL> set autot traceonly exp;

指定组合索引的所有字段时,使用Index range scan:

SQL> select * from dave_test where owner='SYS' and object_id=20 and object_type='TABLE';

Execution Plan

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

Plan hash value: 418973243

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

| Id  | Operation                   | Name              | Rows  | Bytes | Cost (

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

|   0 | SELECT STATEMENT            |                   |     1 |    27 |     2

|   1 |  TABLE ACCESS BY INDEX ROWID| DAVE_TEST         |     1 |    27 |     2

|*  2 |   INDEX RANGE SCAN          | IDX_DAVE_TEST_COM |     1 |       |     1

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

Predicate Information (identified by operation id):

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

2 - access("OWNER"='SYS' AND "OBJECT_ID"=20 AND "OBJECT_TYPE"='TABLE')

指定组合索引的2个字段时,使用的还是index range scan:

SQL> select * from dave_test where owner='SYS' and object_id=20;

Execution Plan

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

Plan hash value: 418973243

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

| Id  | Operation                   | Name              | Rows  | Bytes | Cost (

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

|   0 | SELECT STATEMENT            |                   |     1 |    27 |     3

|   1 |  TABLE ACCESS BY INDEX ROWID| DAVE_TEST         |     1 |    27 |     3

|*  2 |   INDEX RANGE SCAN          | IDX_DAVE_TEST_COM |     1 |       |     2

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

Predicate Information (identified by operation id):

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

2 - access("OWNER"='SYS' AND "OBJECT_ID"=20)

指定组合索引的引导列,即第一个列时,不走索引,走全表扫描

SQL> select * from dave_test where owner='SYS';

Execution Plan

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

Plan hash value: 1539627441

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

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

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

|   0 | SELECT STATEMENT  |           | 23567 |   621K|    52   (4)| 00:00:01 |

|*  1 |  TABLE ACCESS FULL| DAVE_TEST | 23567 |   621K|    52   (4)| 00:00:01 |

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

Predicate Information (identified by operation id):

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

1 - filter("OWNER"='SYS')

指定组合索引的非引导列,使用Index skip scan:

SQL> select * from dave_test where object_id=20 and object_type='TABLE';

Execution Plan

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

Plan hash value: 3446962311

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

| Id  | Operation                   | Name              | Rows  | Bytes | Cost (

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

|   0 | SELECT STATEMENT            |                   |     1 |    27 |    22

|   1 |  TABLE ACCESS BY INDEX ROWID| DAVE_TEST         |     1 |    27 |    22

|*  2 |   INDEX SKIP SCAN           | IDX_DAVE_TEST_COM |     1 |       |    21

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

Predicate Information (identified by operation id):

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

2 - access("OBJECT_ID"=20 AND "OBJECT_TYPE"='TABLE')

filter("OBJECT_ID"=20 AND "OBJECT_TYPE"='TABLE')

指定组合索引的最后一列,不走索引,走全表扫描

SQL> select * from dave_test where object_type='TABLE';

Execution Plan

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

Plan hash value: 1539627441

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

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

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

|   0 | SELECT STATEMENT  |           |  1774 | 47898 |    52   (4)| 00:00:01 |

|*  1 |  TABLE ACCESS FULL| DAVE_TEST |  1774 | 47898 |    52   (4)| 00:00:01 |

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

Predicate Information (identified by operation id):

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

1 - filter("OBJECT_TYPE"='TABLE')

指定组合索引的头尾2列,不走索引:

SQL> select * from dave_test where owner='SYS' and object_type='TABLE';

Execution Plan

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

Plan hash value: 1539627441

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

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

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

|   0 | SELECT STATEMENT  |           |   830 | 22410 |    52   (4)| 00:00:01 |

|*  1 |  TABLE ACCESS FULL| DAVE_TEST |   830 | 22410 |    52   (4)| 00:00:01 |

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

Predicate Information (identified by operation id):

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

1 - filter("OBJECT_TYPE"='TABLE' AND "OWNER"='SYS')

通过以上测试,和之前官网的说明,Index skip scan 仅是在组合索引的引导列,即第一列没有指定,并且非引导列指定的情况下。

联合索引选择性更高咯,所占空间应当是比单独索引要少,因为叶节点节省了重复的rowid,当然branch节点可能稍微多一点。

禁用skip scan:

alter system set “_optimizer_skip_scan_enabled” = false scope=spfile;

Oracle 索引扫描的五种类型的更多相关文章

  1. Oracle 索引扫描的4种类型

    根据索引的类型与where限制条件的不同,有4种类型的Oracle索引扫描: 3,4可归一种 (1) 索引唯一扫描(index uniquescan) (2) 索引范围扫描(index range s ...

  2. Oracle 索引扫描的几种情况

    index range scan(索引范围扫描): 1.对于unique index来说,如果where 条件后面出现了<,> ,between ...and...的时候,那么就可能执行i ...

  3. Oracle索引扫描

    Oracle索引扫描:先通过index查找到索引的值,并根据索引的值对应的rowid值(对于非唯一索引可能返回多个rowid值)直接从表中得到具体的数据.一个rowid唯一的表示一行数据,该行对应的数 ...

  4. Spring 切面可以应用五种类型的通知?

    Spring 切面可以应用五种类型的通知: before:前置通知,在一个方法执行前被调用. after: 在方法执行之后调用的通知,无论方法执行是否成功. after-returning: 仅当方法 ...

  5. Oracle索引扫描算法

    SQL> create table t as select * from dba_objects; Table created. SQL> create index idx_t on t( ...

  6. oracle 索引扫描类型的分类与构造

    1. INDEX RANGE SCAN--请记住这个INDEX RANGE SCAN扫描方式drop table t purge;create table t as select * from dba ...

  7. Python函数参数的五种类型

    之前项目需求,需要通过反射获取函数的参数,python中可以通过函数签名(signature)来实现. 首先需要了解函数参数的类型,Python的参数类型一共有5种:POSITIONAL_OR_KEY ...

  8. 六、Redis五种类型 - hash(散列)类型

    1.介绍 (1).hash也是一种字典结构,存储了字段(field)和字段值(value)的映射,字段值只能是字符串,不支持其他类型.(2).适合存储对象,对象列表和ID构成键名,字段表示对象的属性, ...

  9. 五、Redis五种类型 - 字符串类型

    1.介绍: 字符串类型是Redis中最基本的数据类型,可以存储任何形式的字符串数据,最大容量是512MB. key 和 value 都是区分大小写的. 2.命令介绍 (1).赋值: set key v ...

随机推荐

  1. springmvc4处理get和post请求中文乱码问题

    1.在springmvc4处理get和post请求的问题 参看大牛博客连接:https://blog.csdn.net/qq_41665356/article/details/80234392

  2. js 合并多个对象 Object.assign

    Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. var o1 = { a: 1 };var o2 = { b: 2 };var o3 ...

  3. SQL SERVER 常用公式

    SQL SERVER 获取当前月的天数 SELECT -DAY(getdate()+-DAY(getdate())) SQL server 除法计算百分比[整数乘1.0否则结果为0或1] CONVER ...

  4. rabbitmq之核心构架和原理总结(四)

    前言 前面博文已经将安装配置和站点管理介绍了,现在开始正式学习rabbitmq的使用了: rabbitMQ的构架 rabbitmq作为消息队列,一条消息从发布到订阅消费的完整流程为: 消息 --> ...

  5. 关于shutdown和close

    示例代码: void str_cli(FILE *fp, int sockfd) { pid_t pid; char sendline[MAXLINE], recvline[MAXLINE]; ) { ...

  6. java===java基础学习(1)---数据类型,运算,变量,常量

    今天起开始了java的学习之路,主要学习了数据类型和运算,变量,常量.基本和python有很多相通的地方,所以看起来很容易上手.下面是学习笔记! package testbotoo; public c ...

  7. 再思linux内核在中断路径内不能睡眠/调度的原因(2010)【转】

    转自:http://blog.csdn.net/maray/article/details/5770889 Linux内核中断路径中不能睡眠,为什么? 这里就行了很深入的讨论,值得一看:http:// ...

  8. mysql 5.6在gtid复制模式下复制错误,如何跳过??

    mysql 5.6在gtid复制模式下复制错误,如何跳过?? http://www.xuchanggang.cn/archives/918.html

  9. sicily 1153. 马的周游问题

    一.题目描述 在一个8 * 8的棋盘中的某个位置有一只马,如果它走29步正好经过除起点外的其他位置各一次,这样一种走法则称马的周游路线,试设计一个算法,从给定的起点出发,找出它的一条周游路线. 为了便 ...

  10. JAVA常见的集合类

    关系的介绍: Set(集):集合中的元素不按特定方式排序,并且没有重复对象.他的有些实现类能对集合中的对象按特定方式排序. List(列表):集合中的元素按索引位置排序,可以有重复对象,允许按照对象在 ...