WEB开发网
开发学院数据库Oracle 跳跃式索引(Skip Scan Index)浅析 阅读

跳跃式索引(Skip Scan Index)浅析

 2007-06-02 16:03:42 来源:WEB开发网   
核心提示:在Oracle9i中,有一个新的特性:跳跃式索引(Skip Scan Index),跳跃式索引(Skip Scan Index)浅析,当表有一个复合索引,而在查询中有除了索引中第一列的其他列作为条件,每次对一个子索引进行扫描,因此SS的索引扫描成本为a.num_distinct.下面做一些试验,看看在什么情况下Orac

在Oracle9i中,有一个新的特性:跳跃式索引(Skip Scan Index)。当表有一个复合索引,而在查询中有除了索引中第一列的其他列作为条件,并且优化器模式为CBO,这时候查询计划就有可能使用到SS。此外,还可以通过使用提示index_ss(CBO下)来强制使用SS。

举例:

SQL> create table test1 (a number, b char(10), c varchar2(10));

Table created.

SQL> create index test_idx1 on test1(a, b);

Index created.

SQL> set autotrace on

SQL> select /*+index_ss(test1 test_idx1)*/* from test1 a

2 where b ='a';

no rows selected

Execution Plan

0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=32)

1 0 TABLE ACCESS (BY INDEX ROWID) OF 'TEST1' (Cost=2 Card=1 Bytes=32)

2 1 INDEX (SKIP SCAN) OF 'TEST_IDX1' (NON-UNIQUE)
但并不是任何情况下都会使用到SS。在Oracle的官方文档中,除了提到需要CBO,并且对表进行过分析外,还需要保证第一列的distinct value非常小。这一段是从官方文档上摘取的关于SS的一段解释:

Index skip scans improve index scans by nonprefix columns since it is often faster to scan index blocks than scanning table data blocks.

In this case a composite index is split logically into smaller subindexes. The number of logical subindexes depends on the cardinality of the initial column. Hence it is now possible to use the index even if the leading column is not used in a where clause.

Oracle并没有公布过关于SS更多的内部技术细节。但注意上面的这句话:In this case a composite index is split logically into smaller subindexes. The number of logical subindexes depends on the cardinality of the initial column.即Oralce会对复合索引进行逻辑划分,分成多个子索引。可以这样理解,Oracle将索引从逻辑上划分为a.num_distinct个子索引,每次对一个子索引进行扫描。因此SS的索引扫描成本为a.num_distinct.

下面做一些试验,看看在什么情况下Oracle采用SS.

首先要保证使用SS的几个必要条件:

· Optimizer为CBO

· 相关表要有正确的统计数据

· Oracle DB版本为9i以上

下面就是一个使用到SS的特殊条件:第一列的distinct num要足够小。小到什么程度呢?

还是以上面的表为例(省略中间的麻烦步骤,取两个临界值做实验):

取第一列distinct number为37:

SQL> truncate table test1;

Table truncated.

SQL> begin

2 for i in 1..100000 loop

3 insert into test1 values (mod(i,37), to_char(i), to_char(i));

4 end loop;p;

5 commit;

6 end;

7 /

PL/SQL procedure successfully completed.

SQL> analyze table test1 compute statistics;

Table analyzed.

SQL> set autotrace on explain

SQL> select * from test1

2 where b = '500';

A B C

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

19 500 500

Execution Plan

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

0 SELECT STATEMENT Optimizer=CHOOSE (Cost=37 Card=1 Bytes=17)

1 0 TABLE ACCESS (FULL) OF 'TEST1' (Cost=37 Card=1 Bytes=17)

1 2 3  下一页

Tags:跳跃式 索引 Skip

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接