【索引】索引五种扫描方式至索引唯一扫描

索引唯一扫描,无疑是索引访问里最快的方式,利用至少的块,即可定位到叶子节点,提取到row后,直接回表读取行记录。

SQL> select empno,ename from emp where empno=7369 or empno=7844;


     EMPNO ENAME
---------- ----------
      7369 SMITH
      7844 TURNER

执行计划
----------------------------------------------------------
Plan hash value: 2355049923

---------------------------------------------------------------------------------------
| Id  | Operation                    | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |        |     2 |    40 |     2   (0)| 00:00:01 |
|   1 |  INLIST ITERATOR             |        |       |       |            |          |
|   2 |   TABLE ACCESS BY INDEX ROWID| EMP    |     2 |    40 |     2   (0)| 00:00:01 |
|*  3 |    INDEX UNIQUE SCAN         | PK_EMP |     1 |       |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("EMPNO"=7369 OR "EMPNO"=7844)

Note
-----
   - dynamic sampling used for this statement (level=2)

统计信息
----------------------------------------------------------
          5  recursive calls
          0  db block gets
         12  consistent gets
          1  physical reads
          0  redo size
        532  bytes sent via SQL*Net to client
        416  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          2  rows processed

SQL> select empno,ename from emp where empno=7369;

     EMPNO ENAME
---------- ----------
      7369 SMITH

执行计划
----------------------------------------------------------

Plan hash value: 2949544139


-------------------------------------------------------------------------------------
| Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |        |     1 |    20 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| EMP    |     1 |    20 |     2   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | PK_EMP |     1 |       |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("EMPNO"=7369)

统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
          2  consistent gets
          0  physical reads
          0  redo size
        486  bytes sent via SQL*Net to client
        416  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

SQL>

索引唯一扫描发生在where的条件谓词为主键或者唯一键列值时,并且谓词上的判断条件为等于符号“=”。

SQL> select empno,ename from emp where empno <=7369;


     EMPNO ENAME
---------- ----------
      7369 SMITH




执行计划
----------------------------------------------------------
Plan hash value: 3956160932


--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |    20 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| EMP  |     1 |    20 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------


Predicate Information (identified by operation id):
---------------------------------------------------


   1 - filter("EMPNO"<=7369)


Note
-----
   - dynamic sampling used for this statement (level=2)




统计信息
----------------------------------------------------------
          9  recursive calls
          0  db block gets
         18  consistent gets
          1  physical reads
          0  redo size
        486  bytes sent via SQL*Net to client
        416  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed


SQL>

这里不应该是全表扫描,并且也提示语句是动态采样,所以收集一次统计信息再看。

做一次统计信息收集:

SQL> exec dbms_stats.gather_table_stats(ownname => 'SCOTT',tabname => 'EMP',cascade => true);


PL/SQL 过程已成功完成。


SQL>

SQL> select empno,ename from emp where empno <=7369;


     EMPNO ENAME
---------- ----------
      7369 SMITH




执行计划
----------------------------------------------------------
Plan hash value: 169057108


--------------------------------------------------------------------------------------
| Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |        |     1 |    10 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| EMP    |     1 |    10 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | PK_EMP |     1 |       |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------


Predicate Information (identified by operation id):
---------------------------------------------------


   2 - access("EMPNO"<=7369)




统计信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          8  consistent gets
          0  physical reads
          0  redo size
        486  bytes sent via SQL*Net to client
        416  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed


SQL>

更新统计信息后,<=变成索引范围扫描。

你可能感兴趣的:(Oracle数据库-管理)