[20121015]探索索引-学习bbed.txt

[20121015]探索索引-学习bbed.txt
参考链接:http://www.adellera.it/blog/2009/05/24/order-keys-inside-index-blocks/
1.探索索引
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE    11.2.0.1.0      Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
SQL> create table t (x varchar2(10)) tablespace test;
SQL> create index i_t_x on t(x) tablespace test;
随机插入如下记录:
insert into t values('000000');
insert into t values('777777');
insert into t values('111111');
insert into t values('666666');
insert into t values('222222');
insert into t values('555555');
insert into t values('333333');
insert into t values('444444');
commit ;
2.看看对应的索引块的位置:
SQL> select object_id,data_object_id from dba_objects where wner=USER and object_name='I_T_X';
 OBJECT_ID DATA_OBJECT_ID
---------- --------------
    114882         114882
SQL> select object_id,data_object_id from dba_objects where wner=USER and object_name='T';
 OBJECT_ID DATA_OBJECT_ID
---------- --------------
    114881         114881

SQL> select header_file,header_block from dba_segments where wner=USER and segment_name='I_T_X';
HEADER_FILE HEADER_BLOCK
----------- ------------
          8          146

SQL> alter system checkpoint;
System altered.
--保证数据信息写到磁盘.
SQL> column dump(rowid,16) format a50
SQL> select dump(rowid,16) ,dbms_rowid.rowid_relative_fno(rowid) rfile#,dbms_rowid.ROWID_BLOCK_NUMBER(rowid) block#,dbms_rowid.ROWID_ROW_NUMBER(rowid) row#,t.* from scott.t;
DUMP(ROWID,16)                                         RFILE#     BLOCK#       ROW# X
-------------------------------------------------- ---------- ---------- ---------- ----------
Typ=69 Len=10: 0,1,c0,c1,2,0,0,8f,0,0                       8        143          0 000000
Typ=69 Len=10: 0,1,c0,c1,2,0,0,8f,0,1                       8        143          1 777777
Typ=69 Len=10: 0,1,c0,c1,2,0,0,8f,0,2                       8        143          2 111111
Typ=69 Len=10: 0,1,c0,c1,2,0,0,8f,0,3                       8        143          3 666666
Typ=69 Len=10: 0,1,c0,c1,2,0,0,8f,0,4                       8        143          4 222222
Typ=69 Len=10: 0,1,c0,c1,2,0,0,8f,0,5                       8        143          5 555555
Typ=69 Len=10: 0,1,c0,c1,2,0,0,8f,0,6                       8        143          6 333333
Typ=69 Len=10: 0,1,c0,c1,2,0,0,8f,0,7                       8        143          7 444444
8 rows selected.
$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
ibase=16
1C0C1
114881
--rowid前面几个字节对应表T的DATA_OBJECT_ID.
3.设置bbed
在.bashrc加入函数:
rlbbedro()
{
        cd /home/oracle11g/bbed
        rlwrap -s 9999 -c -r -i -f  /usr/local/share/rlwrap/bbed /u01/app/oracle11g/product/11.2.0/db_1/bin/bbed parfile=bbedreadonly.par cmdfile=cmd.par
}
$ cat bbed/bbedreadonly.par
blocksize=8192
listfile=/home/oracle11g/bbed/filelist.txt
mode=browse
PASSWORD=blockedit
$ cat bbed/cmd.par
set count 8192
set width 210
--安全起见,采用browse模式.

BBED> set dba 8,147
        DBA             0x02000093 (33554579 8,147)
--
BBED> map /v
 File: /u01/app/oracle11g/oradata/test/test01.dbf (8)
 Block: 147                                   Dba:0x02000093
