db block get和consistent read get

尛樣兒發表於2011-06-15

Buffer Modes

When a client requests data, Oracle Database retrieves buffers from the database buffer cache in either of the following modes:

  • Current mode

    A current mode get, also called a db block get, is a retrieval of a block as it currently appears in the buffer cache. For example, if an uncommitted transaction has updated two rows in a block, then a current mode get retrieves the block with these uncommitted rows. The database uses db block gets most frequently during modification statements, which must update only the current version of the block.

  • Consistent mode

    A consistent read get is a retrieval of a read-consistent version of a block. This retrieval may use . For example, if an uncommitted transaction has updated two rows in a block, and if a query in a separate session requests the block, then the database uses undo data to create a read-consistent version of this block (called a consistent read clone) that does not include the uncommitted updates. Typically, a query retrieves blocks in consistent mode.

這裡我們主要討論db block get發生的情況:
1.建立模擬資料:
SQL> create user test identified by test;

使用者已建立。

SQL> grant connect,resource to test;

授權成功。

SQL>
SQL>
SQL> connect test/test
已連線。
SQL> create table test(id number);

表已建立。

SQL> insert into test values (1);

已建立 1 行。

SQL> insert into test values (2);

已建立 1 行。

SQL> commit;

提交完成。

SQL>
SQL>
SQL> select DBMS_ROWID.ROWID_BLOCK_NUMBER(rowid),id from test;

DBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID)         ID
------------------------------------ ----------
                                 174          1
                                 174          2
建立了一張test表,插入了兩條記錄,兩條記錄都儲存在相同的塊。

2.用sys使用者執行如下兩個指令碼,使得test使用者也能跟蹤統計資訊:
SQL> @?/rdbms/admin/utlxplan.sql

表已建立。

SQL> @?/sqlplus/admin/plustrce.sql
SQL>
SQL> drop role plustrace;
drop role plustrace
          *
第 1 行出現錯誤:
ORA-01919: 角色 'PLUSTRACE' 不存在


SQL> create role plustrace;

角色已建立。

SQL>
SQL> grant select on v_$sesstat to plustrace;

授權成功。

SQL> grant select on v_$statname to plustrace;

授權成功。

SQL> grant select on v_$mystat to plustrace;

授權成功。

SQL> grant plustrace to dba with admin option;

授權成功。

SQL>
SQL> set echo off
SQL> grant plustrace to test;

授權成功。

3.模擬db block gets發生情況,db block gets多發生在執行DML語句的時候。
場景1:事務A讀取了一個塊到記憶體中,這時事務B也需要讀取這個塊來進行修改,事務B不需要從磁碟上讀取,直接從事務A之前讀取到記憶體中的塊來讀取,如果事務B操作的資料對應的塊和事務A選擇的資料是在同一個塊上就會發生db block gets。

會話1:
SQL> set autotrace trace statistics;
SQL> select * from test where id=1;


統計資訊
----------------------------------------------------------
        268  recursive calls
          0  db block gets
         46  consistent gets
         17  physical reads
          0  redo size
        417  bytes sent via SQL*Net to client
        415  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          5  sorts (memory)
          0  sorts (disk)
          1  rows processed
會話2:
SQL> set autotrace trace statistics;
SQL> update test set id=6 where id=2;

已更新 1 行。


統計資訊
----------------------------------------------------------
          6  recursive calls
          3  db block gets
         20  consistent gets
          2  physical reads
          0  redo size
        671  bytes sent via SQL*Net to client
        600  bytes received via SQL*Net from client
          3  SQL*Net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
          1  rows processed

場景2:如果事務A修改某條資料,從磁碟中讀到了記憶體,並未提交,這時事務B也修改了表的一條資料,資料所在的塊和事務A修改的資料在相同的塊上。事務B不會從磁碟讀取這個塊,而是直接中事務A讀取到記憶體中的塊讀取,這時也會發生db block gets,db block gets的發生產生了髒讀,也就說事務A沒有提交的資料也讀取到事務B的塊中來了,但是由於事務B與事務A修改的不是同一條資料,所以髒讀並不會造成資料的不一致。db block gets這也是提高SQL執行效率的處理方式。
先用sys使用者清空shared_pool和buffer_cache:
SQL> alter system flush buffer_cache;

系統已更改。

SQL> alter system flush shared_pool;

系統已更改。

會話1:
SQL> set autotrace trace statistics;
SQL> update test set id=5 where id=1;

已更新 1 行。


統計資訊
----------------------------------------------------------
        239  recursive calls
          3  db block gets
         48  consistent gets
         11  physical reads
          0  redo size
        674  bytes sent via SQL*Net to client
        600  bytes received via SQL*Net from client
          3  SQL*Net roundtrips to/from client
          6  sorts (memory)
          0  sorts (disk)
          1  rows processed

會話2:
SQL> set autotrace trace statistics;
SQL> update test set id=6 where id=2;

已更新 1 行。


統計資訊
----------------------------------------------------------
          4  recursive calls
          3  db block gets
         17  consistent gets
          0  physical reads
        476  redo size
        670  bytes sent via SQL*Net to client
        599  bytes received via SQL*Net from client
          3  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
          1  rows processed

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23135684/viewspace-697963/,如需轉載,請註明出處,否則將追究法律責任。

相關文章