日期:2014-05-16  浏览次数:20420 次

oracle 物理读 逻辑读的理解
       逻辑读是指从内存中读,物理读是指从磁盘上读取数据;如果内存中存在相应的读取数据,那么直接从内存中读取,逻辑读的次数加1,如果不存在,则从磁盘上读到内存中,再从内存中读取,逻辑度次数加1,物理读次数也加1。需要注意的是,物理读非常消耗系统资源,一个物理读需要消耗8ms,而一秒可以处理20万个逻辑读。

1.物理读(physical read)

当数据块第一次读取到,就会缓存到buffer cache 中,而第二次读取和修改该数据块时就在内存buffer cache 了 以下是例子:

1.1  第一次读取

C:\Documents and Settings\Paul Yi>sqlplus "/as sysdba"

SQL*Plus: Release 9.2.0.4.0 - Production on Thu Feb 28 09:32:04 2008

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.


Connected to:
Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.4.0 - Production

SQL> set autotrace traceonly
SQL> select * from test;


Execution Plan
----------------------
    0      SELECT STATEMENT ptimizer=CHOOSE (Cost=2 Card=4 Bytes=8)
    1    0   TABLE ACCESS (FULL) OF 'TEST' (Cost=2 Card=4 Bytes=8)




Statistics
----------------------
         175  recursive calls
           0   db block gets
          24  consistent gets
           9   physical reads             --9个物理读
          0   redo size
        373  bytes sent via SQL*Net to client
        503  bytes received via SQL*Net from client
          2   SQL*Net roundtrips to/from client
          2   sorts (memory)
          0   sorts (disk)
          1   rows processed

1.2  第二次读取

SQL> select * from test;


Execution Plan
----------------------
   0      SELECT STATEMENT ptimizer=CHOOSE (Cost=2 Card=4 Bytes=8)
   1    0   TABLE ACCESS (FULL) OF 'TEST' (Cost=2 Card=4 Bytes=8)




Statistics
----------------------
          0  recursive calls
          0  db block gets
          7  consistent gets
          0  physical reads     --没有发生物理读了,直接从buffer cache 中读取了
          0  redo size
        373  bytes sent via SQL*Net to client
        503  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

1.3  数据块被重新读入buffer cache ,这种发生在如果有新的数据需要被读入Buffer Cache中,而Buffer Cache又没有足够的空闲空间,Oracle就根据LRU算法将LRU链表中LRU端的数据置换出去。当这些数据被再次访问到时,需要重新从磁盘读入。

SQL> alter session set events 'immediate trace name flush_cache';      --清空数据缓冲区