题记:今天是2014-01-13,在春节之前打算把oracle索引技术深入研究一下,在此记录一下学习笔记。今天我学习的内容是B树索引知识。
B树索引深入总结。
首先看一下网络上一个B树索引图,如下:

1、oracle如何使用B树索引?
B树索引有根节点块,分支节点块,叶子节点块组成。一般情况下B树索引是oracle默认创建索引使用的类型。使用B树索引可以提高数据的检索速度,可以强制执行主键和唯一键的约束性,对于减少通过主键和外键约束关联的父表和字表锁的问题。
当检索数据列位于索引指定列的时候,只需要访问索引块即可完成数据的展现,这非常提高数据的检索速率;
当检索数据列不完全位于索引指定列的时候,将会访问索引块以及数据块进而将数据进行展现;
最后一种是不适用索引,而是通过检索表块完成数据的检索;
下面分别对这三种情况进行试验说明:
NO.1:对于该情况所有字段都位于索引 段中,一般设计到index range scan表示要扫B树索引的叶子子节点一段数据;另一个是index fast full scan,表示要扫描所有索引的叶子子节点数据,一般用在count中如下:
SQL> create table emp(
2 empno number(4),
3 ename varchar2(10),
4 job varchar2(9),
5 mgr number(4),
6 hiredate date,
7 sal number(7,2),
8 comm number(7,2),
9 deptno number(2)
10 );
Table created.
SQL> create index emp_pk on emp(empno);
Index created.
SQL> insert into emp select * from scott.emp;
14 rows created.
SQL> insert into emp select * from scott.emp;
14 rows created.
SQL> set autotrace trace
SQL> select empno from emp where empno=7369;
Execution Plan
----------------------------------------------------------
Plan hash value: 745440807
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 26 | 1 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| EMP_PK | 2 | 26 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("EMPNO"=7369)
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
22 recursive calls
0 db block gets
36 consistent gets
0 physical reads
0 redo size
579 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
3 sorts (memory)
0 sorts (disk)
2 rows processed
SQL>
有个问题,在这个操作过程中,对应一下上图,该过程会扫描几个数据块?嘿嘿,可以思考一下。
以上图为例,每个大方框都代表一个数据块,那么如果要获得0号数据信息,那么就会读0B1数据块、B1数据块、L1数据块,如果为了确保L1数据都要验证是否满足需求因此需要读整个L1块的数据,这就是index range scan;
注意:
db block gets=db block gets from cache+db block gets direct
consistent gets=consistent gets from cache+consistent get direct
physical reads=physical reads cache+physical
在看一下index fast full scan:
SQL> select count(empno) from emp;
Execution Plan
----------------------------------------------------------
Plan hash value: 3381701820
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 1 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
| 2 | INDEX FULL SCAN| EMP_PK | 28 | 364 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
18 recursive calls
0 db block gets
34 consistent gets
0 physical reads
0 redo size
530 bytes sent via SQL*Net to cli