您的位置:首页 > 其它

通过案例学调优之--动态视图(v$bh和x$bh)

2015-03-31 12:06 295 查看
通过案例学调优之--动态视图(v$bh和x$bh)v$bh与x$bh
v$bh:非常详细地记录了数据块在数据缓冲区内的使用情况,一条记录对应一个block的详细记录。
v$bh来自于基表x$bh与x$le

查看v$bh结构:
VIEW_NAME                      VIEW_DEFINITION
------------------------------ --------------------------------------------------
GV$BH                          select bh.inst_id, file#, dbablk, class, decode(st
                               ate,0,'free',1,'xcur',2,'scur',3,'cr', 4,'read',5,
                               'mrec',6,'irec',7,'write',8,'pi', 9,'memory',10,'m
                               write',11,'donated', 12,'protected',  13,'securefi
                               le', 14,'siop',15,'recckpt', 16, 'flashfree',  17,
                                'flashcur', 18, 'flashna'), 0, 0, 0, bh.le_addr,
                               le_id1, le_id2, decode(bitand(flag,1), 0, 'N', 'Y'
                               ), decode(bitand(flag,16), 0, 'N', 'Y'), decode(bi
                               tand(flag,1536), 0, 'N', 'Y'), decode(bitand(flag,
                               16384), 0, 'N', 'Y'), decode(bitand(flag,65536), 0
                               , 'N', 'Y'), 'N', obj, ts#, lobid,  bitand(OBJ_FLA
                               G, 240)/16 from x$bh bh, x$le le where bh.le_addr
                               = le.le_addr (+)