------------------------------------------------------------
 KTB Data Block (Index Leaf)
 struct kcbh, 20 bytes                      @0
    ub1 type_kcbh                           @0
    ub1 frmt_kcbh                           @1
    ub1 spare1_kcbh                         @2
    ub1 spare2_kcbh                         @3
    ub4 rdba_kcbh                           @4
    ub4 bas_kcbh                            @8
    ub2 wrp_kcbh                            @12
    ub1 seq_kcbh                            @14
    ub1 flg_kcbh                            @15
    ub2 chkval_kcbh                         @16
    ub2 spare3_kcbh                         @18
 struct ktbbh, 72 bytes                     @20
    ub1 ktbbhtyp                            @20
    union ktbbhsid, 4 bytes                 @24
    struct ktbbhcsc, 8 bytes                @28
    sb2 ktbbhict                            @36
    ub1 ktbbhflg                            @38
    ub1 ktbbhfsl                            @39
    ub4 ktbbhfnx                            @40
    struct ktbbhitl[2], 48 bytes            @44
 struct kdxle, 32 bytes                     @100
    struct kdxlexco, 16 bytes               @100
    sb2 kdxlespl                            @116
    sb2 kdxlende                            @118
    ub4 kdxlenxt                            @120
    ub4 kdxleprv                            @124
    ub1 kdxledsz                            @128
    ub1 kdxleflg                            @129
 sb2 kd_off[8]                              @132
 ub1 freespace[7852]                        @148
 ub1 rowdata[128]                           @8000
 ub4 tailchk                                @8188

BBED> p kd_off
sb2 kd_off[0]                               @132      8032
sb2 kd_off[1]                               @134      0
sb2 kd_off[2]                               @136      8016
sb2 kd_off[3]                               @138      7984
sb2 kd_off[4]                               @140      7952
sb2 kd_off[5]                               @142      7920
sb2 kd_off[6]                               @144      7904
sb2 kd_off[7]                               @146      7936
--最小的位置是:
sb2 kd_off[6]                               @144      7904
BBED> p  *kd_off[6]
rowdata[4]
----------
ub1 rowdata[4]                              @8004     0x00
BBED> x /8rcx
rowdata[4]                                  @8004
----------
flag@8004:     0x00 (NONE)
lock@8005:     0x02
data key:
col    0[6] @8007: 444444
col    1[6] @8014:  0x02  0x00  0x00  0x8f  0x00  0x07
rowdata[20]                                 @8020
-----------
flag@8020:     0x00 (NONE)
lock@8021:     0x02
data key:
col    0[6] @8023: 333333
col    1[6] @8030:  0x02  0x00  0x00  0x8f  0x00  0x06
rowdata[36]                                 @8036
-----------
flag@8036:     0x00 (NONE)
lock@8037:     0x02
data key:
col    0[6] @8039: 555555
col    1[6] @8046:  0x02  0x00  0x00  0x8f  0x00  0x05
rowdata[52]                                 @8052
-----------
flag@8052:     0x00 (NONE)
lock@8053:     0x02
data key:
col    0[6] @8055: 222222
col    1[6] @8062:  0x02  0x00  0x00  0x8f  0x00  0x04
rowdata[68]                                 @8068
-----------
flag@8068:     0x00 (NONE)
lock@8069:     0x02
data key:
col    0[6] @8071: 666666
col    1[6] @8078:  0x02  0x00  0x00  0x8f  0x00  0x03
rowdata[84]                                 @8084
-----------
flag@8084:     0x00 (NONE)
lock@8085:     0x02
data key:
col    0[6] @8087: 111111
col    1[6] @8094:  0x02  0x00  0x00  0x8f  0x00  0x02
rowdata[100]                                @8100
------------
flag@8100:     0x00 (NONE)
lock@8101:     0x02
data key:
col    0[6] @8103: 777777
col    1[6] @8110:  0x02  0x00  0x00  0x8f  0x00  0x01
rowdata[116]                                @8116
------------
flag@8116:     0x00 (NONE)
lock@8117:     0x02
data key:
col    0[6] @8119: 000000
col    1[6] @8126:  0x02  0x00  0x00  0x8f  0x00  0x00
--可以发现(倒着看),索引也是从底部插入的,与数据的插入顺序一致.
--而且rowid也对应.
4.看看kd_off结构:
sb2 kd_off[0]                               @132      8032
sb2 kd_off[1]                               @134      0
sb2 kd_off[2]                               @136      8016
sb2 kd_off[3]                               @138      7984
sb2 kd_off[4]                               @140      7952
sb2 kd_off[5]                               @142      7920
sb2 kd_off[6]                               @144      7904
sb2 kd_off[7]                               @146      7936
BBED> dump /v offset 132 count 32
 File: /u01/app/oracle11g/oradata/test/test01.dbf (8)
 Block: 147                                                         Offsets:  132 to  163                                                      Dba:0x02000093
