oracle中的B-TREE索引

在字段值情况不同的条件下测试B-TREE索引效率

清空共享池和数据缓冲区
alter system flush shared_pool;
alter system flush buffer_cache;

创建测试表
create table ann_t1 as select object_id,object_name from dba_objects;
create table ann_t2 as select mode(object_id,2) objectid,object_name from dba_objects;

创建索引,默认为B树索引
create index idx_t1_objectid on ann_t1(object_id);
create index idx_t2_objectid on ann_t2(objectid);

收集统计信息
execute dbms_stats.gather_table_stats(ownname=>'bolan',tabname=>'ANN_T1',method_opt=>'for all indexed columns size 2',cascade=>TRUE);
execute dbms_stats.gather_table_stats(ownname=>'bolan',tabname=>'ANN_T2',method_opt=>'for all indexed columns size 2',cascade=>TRUE);

打开语句跟踪
set autotrace traceonly;

查询1:
select * from ann_t1 where object_id=1;
/*********************************************************************************************************************************/
no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 1206843447

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

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

2 - access("OBJECT_ID"=1)


Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
410 bytes sent via SQL*Net to client
513 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
/*********************************************************************************************************************************/

查询2:
select * from ann_t2 where objectid=1;
/*********************************************************************************************************************************/

38408 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 350754170

----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 38180 | 969K| 98 (2)| 00:00:02 |
|* 1 | TABLE ACCESS FULL| ANN_T2 | 38180 | 969K| 98 (2)| 00:00:02 |
----------------------------------------------------------------------------

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

1 - filter("OBJECTID"=1)


Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
2894 consistent gets
0 physical reads
0 redo size
1591370 bytes sent via SQL*Net to client
28684 bytes received via SQL*Net from client
2562 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
38408 rows processed

/*********************************************************************************************************************************/

发现ann_t2使用了全表扫描(因为CBO的原因),强制索引扫描
查询3:
select /*+index(ann_t2 idx_t2_objectid) */ * from ann_t2 where objectid=1;

/*********************************************************************************************************************************/
38408 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 3426877313

-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 38180 | 969K| 412 (1)| 00:00:05 |
| 1 | TABLE ACCESS BY INDEX ROWID| ANN_T2 | 38180 | 969K| 412 (1)| 00:00:05 |
|* 2 | INDEX RANGE SCAN | IDX_T2_OBJECTID | 38180 | | 73 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------

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

2 - access("OBJECTID"=1)


Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
5513 consistent gets
0 physical reads
0 redo size
1716248 bytes sent via SQL*Net to client
28684 bytes received via SQL*Net from client
2562 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
38408 rows processed
/*********************************************************************************************************************************/

结论:分析同样使用索引的跟踪结果,cost和consistent gets在“查询3”中的值均比“查询1”高,说明在“唯一值多”时选择B-tree索引可以提高查询效率;

补充:
1.因查询的列键值均存在于索引中,故均不产生physical reads;
2.之所以B-tree适用于“唯一值多”情况,是因为表数据量大致相同的情况下,存在重复值的列经过条件过滤后结果集较大,数据可能不完全分布在一个数据块上,故产生consistent gets较高;
3.列上定义了索引,查询未必走索引,因cbo的缘故,可能存在全表扫描比索引扫描效率高的情况,优化器自动选择了全表扫描;
4.Consistent Gets(数据请求总数在回滚段Buffer中的数据一致性读所需要的数据块) 这里的概念是在处理你这个操作的时候需要在一致性读状态上处理多少个块,这些块产生的主要原因是因为由于在你查询的过程中,由于其他会话对数据块进行操作,而对所要查询的块有了修改,但是由于我们的查询是在这些修改之前调用的,所以需要对回滚段中的数据块的前映像进行查询,以保证数据的一致性。这样就产生了一致性读。

5.借用一个图理解下B-tree索引的工作原理:

6.oracle的索引类型:B树索引,位图索引,HASH索引,基于函数的索引,反转键索引,分区索引等;

转载于:https://www.cnblogs.com/assassinann/p/B_TREE%e7%b4%a2%e5%bc%95.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值