13:29:01 SCOTT@ test1 >truncate table t1;
13:29:09 SCOTT@ test1 >begin
13:29:12   2  for i in 1..5000 loop
13:29:25   3  insert into t1 values (i);
13:29:35   4  end loop;
13:29:38   5  end;
13:29:39   6  /
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.67
13:29:41 SCOTT@ test1 >commit;[code;'>创建一存储过程:show space读取数据块信息

13:29:43 SCOTT@ test1 >create or replace procedure show_space
13:34:44   2
13:34:44   3    ( p_segname in varchar2,
13:34:44   4
13:34:44   5    p_owner   in varchar2 default user,
13:34:44   6
13:34:44   7    p_type    in varchar2 default 'TABLE',
13:34:44   8
13:34:44   9    p_partition in varchar2 default NULL )
13:34:44  10
13:34:44  11  as
13:34:44  12
13:34:44  13      l_total_blocks              number;
13:34:44  14
13:34:44  15      l_total_bytes               number;
13:34:44  16
13:34:44  17      l_unused_blocks             number;
13:34:44  18
13:34:44  19      l_unused_bytes              number;
13:34:44  20
13:34:44  21      l_LastUsedExtFileId         number;
13:34:44  22
13:34:44  23      l_LastUsedExtBlockId        number;
13:34:44  24
13:34:44  25      l_last_used_block           number;
13:34:44  26
13:34:44  27      procedure p( p_label in varchar2, p_num in number )
13:34:44  28
13:34:44  29      is
13:34:44  30
13:34:44  31      begin
13:34:44  32
13:34:44  33          dbms_output.put_line( rpad(p_label,40,'.') ||
13:34:44  34
13:34:45  35                                p_num );
13:34:45  36
13:34:45  37      end;
13:34:45  38
13:34:45  39  begin
13:34:45  40
13:34:45  41
13:34:45  42
13:34:45  43      dbms_space.unused_space
13:34:45  44
13:34:45  45      ( segment_owner     => p_owner,
13:34:45  46
13:34:45  47        segment_name      => p_segname,
13:34:45  48
13:34:45  49        segment_type      => p_type,
13:34:45  50
13:34:45  51        partition_name    => p_partition,
13:34:45  52
13:34:45  53        total_blocks      => l_total_blocks,
13:34:45  54
13:34:45  55        total_bytes       => l_total_bytes,
13:34:45  56
13:34:45  57        unused_blocks     => l_unused_blocks,
13:34:45  58
13:34:45  59        unused_bytes      => l_unused_bytes,
13:34:45  60
13:34:45  61        last_used_extent_file_id => l_LastUsedExtFileId,
13:34:45  62
13:34:45  63        last_used_extent_block_id => l_LastUsedExtBlockId,
13:34:45  64
13:34:45  65        last_used_block => l_last_used_block );
13:34:45  66
13:34:45  67
13:34:45  68
13:34:45  69      p( 'Total Blocks', l_total_blocks );
13:34:45  70
13:34:45  71      p( 'Total Bytes', l_total_bytes );
13:34:45  72
13:34:45  73      p( 'Unused Blocks', l_unused_blocks );
13:34:45  74
13:34:45  75      p( 'Unused Bytes', l_unused_bytes );
13:34:45  76
13:34:45  77      p( 'Last Used Ext FileId', l_LastUsedExtFileId );
13:34:45  78
13:34:45  79      p( 'Last Used Ext BlockId', l_LastUsedExtBlockId );
13:34:45  80
13:34:45  81      p( 'Last Used Block', l_last_used_block );
13:34:45  82
13:34:45  83  end;
13:34:47  84  /13:34:49 SCOTT@ test1 >set serverout on13:35:01 SCOTT@ test1 >exec show_space(p_segname=>'T1');
Total Blocks............................16
Total Bytes.............................131072
Unused Blocks...........................0
Unused Bytes............................0
Last Used Ext FileId....................4
Last Used Ext BlockId...................136
Last Used Block.........................8
PL/SQL procedure successfully completed.13:42:52 SYS@ test1 >select file#,name from v$datafile;
     FILE# NAME
---------- --------------------------------------------------
         1 /u01/app/oracle/oradata/test1/system01.dbf
         2 /u01/app/oracle/oradata/test1/sysaux01.dbf
         3 /u01/app/oracle/oradata/test1/undotbs01.dbf
         4 /u01/app/oracle/oradata/test1/users01.dbf
        10 /u01/app/oracle/oradata/test1/index01.dbf
        11 /dsk1/oradata/test1/users02.dbf
6 rows selected. FILE#     BLOCK# STATUS
---------- ---------- ----------
         4        136 xcur
         4        137 xcur
         4        138 xcur
         4        139 xcur
         4        140 xcur
         4        141 xcur
         4        142 xcur
         4        143 xcur
        11        688 xcur
        11        689 xcur
        11        690 xcur
        11        690 cr
        11        691 xcur
        11        692 xcur
        11        693 xcur
        11        694 xcur
        11        695 xcur
17 rows selected.[b]14:03:30 SYS@ test1 >alter system flush buffer_cache;
System altered.

14:06:02 SYS@ test1 >select file#,dbablk,tch from x$bh where obj=(select data_object_id from dba_objects where owner='SCOTT' AND object_name='T1') order by dbablk;
no rows selected

14:06:16 SYS@ test1 >select file#,block#,status from v$bh where objd=(select data_object_id from dba_objects where owner='SCOTT' and object_name='T1') order by block#;
no rows selected统计一个object的非free状态的v$bh的记录数,基本就反映了一个对象再data buffer中被cache的块数。

14:10:29 SYS@ test1 >select count(*) from v$bh where objd=
14:10:45   2  (select data_object_id from dba_objects
14:10:59   3   where owner='SCOTT' and object_name='T1')
14:11:23   4  and status !='free';
 FILE#     BLOCK# STATUS
---------- ---------- ----------
         4        143 xcur
        11        688 xcur
        11        689 xcur
        11        690 xcur14:29:05 SYS@ test1 >select tch from x$bh
14:29:20   2   where obj=16399 and dbablk=143 and file#=4 and tch>0;
       TCH
----------
         1[b]3)访问block 143

采用了dbms_rowid.rowid_create函数来创建rowid,表示新创建的rowid类型为扩展rowid,类型为1;data_object_id为16399;数据文件id为4;块的id为143;行数为一行(0)。14:30:47 SYS@ test1 >select count(*) from scott.t1
14:31:02   2   where rowid=dbms_rowid.rowid_create(1,16399,4,143,0);
  COUNT(*)
----------
         1多次访问后,block 143的tch在增加14:32:19 SYS@ test1 >select tch from x$bh where obj=16399 and dbablk=143 and file#=4 and tch>0;
       TCH
----------
         2
         
14:32:35 SYS@ test1 >select count(*) from scott.t1 where rowid=dbms_rowid.rowid_create(1,16399,4,143,0);
  COUNT(*)
----------
         1

14:33:23 SYS@ test1 >select tch from x$bh where obj=16399 and dbablk=143 and file#=4 and tch>0;
       TCH
----------
         3block 143的初始tch=1,经过连续两次查询后,tch=3.根据这一变化,可以基本判断x$bh表中的tch大的块,一般都是热点块。

[b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: