1、物理读即是把数据从磁盘读入到buffer catch的过程。
通常情况下是,如果需要数据的时候发现不存在于buffer catch当中,即oracle就会执行物理读。
例子如下:
C:/Documents and Settings/Administrator>sqlplus jenny/jenny
SQL> set autotrace traceonly;
SQL> select * from t_test1;
----------------------------------------------------------
Plan hash value: 1883417357
----------------------------------------------------------
0 recursive calls
0 db block gets
11 consistent gets
3 physical reads --发生了3次物理读写
0 redo size
10632 bytes sent via SQL*Net to client
451 bytes received via SQL*Net from client
8 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
97 rows processed
SQL> select * from t_test1;
----------------------------------------------------------
Plan hash value: 1883417357
----------------------------------------------------------
0 recursive calls
0 db block gets
11 consistent gets
0 physical reads --这里没有发生物理读取,因为这时数据已存在buffer catche中
0 redo size
10632 bytes sent via SQL*Net to client
451 bytes received via SQL*Net from client
8 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
97 rows processed
如果重新清掉缓存,再执行以上查询语句,又会重新把数据从磁盘读入数据,即会发生物理读取
SQL> alter session set events 'immediate trace name flush_cache';
SQL> select * from t_test1;
----------------------------------------------------------
Plan hash value: 1883417355
........
----------------------------------------------------------
0 recursive calls
0 db block gets
11 consistent gets
3 physical reads --重新发生物理读取
0 redo size
10632 bytes sent via SQL*Net to client
451 bytes received via SQL*Net from client
8 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
97 rows processed
2、逻辑读取
逻辑读指的就是从(或者视图从)Buffer Cache中读取数据块。按照访问数据块的模式不同,可以分为即时读(Current Read)和一致性读(Consistent Read)。
1) 即时读
即时读即读取数据块当前的最新数据。任何时候在Buffer Cache中都只有一份当前数据块。即时读通常发生在对数据进行修改、删除操作时。这时,进程会给数据加上行级锁,并且标识数据为“脏”数据。
SQL> select * from t_test1 for update;
----------------------------------------------------------
Plan hash value: 3323170753
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 97 | 8536 | 3 (0)| 00:00:01 |
| 1 | FOR UPDATE | | | | | |
| 2 | TABLE ACCESS FULL| T_TEST1 | 97 | 8536 | 3 (0)| 00:00:01 |
----------------------------------------------------------
1 recursive calls
99 db block gets --即时读取
15 consistent gets
0 physical reads
19608 redo size
9123 bytes sent via SQL*Net to client
451 bytes received via SQL*Net from client
8 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
97 rows processed
2) 一致性读
Oracle是一个多用户系统。当一个会话开始读取数据还未结束读取之前,可能会有其他会话修改它将要读取的数据。如果会话读取到修改后的数据,就会造成数据的不一致。
一致性读就是为了保证数据的一致性。在Buffer Cache中的数据块上都会有最后一次修改数据块时的SCN。如果一个事务需要修改数据块中数据,会先在回滚段中保存一份修改前数据和SCN的数据块,然后再更新Buffer Cache中的数据块的数据及其SCN,并标识其为“脏”数据。
当其他进程读取数据块时,会先比较数据块上的SCN和自己的SCN。如果数据块上的SCN小于等于进程本身的SCN,则直接读取数据块上的数据;如果数据块上的SCN大于进程本身的SCN,则会从回滚段中找出修改前的数据块读取数据。通常,普通查询都是一致性读。
SQL> select * from t_test1
----------------------------------------------------------
Plan hash value: 1883417357
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 97 | 8536 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T_TEST1 | 97 | 8536 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------
1 recursive calls
0 db block gets
11 consistent gets --一致性读取,这是没有读取回滚段情况下
0 physical reads
0 redo size
10632 bytes sent via SQL*Net to client
451 bytes received via SQL*Net from client
8 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
97 rows processed
打开另一个session执行同一个表的update或者delete并且是在没有commit的情况下(只有这种情况才会去读回滚段),再执行sql
SQL> select * from t_test1;
----------------------------------------------------------
Plan hash value: 1883417357
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 97 | 8536 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T_TEST1 | 97 | 8536 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------
0 recursive calls
0 db block gets
12 consistent gets --需要读回滚段的数据,所以会增加consistent gets
0 physical reads
0 redo size
10632 bytes sent via SQL*Net to client
451 bytes received via SQL*Net from client
8 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
97 rows processed