以前在接触索引的时候,就想过要是表字段太少,索引效果不是很不好吗,直接用索引不是更直接吗?后来因为懒惰也没有去查找相关资料。正好今天看到了table organization index,看了一下,实现的功能就是这个意思,这里分享给大家。
其实正常项目中,90%以上都是正常的heap表。但我们也不能忽略那些用的少,但给我们性能带来巨大优化的其他表类型。
首先,创建两张表:
create table tindex
(myid number primary key,
myname varchar2(20))
organization index;
create table tindex1
(myid number primary key,
myname varchar2(20)) ;
分别插入数据
insert into tindex values(111,'abcd');
insert into tindex1 values(111,'abcd');
1、我们来看没有where解释计划:
select * from tindex;
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|Time |
------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 25 | 2 (0)|00:00:01 |
| 1 | INDEX FAST FULL SCAN| SYS_IOT_TOP_92809 | 1 | 25 | 2 (0)|00:00:01 |
------------------------------------------------------------------------------------------
Note
------ dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
491 bytes sent via SQL*Net to client
420 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
select * from tindex1;
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 25 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| TINDEX1 | 1 | 25 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Note
------ dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
7 consistent gets
0 physical reads
0 redo size
495 bytes sent via SQL*Net to client
419 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
可以看到consistent gets相差2,通过解释计划,一个为INDEX FAST FULL SCAN,另一个是INDEX FAST FULL SCAN,相信我们也可以看出其中的原因。因为一个是按照物理顺序来读取。
2、在我们加上条件where后,效果更加明显:
select * from tindex where myid=111;
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Tim
e |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 25 | 1 (0)| 00:
00:01 |
|* 1 | INDEX UNIQUE SCAN| SYS_IOT_TOP_92809 | 1 | 25 | 1 (0)| 00:
00:01 |
select * from tindex1 where myid=111;
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)
| Time |
--------------------------------------------------------------------------------
------------
| 0 | SELECT STATEMENT | | 1 | 25 | 1 (0)
| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TINDEX1 | 1 | 25 | 1 (0)
| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | SYS_C0015919 | 1 | | 1 (0)
| 00:00:01 |
其中一个consistent gets为1,另一个为2,原因从解释计划的执行方式可以看出,一个需要返回给heap表,另一个则不需要。