索引唯一扫描

一 创建表和数据
create table zfk select * from dba_objects;
create table ZFK
(
object_name VARCHAR2(128),
object_id NUMBER not null,
data_object_id NUMBER,
) pctfree 10

二创建唯一索引
alter table ZFK add constraint ZFK_PK_OBJID primary key (OBJECT_ID) pctfree 10 using index ;
三检查信息
1 对象ID
select object_name,object_id,data_object_id from dba_objects where owner=user and object_name in (‘ZFK’,’ZFK_PK_OBJID’);

OBJECT_NAME OBJECT_ID DATA_OBJECT_ID
ZFK 119734 120052
ZFK_PK_OBJID 120051 120053

OBJECT_ID 十六进制
119734=>1D3B6
120051=>1D4F3
120052=>1D4F4
120053=>1D4F5

2 表和索引的块数量
select segment_name,segment_type,file_id,extent_id,block_id,blocks from dba_extents where segment_name in (‘ZFK’,’ZFK_PK_OBJID’);
SEGMENT_NAME SEGMENT_TYPE FILE_ID EXTENT_ID BLOCK_ID BLOCKS
ZFK_PK_OBJID INDEX 4 0 536 8
ZFK_PK_OBJID INDEX 4 1 544 8
ZFK_PK_OBJID INDEX 4 2 552 8
ZFK_PK_OBJID INDEX 4 3 560 8
ZFK_PK_OBJID INDEX 4 4 568 8
ZFK_PK_OBJID INDEX 4 5 576 8
ZFK_PK_OBJID INDEX 4 6 584 8
ZFK_PK_OBJID INDEX 4 7 592 8
ZFK_PK_OBJID INDEX 4 8 600 8
ZFK_PK_OBJID INDEX 4 9 608 8
ZFK_PK_OBJID INDEX 4 10 616 8
ZFK_PK_OBJID INDEX 4 11 624 8
ZFK_PK_OBJID INDEX 4 12 632 8
ZFK_PK_OBJID INDEX 4 13 640 8
ZFK_PK_OBJID INDEX 4 14 648 8
ZFK_PK_OBJID INDEX 4 15 656 8
ZFK_PK_OBJID INDEX 4 16 768 128
ZFK TABLE 12 0 328 8
ZFK TABLE 12 1 336 8
ZFK TABLE 12 2 344 8
ZFK TABLE 12 3 352 8
ZFK TABLE 12 4 360 8
ZFK TABLE 12 5 368 8
ZFK TABLE 12 6 376 8
ZFK TABLE 12 7 16640 8
ZFK TABLE 12 8 16648 8
ZFK TABLE 12 9 16656 8
ZFK TABLE 12 10 16664 8
ZFK TABLE 12 11 16672 8
ZFK TABLE 12 12 16680 8
ZFK TABLE 12 13 16688 8
ZFK TABLE 12 14 16696 8
ZFK TABLE 12 15 16704 8
ZFK TABLE 12 16 16768 128
ZFK TABLE 12 17 16896 128
ZFK TABLE 12 18 17024 128
ZFK TABLE 12 19 17152 128
ZFK TABLE 12 20 17280 128
ZFK TABLE 12 21 17408 128
ZFK TABLE 12 22 17536 128
ZFK TABLE 12 23 17664 128
ZFK TABLE 12 24 17792 128
ZFK TABLE 12 25 17920 128

3 分析索引
Analyze index ZFK_PK_OBJID validate structure;
select * from index_stats;
HEIGHT 2 <<== 两层高
BLOCKS 256 <<= 占256个块
NAME ZFK_PK_OBJID
LF_ROWS 87713 <<==叶块包含这么多键值记录
LF_BLKS 182 <<==叶块总数
LF_ROWS_LEN 1305009
LF_BLK_LEN 7996
BR_ROWS 181 <<==分支行 181
BR_BLKS 1 <<==分支块数 这里就是根节点它一个人了
BR_ROWS_LEN 1973
BR_BLK_LEN 8028
DEL_LF_ROWS 0
DEL_LF_ROWS_LEN 0
DISTINCT_KEYS 87713
MOST_REPEATED_KEY 1
BTREE_SPACE 1463300
USED_SPACE 1306982
PCT_USED 90
ROWS_PER_KEY 1
BLKS_GETS_PER_ACCESS 3
PRE_ROWS 0
PRE_ROWS_LEN 0
OPT_CMPR_COUNT 0
OPT_CMPR_PCTSAVE 0

三普通的B树索引结构图 逻辑上的
这里写图片描述
四 执行查询
SQL> select object_id from ZFK t where object_id in(9);
已用时间: 00: 00: 00.01

执行计划

Plan hash value: 434702176

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

| 0 | SELECT STATEMENT | | 1 | 5 | 1 (0)| 00:00:01 |

|* 1 | INDEX UNIQUE SCAN| ZFK_PK_OBJID | 1 | 5 | 1 (0)| 00:00:01 |

Predicate Information (identified by operation id):

