`

转:oracle 索引跳跃式扫描

 
阅读更多

在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 ptimizer=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 ptimizer=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语句
......

 

如下是网上收集的其他关于跳跃式扫描的内容:

索引跳跃式扫描主要有两个优点:
1、以前版本中的表扫描(TABLE SCAN)可能会转变为索引扫描,提高了某些查询的执行效率;
2、应用程序使用较少的索引就能达到提高效能的目的,并且既节省存储空间,又能提高DML和维护操作的效率。


对于高顺序键(high order key)中的独特值数目,Oracle的索引跳跃式扫描性能将会降低。如果主列有50个值,Oracle要发出50条查询才能找回结果。
  
  索引跳跃式扫描只适用于硬盘空间和存储空间相当紧缺的情况。
-----------------------------------------------------------------------------------------------------------------

create index skip1 on emp5(job,empno);
Index created.
select  count(*)
from     emp5
where    empno = 7900;
Elapsed: 00:00:03.13 (Result is a single row…not displayed)
Execution Plan
0           SELECT STATEMENT ptimizer=CHOOSE (Cost=4 Card=1 Bytes=5)
1        0    SORT (AGGREGATE)
2        1      INDEX (FAST FULL SCAN) OF 'SKIP1' (NON-UNIQUE)
Statistics
6826    consistent gets
6819    physical reads
select /*+ index(emp5 skip1) */ count(*)
from     emp5
where    empno = 7900;
Elapsed: 00:00:00.56
Execution Plan
0      SELECT STATEMENT ptimizer=CHOOSE (Cost=6 Card=1 Bytes=5)
1    0   SORT (AGGREGATE)
2    1     INDEX (SKIP SCAN) OF 'SKIP1' (NON-UNIQUE)
Statistics
21   consistent gets
17   physical reads


如同该程序清单所示,第二个选项使用INDEX (SKIP SCAN)操作读取索引。该执行路径需要21个逻辑读,这些逻辑读又需要17个物理I/O操作。第一个选项执行INDEX (FAST FULL SCAN)操作,该操作需要更多数量的逻辑和物理I/O。

为了让优化器选择跳跃式扫描,可能需要在查询中使用提示,如同该程序清单所示。提示影响了优化器,使其偏向您所指定的执行路径。

 

技巧:

对于那些有组合索引的大型表而言,索引跳跃式扫描特性可以提供一个快速访问,即使索引的第一列没有在限制条件中使用

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics