设为首页 收藏本站
查看: 572|回复: 0

[经验分享] 《Pro Oracle SQL》Chapter3--3.2.6 Index Skip Scan

[复制链接]
YunVN网友  发表于 2016-8-16 07:24:26 |阅读模式
  Index Skip Scan    索引跳跃扫描    (page 107)
    An index skip scan is chosen when the predicate contains a condition on a non-leading column in an
index and the leading columns are fairly distinct.
  In earlier releases of Oracle, if a predicate used a
column that wasn’t the leading column in an index, the index couldn’t be chosen.  This behavior
changed in Oracle version 9 with the introduction of the index skip scan .  A skip scan works by logically
splitting a multi-column index into smaller subindexes.  The number of logical subindexes is
determined by the number of distinct values in the leading columns of the index.  Therefore, the more
distinct the leading columns are, the more logical subindexes would need to be created.
  If too many
subindexes would be required, the operation won’t be as efficient as simply doing a full scan.  
However, in the cases where the number of subindexes needed would be smaller, the operation can be
many times more efficient than a full scan as scanning smaller index blocks can be more efficient
than scanning larger table blocks. Listing 3-15 shows an example of an index skip scan plan (Note: For
this example, I used a copy of the hr.employees table which had nearly 28,000 rows).
   当谓词包含的条件(作用)在索引的非前置列上而前置列却十分明显时,索引跳跃扫描将被选择。在Oracle的早期版本中,如果谓词使用了索引的非前置列,索引就不会被选择。该行为随着Oracle 9引入了索引跳跃扫描而改变了。跳跃扫描原理是,把多列索引逻辑划分成小的子索引。逻辑子索引的数量由索引前置列的明显值的个数确定。因此,前置列越明显,就会创建越多的逻辑子索引。如果需要创建的逻辑子索引太多,操作的效率可能还比不上直接做全扫描。然而,如果所需的子索引的数量较小,操作的效率将数倍于全扫描,因为扫描较小(范围)的索引块比扫描较大表块更有效率。列表3-15展示了一个索引跳跃扫描的例子(注意:本例中,我使用了hr.employees表的一备份表有将近28,000行)。
Listing 3-15. Index Skip Scan Examples
SQL> create index emp_jobfname_ix on employees(job_id, first_name, salary);   
 --这里job_id就是前置列
Index created.
 
SQL> set autotrace traceonly 
SQL>
SQL> select * from employees where first_name = 'William';
 
Execution Plan
----------------------------------------------------------
Plan hash value: 3440948136
 
------------------------------------------------------------------------------------
| Id  | Operation                                        | Name                        | Rows  | Bytes | Cost (%CPU)|
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                      |                                  |     1 |    82 |    21   (0)|
|   1 |  TABLE ACCESS BY INDEX ROWID| EMPLOYEES             |     1 |    82 |    21   (0)|
|*  2 |   INDEX SKIP SCAN                        | EMP_JOBFNAME_IX |     1 |         |    20   (0)|
------------------------------------------------------------------------------------
 Predicate Information (identified by operation id):
---------------------------------------------------
 
   2 - access("FIRST_NAME"='William')
       filter("FIRST_NAME"='William')
 
Statistics
-------------------------------------------------------
          0  recursive calls
          0  db block gets
         50  consistent gets
          0  physical reads
          0  redo size
       2362  bytes sent via SQL*Net to client
        392  bytes received via SQL*Net from client
          3  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         23  rows processed
 
SQL> select /*+ full(employees) */* from employees where first_name = 'William';
 
23 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1445457117
 
-------------------------------------------------------------------------------
| Id  | Operation                       | Name            | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |                      |     1 |    82 |    84   (2)| 00:00:02 |
|*  1 |  TABLE ACCESS FULL  | EMPLOYEES     |     1 |    82 |    84   (2)| 00:00:02 |
-------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   1 - filter("FIRST_NAME"='William')
Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
        376  consistent gets
          0  physical reads
          0  redo size
       2735  bytes sent via SQL*Net to client
        392  bytes received via SQL*Net from client
          3  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         23  rows processed
 
SQL> -- How many distinct values of job_id?
SQL> select count(distinct job_id) ct from employees ;
 
             CT
---------------
             19
    In this example, the leading column of the index,  job_id, has 19 distinct values.  Using an index
skip scan to access the 23 rows that match the condition (first_name = ‘William’), there are 50
consistent gets (logical block accesses).  However, if a full table scan is used, 376 blocks are accessed. 
As you can see, the skip scan is much more efficient.  What happened was that the index was logically
divided into 19 subindexes and each subindex was scanned for a match for first_name = ‘William’. 
For this index scan type, just keep in mind that the fewer distinct values the leading column (or
columns) have, the fewer logical subindexes will be needed and therefore the fewer total block
accesses required. 
 
   在例中,索引的前导列,job_id,有19个明显值。使用索引跳跃扫描访问匹配条件(first_name='William')的23行(记录),用了50个一致读(逻辑块访问)。然而,如果使用全表扫描,就要访问376个块。如你所见,跳跃扫描有效率多了。索引逻辑上划分成19个子索引且扫描每个子索引匹配first_name='William'。对于这种扫描类型,只要记住前置列(或列集)的明显值越少,所需的逻辑子索引越少,则因此所要访问的总块数越少。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-258451-1-1.html 上篇帖子: oracle导入表失败,很诡异,找到原因 下篇帖子: 使用Oracle SQL Developer 编写运行 PL/SQL 块(转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表