1 - access(“OBJECT_ID”=9)

统计信息

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

四 调用跟踪语句执行下
SQL> oradebug setmypid;
已处理的语句
SQL> oradebug event 10200 trace name context forever,level 1;
已处理的语句
SQL> select object_id from DBA_MONITER.ZFK t where object_id in(9);

OBJECT_ID

 9

SQL> oradebug event 10200 trace name context off;
已处理的语句
SQL> oradebug tracefile_name;
/u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_128419.trc

跟踪文件核心部分
* 2017-02-14 18:04:34.731
Oradebug command ‘event 10200 trace name context forever,level 1’ console output:

* 2017-02-14 18:05:08.499
ktrgtc2(): started for block <0x0000 : 0x00400159> objd: 0x00000025
env [0x7f1e8555845c]: (scn: 0x0000.16a19f8b xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 statement num=0 parent xid: 0x0000.000.00000000 st-scn: 0x0000.00000000 hi-scn: 0x0000.00000000 ma-scn: 0x0000.16a19f2b flg: 0x00000660)
ktrexc(): returning 2 on: 0xc0f4928 cr-scn: 0xffff.ffffffff xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 cl-scn: 0xffff.ffffffff sfl: 0
ktrgtc2(): completed for block <0x0000 : 0x00400159> objd: 0x00000025
ktrgtc2(): started for block <0x0000 : 0x0040adf0> objd: 0x00000025
env [0x7f1e8555845c]: (scn: 0x0000.16a19f8b xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 statement num=0 parent xid: 0x0000.000.00000000 st-scn: 0x0000.00000000 hi-scn: 0x0000.00000000 ma-scn: 0x0000.16a19f2b flg: 0x00000660)
ktrexc(): returning 2 on: 0xc0f4928 cr-scn: 0xffff.ffffffff xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 cl-scn: 0xffff.ffffffff sfl: 0
ktrgtc2(): completed for block <0x0000 : 0x0040adf0> objd: 0x00000025
ktrget2(): started for block <0x0000 : 0x0040322b> objd: 0x00000025
env [0x7f1e8555845c]: (scn: 0x0000.16a19f8b xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 statement num=0 parent xid: 0x0000.000.00000000 st-scn: 0x0000.00000000 hi-scn: 0x0000.00000000 ma-scn: 0x0000.16a19f2b flg: 0x00000660)
ktrgcm(): completed for block <0x0000 : 0x0040322b> objd: 0x00000025
ktrget3(): completed for block <0x0000 : 0x0040322b> objd: 0x00000025
ktrgtc2(): started for block <0x0000 : 0x00400159> objd: 0x00000025
env [0x7f1e85559494]: (scn: 0x0000.16a19f8b xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 statement num=0 parent xid: 0x0000.000.00000000 st-scn: 0x0000.00000000 hi-scn: 0x0000.00000000 ma-scn: 0x0000.16a19f2b flg: 0x00000660)
ktrexc(): returning 2 on: 0xc0f4928 cr-scn: 0xffff.ffffffff xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 cl-scn: 0xffff.ffffffff sfl: 0
ktrgtc2(): completed for block <0x0000 : 0x00400159> objd: 0x00000025
ktrgtc2(): started for block <0x0000 : 0x0040e321> objd: 0x00000025
env [0x7f1e85559494]: (scn: 0x0000.16a19f8b xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 statement num=0 parent xid: 0x0000.000.00000000 st-scn: 0x0000.00000000 hi-scn: 0x0000.00000000 ma-scn: 0x0000.16a19f2b flg: 0x00000660)
ktrexc(): returning 2 on: 0xc0f4928 cr-scn: 0xffff.ffffffff xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 cl-scn: 0xffff.ffffffff sfl: 0
ktrgtc2(): completed for block <0x0000 : 0x0040e321> objd: 0x00000025
ktrget2(): started for block <0x0000 : 0x0040e35f> objd: 0x00000025
env [0x7f1e85559494]: (scn: 0x0000.16a19f8b xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 statement num=0 parent xid: 0x0000.000.00000000 st-scn: 0x0000.00000000 hi-scn: 0x0000.00000000 ma-scn: 0x0000.16a19f2b flg: 0x00000660)
ktrgcm(): completed for block <0x0000 : 0x0040e35f> objd: 0x00000025
ktrget3(): completed for block <0x0000 : 0x0040e35f> objd: 0x00000025

#

objd: 0x00000025 这个对象 一查就知道是 系统索引对象, 就是数据字典,

#

* 2017-02-14 18:05:20.014
ktrgtc2(): started for block <0x0004 : 0x0100021b> objd: 0x0001d4f5
env [0x7f1e8555a4cc]: (scn: 0x0000.16a19f97 xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 statement num=0 parent xid: 0x0000.000.00000000 st-scn: 0x0000.00000000 hi-scn: 0x0000.00000000 ma-scn: 0x0000.16a19f2b flg: 0x00000660)
ktrexc(): returning 2 on: 0xc0f4928 cr-scn: 0xffff.ffffffff xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 cl-scn: 0xffff.ffffffff sfl: 0
ktrgtc2(): completed for block <0x0004 : 0x0100021b> objd: 0x0001d4f5

