zoukankan      html  css  js  c++  java
  • Oracle索引扫描

    Oracle索引扫描:先通过index查找到索引的值,并根据索引的值对应的rowid值(对于非唯一索引可能返回多个rowid值)直接从表中得到具体的数据。一个rowid唯一的表示一行数据,该行对应的数据块是通过一次i/o得到的,在此情况下该次i/o只会读取一个数据库块。

    在索引中,除了存储每个索引的值外,索引还存储具有此值的行对应的ROWID值。
    Oracle索引扫描可以由3步组成:
    (1) 扫描索引得到满足条件的索引值。
    (2) 根据索引值得到对应的rowid。
    (3) 通过找到的rowid从表中读出具体的数据。

    每步都是单独的一次I/O,但是对于索引,由于经常使用,绝大多数都已经CACHE到内存中,所以第1、2步的I /O经常是逻辑I/O,即数据可以从内存中得到。但是对于第3步来说,如果表比较大,则其数据不可能全在内存中,所以其I/O很有可能是物理I/O。相对逻辑I/O来说,是极其费时间的。所以如果大表进行索引扫描,取出的数据如果大于总量的5% -- 10%,使用索引扫描会效率下降很多。

     

    SQL> explain plan for select deptno,dname from dept where deptno=10; 
    已解释。
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 2852011669

    ---------------------------------------------------------------------------------------
    | Id  | Operation                   | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
    ---------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT            |         |     1 |    13 |     1   (0)| 00:00:01 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| DEPT    |     1 |    13 |     1   (0)| 00:00:01 |
    |*  2 |   INDEX UNIQUE SCAN         | PK_DEPT |     1 |       |     0   (0)| 00:00:01 |
    ---------------------------------------------------------------------------------------
    Predicate Information (identified by operation id):
    PLAN_TABLE_OUTPUT
      2 - access("DEPTNO"=10)
    已选择14行。

    分析:此例中先通过INDEX UNIQUE SCAN(index扫描),找到deptno=10对应的rowid;再根据rowid通过TABLE ACCESS BY INDEX ROWID找到depnot=10的数据。(通过1次i/o得到数据。)

     

    SQL> explain plan for select deptno from dept where deptno=10;  //只查找deptno,而deptno上有索引,所以直接得到
    已解释。
    SQL> select * from table(dbms_xplan.display);

    PLAN_TABLE_OUTPUT

    Plan hash value: 3202655801

    -----------------------------------------------------------------------------
    | Id  | Operation         | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
    -----------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |         |     1 |     3 |     0   (0)| 00:00:01 |
    |*  1 |  INDEX UNIQUE SCAN| PK_DEPT |     1 |     3 |     0   (0)| 00:00:01 |
    -----------------------------------------------------------------------------
    Predicate Information (identified by operation id):
    PLAN_TABLE_OUTPUT
     1 - access("DEPTNO"=10)
    已选择13行。

    分析:此例中因为index里已经存有了deptno,所以直接得到deptno的值。(通过0次i/o得到数据。)

     

    SQL> explain plan for select deptno,dname from dept where deptno>20;
    已解释。
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 2985873453
    ---------------------------------------------------------------------------------------

    | Id  | Operation                   | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
    ---------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT            |         |     3 |    39 |     2   (0)| 00:00:01 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| DEPT    |     3 |    39 |     2   (0)| 00:00:01 |
    |*  2 |   INDEX RANGE SCAN          | PK_DEPT |     3 |       |     1   (0)| 00:00:01 |
    ---------------------------------------------------------------------------------------
    Predicate Information (identified by operation id):
    PLAN_TABLE_OUTPUT
      2 - access("DEPTNO">20)
    已选择14行。

    SQL> select * from dept;
    DEPTNO DNAME          LOC
    ------ -------------- --------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

    分析:此例中因为索引是已经排序了的,所以将按照索引的顺序通过INDEX RANGE SCAN(index范围扫描)找到满足条件的2条索引值deptno=30,deptno=40。根据它们对应的rowid找到相应的数据。(通过2次i/o得到所有数据。)

     

  • 相关阅读:
    sqlmap使用和X-Forwarded-For注入漏洞测试
    Spring 配置文件中将common.properties文件外置
    git rebase 和 reset的区别
    fatal: Not a valid object name: 'master'.
    Git error on commit after merge
    git 常用命令使用
    git常用命令学习
    关于idea 修改jsp文件后不能生效
    mysql 表被锁处理方案
    Redis 基本操作
  • 原文地址:https://www.cnblogs.com/toughhou/p/3778751.html
Copyright © 2011-2022 走看看