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

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

  在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要足够小。小到什么程度呢?

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

[@more@]具体见:http://database.ctocio.com.cn/260/8985760.shtml

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/789833/viewspace-1039983/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/789833/viewspace-1039983/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值