ktrgtc2(): started for block <0x0004 : 0x0100021c> objd: 0x0001d4f5
env [0x7f1e8555a4cc]: (scn: 0x0000.16a19f97 xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 statement num=0 parent xid: 0x0000.000.00000000 st-scn: 0x0000.00000000 hi-scn: 0x0000.00000000 ma-scn: 0x0000.16a19f2b flg: 0x00000660)
ktrexc(): returning 2 on: 0xc0f4928 cr-scn: 0xffff.ffffffff xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 cl-scn: 0xffff.ffffffff sfl: 0
ktrgtc2(): completed for block <0x0004 : 0x0100021c> objd: 0x0001d4f5

* 2017-02-14 18:05:46.829
Processing Oradebug command ‘event 10200 trace name context off’

解释下
started for block 表示开始读取块
<0x0004 : 0x0100021c> 表示的块地址 前面的表示FILE_ID,后面要去掉前面的0X01后 21C转换成十进制是 540. 是我们索引第一行 536开始区后面8个块其中的540
objd: 0x0001d4f5 表示的是对象数据编号

把上面的 信息拿下来看
OBJECT_NAME OBJECT_ID DATA_OBJECT_ID
ZFK 119734 120052
ZFK_PK_OBJID 120051 120053
OBJECT_ID 十六进制
119734=>1d3b6
120051=>1d4f3

DATA_OBJECT_ID
120052=>1d4f4
120053=>1d4f5

对照看下 就知道访问的两个块都是 主键ZFK_PK_OBJD的. 分别是根节点和叶节点!
这里还没有回表,下面看看回表的操作

SQL> select object_name from ZFK t where object_id in(5);

OBJECT_NAME

CLU$
已用时间: 00: 00: 00.00

执行计划

Plan hash value: 1482799763

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

| 0 | SELECT STATEMENT | | 1 | 30 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| ZFK | 1 | 30 | 2 (0)| 00:00:01 |

|* 2 | INDEX UNIQUE SCAN | ZFK_PK_OBJID | 1 | | 1 (0)| 00:00:01 |

Predicate Information (identified by operation id):

2 - access(“OBJECT_ID”=5)

统计信息

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

跟踪文件内容

* 2017-02-20 16:47:31.046
Oradebug command ‘event 10200 trace name context forever,level 1’ console output:

* 2017-02-20 16:48:44.544
ktrgtc2(): started for block <0x0004 : 0x0100021b> objd: 0x0001d4f5
env [0x7f21fabda4cc]: (scn: 0x0000.16af666f xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 statement num=0 parent xid: 0x0000.000.00000000 st-scn: 0x0000.00000000 hi-scn: 0x0000.00000000 ma-scn: 0x0000.16af6659 flg: 0x00000660)
ktrexc(): returning 2 on: 0xc0f4928 cr-scn: 0xffff.ffffffff xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 cl-scn: 0xffff.ffffffff sfl: 0
ktrgtc2(): completed for block <0x0004 : 0x0100021b> objd: 0x0001d4f5
ktrgtc2(): started for block <0x0004 : 0x0100021c> objd: 0x0001d4f5
env [0x7f21fabda4cc]: (scn: 0x0000.16af666f xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 statement num=0 parent xid: 0x0000.000.00000000 st-scn: 0x0000.00000000 hi-scn: 0x0000.00000000 ma-scn: 0x0000.16af6659 flg: 0x00000660)
ktrexc(): returning 2 on: 0xc0f4928 cr-scn: 0xffff.ffffffff xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 cl-scn: 0xffff.ffffffff sfl: 0
ktrgtc2(): completed for block <0x0004 : 0x0100021c> objd: 0x0001d4f5
ktrgtc2(): started for block <0x000d : 0x0300014b> objd: 0x0001d4f4
env [0x7f21fabda4cc]: (scn: 0x0000.16af666f xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 statement num=0 parent xid: 0x0000.000.00000000 st-scn: 0x0000.00000000 hi-scn: 0x0000.00000000 ma-scn: 0x0000.16af6659 flg: 0x00000660)
ktrexc(): returning 2 on: 0xc0f4928 cr-scn: 0xffff.ffffffff xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 cl-scn: 0xffff.ffffffff sfl: 0
ktrgtc2(): completed for block <0x000d : 0x0300014b> objd: 0x0001d4f4

* 2017-02-20 16:49:02.999
Processing Oradebug command ‘event 10200 trace name context off’

这个时候你看到了它访问了3个块,前面两个块我们已经解读过了, 后面的objd: 0x0001d4f4 是表对象数据地址, 访问了块是 <0x000d : 0x0300014b>. 另外重执行,没有递归调用,也没有去访问数据字典.
请关注公众号:
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值