关于Oracle9i跳跃式索引扫描(IndexSkipScan)的小测试

发表于:2007-05-25来源:作者:点击数: 标签:
在 Oracle 9i中我们知道能够使用跳跃式索引扫描(Index Skip Scan).然而,能利用跳跃式索引扫描的情况其实是有些限制的. 从Oracle的文档中我们可以找到这样的话: Index Skip Scans Index skip scans improve index scans by nonprefix columns. Often, scannin


Oracle9i中我们知道能够使用跳跃式索引扫描(Index Skip Scan).然而,能利用跳跃式索引扫描的情况其实是有些限制的.

从Oracle的文档中我们可以找到这样的话:

Index Skip Scans
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.

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.

也可以这样说,优化器根据索引中的前导列(索引到的第一列)的唯一值的数量决定是否使用Skip Scan.

我们首先做个测试

SQL> CREATE TABLE test AS
  2  SELECT ROWNUM a,ROWNUM-1 b ,ROWNUM-2 c,ROWNUM-3 d,ROWNUM-4 e
  3  FROM all_objects
  4  /

SQL> SELECT DISTINCT COUNT (a) FROM test;

  COUNT(A)
----------
     28251

表已创建。

SQL>
SQL> CREATE INDEX test_idx ON test(a,b,c)
  2  /

索引已创建。

SQL> ANALYZE TABLE test COMPUTE STATISTICS
  2  FOR TABLE
  3  FOR ALL INDEXES
  4  FOR ALL INDEXED COLUMNS
  5  /

表已分析。

SQL> SET autotrace traceonly explain
SQL> SELECT *  FROM test WHERE b = 99
  2  /

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=36 Card=1 Bytes=26)
   1    0 TABLE ACCESS (FULL) OF 'TEST' (Cost=36 Card=1 Bytes=26)

--可见这里CBO选择了全表扫描.

--我们接着做另一个测试:

SQL> drop table test;

表已丢弃。

SQL> CREATE TABLE test
  2  AS
  3  SELECT DECODE(MOD(ROWNUM,2), 0, '1', '2' ) a,
  4                    ROWNUM-1 b,
  5                    ROWNUM-2 c,
  6                    ROWNUM-3 d,
  7                    ROWNUM-4 e
  8    FROM all_objects
  9  /

表已创建。

SQL> set autotrace off
SQL> select distinct a from test;

A
--
1
2

--A列只有两个唯一值

SQL> CREATE INDEX test_idx ON test(a,b,c)
  2  /

索引已创建。


SQL> ANALYZE TABLE test COMPUTE STATISTICS
  2  FOR TABLE
  3  FOR ALL INDEXES
  4  FOR ALL INDEXED COLUMNS
  5  /

表已分析。

SQL> set autotrace traceonly explain
SQL> SELECT *  FROM test WHERE b = 99
  2  /

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=4 Card=1 Bytes=24)
   1    0   TABLE ACCESS (BY INDEX ROWID) OF 'TEST' (Cost=4 Card=1 Bytes=24)
   2    1     INDEX (SKIP SCAN) OF 'TEST_IDX' (NON-UNIQUE) (Cost=3 Card=1)

 

Oracle的优化器(这里指的是CBO)能对查询应用Index Skip Scans至少要有几个条件:

1 优化器认为是合适的.
2 索引中的前导列的唯一值的数量能满足一定的条件.
3 优化器要知道前导列的值分布(通过分析/统计表得到)
4 合适的SQL语句
......


更多信息请参考:

http://www.itpub.net/showthread.php?threadid=85948

http://www.cnoug.org/bin/ut/topic_show.cgi?id=608&h=1&bpg=1&age=100

http://www.itpub.net/showthread.php?s=&postid=985602#post985602

Oracle9i Database Performance Tuning Guide and Reference Release 2 (9.2)
Part Number A96533-02

感谢参加讨论的各位高手.

原文转自:http://www.ltesting.net