oracle 物理读,逻辑读的理解 / 2008-02-28 09:51:23/ 个人分类:学习 查看( 3120 ) /评论( 22 ) /评分( 23 / 11 ) 1.物理读(physical read) 当数据块第一次读取到,就会缓存到buffer cache 中,而第二次读取和修改该数据块时就在内存buffer cache 了 以下是例
oracle 物理读,逻辑读的理解 / 2008-02-28 09:51:23/ 个人分类:学习
查看( 3120 ) /评论( 22 ) /评分( 23 / 11 )
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';--清空数据缓冲区
session altered.
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
6 physical reads --又重新发生了物理读
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
2.逻辑读(buffer read)
逻辑读指的就是从(或者视图从)buffer cache中读取数据块。按照访问数据块的模式不同,可以分为即时读(current read)和一致性读(consistent read)。注意:逻辑io只有逻辑读,没有逻辑写。
即时读即时读即读取数据块当前的最新数据。任何时候在buffer cache中都只有一份当前数据块。即时读通常发生在对数据进行修改、删除操作时。这时,进程会给数据加上行级锁,并且标识数据为“脏”数据。
sql> select * from test for update;
execution plan
----------------------------------------------------------
0 select statement ptimizer=choose (cost=2 card=4 bytes=8)
1 0 for update
2 1 table access (full) of 'test' (cost=2 card=4 bytes=8)
statistics
----------------------------------------------------------
0 recursive calls
1 db block gets
14 consistent gets
0 physical reads
252 redo size
386 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
sql>
一致性读oracle是一个多用户系统。当一个会话开始读取数据还未结束读取之前,可能会有其他会话修改它将要读取的数据。如果会话读取到修改后的数据,就会造成数据的不一致。一致性读就是为了保证数据的一致性。在buffer cache中的数据块上都会有最后一次修改数据块时的scn。如果一个事务需要修改数据块中数据,会先在回滚段中保存一份修改前数据和scn的数据块,然后再更新buffer cache中的数据块的数据及其scn,并标识其为“脏”数据。当其他进程读取数据块时,会先比较数据块上的scn和自己的scn。如果数据块上的scn小于等于进程本身的scn,则直接读取数据块上的数据;如果数据块上的scn大于进程本身的scn,则会从回滚段中找出修改前的数据块读取数据。通常,普通查询都是一致性读。
下面这个例子帮助大家理解一下一致性读:
会话1中:
sql> select * from test;
id
----------
1000
sql> update test set id=2000;
1 row updated.
会话2中:
sql> set autotrace on
sql> select * from test;
id
----------
1000
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
9 consistent gets 没有事物做update时 是 7 consistent gets 说明多了2个 consistent gets 这2个是要从回滚段中获取的
0 physical reads
52 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
sql>