---------------------------------------------------------------------------------------------------------------------------------------------------------------
 601f0000 501f301f 101ff01e e01e001f 201f401f 00000000 00000000 00000000                                     l `...P.0......... .@.............
 
--对比上面可以发现,这里的顺序是排序的.
--另外11G的bbed可能存在一些问题,kd_off 存在偏差,遗漏了1f20(7968),1f40(8000).
BBED> p *kd_off[2]
rowdata[116]
------------
ub1 rowdata[116]                            @8116     0x00
BBED> x /rcx
rowdata[116]                                @8116
------------
flag@8116:     0x00 (NONE)
lock@8117:     0x02
data key:
col    0[6] @8119: 000000
col    1[6] @8126:  0x02  0x00  0x00  0x8f  0x00  0x00

BBED> p *kd_off[3]
rowdata[84]
-----------
ub1 rowdata[84]                             @8084     0x00
BBED> x /rcx
rowdata[84]                                 @8084
-----------
flag@8084:     0x00 (NONE)
lock@8085:     0x02
data key:
col    0[6] @8087: 111111
col    1[6] @8094:  0x02  0x00  0x00  0x8f  0x00  0x02

BBED> p *kd_off[4]
rowdata[52]
-----------
ub1 rowdata[52]                             @8052     0x00
BBED> x /rcx
rowdata[52]                                 @8052
-----------
flag@8052:     0x00 (NONE)
lock@8053:     0x02
data key:
col    0[6] @8055: 222222
col    1[6] @8062:  0x02  0x00  0x00  0x8f  0x00  0x04

BBED> p *kd_off[5]
rowdata[20]
-----------
ub1 rowdata[20]                             @8020     0x00
BBED> x /rcx
rowdata[20]                                 @8020
-----------
flag@8020:     0x00 (NONE)
lock@8021:     0x02
data key:
col    0[6] @8023: 333333
col    1[6] @8030:  0x02  0x00  0x00  0x8f  0x00  0x06

BBED> p *kd_off[6]
rowdata[4]
----------
ub1 rowdata[4]                              @8004     0x00
BBED> x /rcx
rowdata[4]                                  @8004
----------
flag@8004:     0x00 (NONE)
lock@8005:     0x02
data key:
col    0[6] @8007: 444444
col    1[6] @8014:  0x02  0x00  0x00  0x8f  0x00  0x07
BBED> p *kd_off[7]
rowdata[36]
-----------
ub1 rowdata[36]                             @8036     0x00
BBED> x /rcx
rowdata[36]                                 @8036
-----------
flag@8036:     0x00 (NONE)
lock@8037:     0x02
data key:
col    0[6] @8039: 555555
col    1[6] @8046:  0x02  0x00  0x00  0x8f  0x00  0x05
--7968+100
BBED> set offset 8068
        OFFSET          8068
BBED> x /rcx
rowdata[68]                                 @8068
-----------
flag@8068:     0x00 (NONE)
lock@8069:     0x02
data key:
col    0[6] @8071: 666666
col    1[6] @8078:  0x02  0x00  0x00  0x8f  0x00  0x03
--8000+100
BBED> set offset 8100
        OFFSET          8100
BBED> x /rcx
rowdata[100]                                @8100
------------
flag@8100:     0x00 (NONE)
lock@8101:     0x02
data key:
col    0[6] @8103: 777777
col    1[6] @8110:  0x02  0x00  0x00  0x8f  0x00  0x01
5.总结:
可以发现块内索引值是无序的,kd_off内指定的位置对应的值才是有序的.
				
时间: 2024-10-10 12:27:06

[20121015]探索索引-学习bbed.txt的相关文章

[20120509]IOT索引组织表相关信息的学习(三).txt

[20120509]IOT索引组织表相关信息的学习(三).txt 上次链接:http://space.itpub.net/267265/viewspace-719517http://space.itpub.net/267265/viewspace-717272 IOT 是一种特殊的索引结构,使用它能够解决特定场合的应用问题,但是在许多应用中很少使用,更多的是使用堆表.我仅仅在生产系统中对3个表使用,我偏向使用静态以及"只读"的小表. 如果IOT表存在除主键外的第2索引,如果使用它存在物

[20120509]IOT索引组织表相关信息的学习(四).txt

[20120509]IOT索引组织表相关信息的学习(四).txt 今天看了一个有关IOT的介绍:http://richardfoote.wordpress.com/2012/04/11/iot-secondary-indexes-primary-key-considerations-beauty-and-the-beast/     If we create a secondary index on a column that forms part of the PK, Oracle can b

[20120803]11G SPM的学习1.txt

[20120803]11G SPM的学习1.txt     开始学习SQL Plan Management(SPM) ,11G开始提供SPM,在10g下我经常使用sql profile看一些bad sql语句,sql profile我觉得已经做的很好,有时候能够提供很好的建议.我开始学习SPM的时候感觉不习惯(也许是因为在toad下使用sql profile很简单)为什么oracle还有搞出SPM来,慢慢看资料,才明白其中一些细节.     我看过别人在从8i升级到9i的时候,出现性能波动,里面

[20121120]windows下使用bbed.txt

[20121120]windows下使用bbed.txt 学习需要,需要在windows下使用bbed,但是我发现在windows下如何编译bbed呢?我google发现如下链接: http://www.xifenfei.com/3876.html 发现我公司的9.2.0.8的windows版本有bbed.exe,按照介绍我copy这些相关文件到我的机器:  驱动器 D 中的卷是 DATA 卷的序列号是 38FB-540B  D:\tools\bbed 的目录 2012-11-20  08:39

[20120806]11G SPM的学习3.txt

[20120806]11G SPM的学习3.txt 继续上面的学习: SQL> select * from v$version ; BANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production PL/SQL Release 11.2

[20120805]11G SPM的学习2.txt

[20120805]11G SPM的学习2.txt 继续上面的学习: SQL> select * from v$version ; BANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production PL/SQL Release 11.2

探索索引的奥秘 - 有索引就一定会用么?

上一篇文章<探索索引的奥秘 - 索引的属性>,我们了解了索引的属性,回顾一下, > 索引设置为unusable,会有以下特点,     1. 索引设置为unusable,此时会删除索引段.     2. 索引处于unusable期间,对表数据做DML操作,此时不维护索引.     3. 索引处于unusable期间,优化器会忽略此索引.     4. 索引处于unusable期间,由于不需要维护索引,因此可以提升批量导入性能.     5. 索引unusable变为usable,有两种方

[20171202]关于函数索引的状态.txt

[20171202]关于函数索引的状态.txt --//我曾经在一篇贴子提到索引可以disable吗?链接: --//http://blog.itpub.net/267265/viewspace-2123537/ --//实际上仅仅函数索引能disable,为什么呢?实际上自己以前并不搞清楚实际上这个跟oracle使用函数的特殊性有关. --//如果一个表删除某个字段,对应的索引也会删除.如果定义的函数删除了,对应的函数索引呢?通过例子来说明问题: 1.环境: SCOTT@test01p> @

[20150304]唯一索引与阻塞.txt

[20150304]唯一索引与阻塞.txt --昨天帮别人定位一个唯一索引导致出现ora-00001的问题,实际上很简单,程序使用max(id)取得最大号,然后插入,这样的结果在业务 --高峰,出现阻塞或者ora-00001错误.我仅仅简单做一下跟踪很容易定位这个问题. --换一个角度,使用别的方法是否可行呢,自己做一个例子来验证看看. 1.建立测试环境: SCOTT@test> create table t as select rownum id,cast ( 'test' as varcha