Physical Reads(物理读)和Logical Reads(逻辑读)(转)

来源:互联网 发布:剑三马驹坐标数据导入 编辑:程序博客网 时间:2024/05/22 02:08

现在没得时间去总结,记录一下别人写的有关文章了

这个是关于oracle里的物理读和逻辑读比较清楚的文章

 

physicalreads> "physicalreadsdirect +physicalreads cache"

Total number of data blocks read from disk.
This value can be greater than the value of "physicalreads direct" plus "physicalreadscache" as reads into process private buffers also included in this statistic.

physicalreads direct(物理上直接读取的块数

Number of reads directly from disk, bypassing the buffer cache.
For example, in high bandwidth, data-intensive operations such as parallel query,reads of disk blocks bypass the buffer cache to maximize transfer rates and to prevent the premature aging of shared data blocks resident in the buffer cache.


physicalreadscache(物理读取缓存)
Total number of data blocks read from disk into the buffer cache. This is a subset of "physicalreads" statistic.

sessionlogicalreads= "db block gets" + "consistent gets".

逻辑读 = 一致读 + db block gets
物理读是内容不在内存中,要去硬盘中读入内存 增加IO
逻辑读内容在内存中,不需要读硬盘 无IO

consistent gets:来自buffer cache + undo(如果有数据改变的话,即在查询点之前的DML操作没有提交)
db_block_gets:来自buffer cache

DB Block Gets:请求的数据块在buffer能满足的个数
Consistent Gets:数据请求在回滚段Buffer中的总数
逻辑读 = 一致读 + DB Block Gets

逻辑读就是从内存在读取的数据块数,物理读就是从硬盘读取到缓冲区的数据块数。

The sum of "db block gets" plus "consistent gets".This includeslogicalreadsof database blocks from either the buffer cache or process private memory.

db block gets
Number of times a CURRENT block was requested

consistent gets
Number of times a consistent read was requested for a block.
See Also: "consistent changes" and "session logicalreads" statistics

consistent changes
Number of times a user process has applied rollback entries to perform a consistent read on the block
Work loads that produce a great deal of consistent changes can consume a great deal of resources. The value of this statistic should be small in relation to the "consistent gets" statistic.

PhysicalReads:从磁盘读到Buffer cache数据块的数量

物理读是由于在DB_BLOCK_BUFFER中没有找到需要的数据,需要进行缓冲区的数据置换,物理读操作是非常消耗系统资源的,应当尽量避免。

DB Bocks gets,Consistent gets And Physicalreads
通过前面关于索引扫描还是全表扫描(Index Scan Or Full Table Scan)?的讨论又发现了自己基础概念不清晰的一个大问题:
DB block gets,Consistent gets,PhysicalreadsLogicalreads各自具体表示的是什么以及之间的的关系到底是怎样。

官方在线文档基本的定义为如下:
DB block gets:the number of accesses to the current image of a block
Consistent gets:the number of accesses to a read-consistent image of a block
Physicalreads:the number of blocks read from disk

通过官方的简要解释加上对fenng和旺旺的请教以及网上搜索了一些资料,终于加深了一些理解:

这其中主要涉及到了Oracle读取数据的consistent mode和current mode这两个模式,对于db block gets是在current mode下读取的block数目(单位应该是“块次”,同一个block读取了两个算做2),而consistent gets是在consistent mode下读取的block数目(单位同上)。
current mode下读取数据是为了保证读取到的数据是当前时间点上最新的数据,这样做的目的一般都是为了DML语句的需求,比如需要更新,自然需要知道最新的数据才行;consistent mode呢主要是为了保证Oracle数据一致读的特性,一般都是在select情况下发生,读到的数据可能是一个实际存在的block,也有可能需要根据scn信息以及transaction相关信息以及回滚段中数据来构造。

physicalreads是与logicalreads相对的一个概念,两者的区别是读取的数据是从buffer中读取到还是从disk上的db file中取到。通过v$sysstat也可以看到,里面还有db block gets from cache以及consistent gets from cache两项,且这两项的数值与db block gets和consistent gets并不相同且小于后两者。

所以不管是db block gets还是consistent gets,都可能出现了physicalreadslogicalreads两种情况(由buffer中的是否已经存在需要的数据),也即是说,db block gets与consistent gets两者已经构成了一次数据库操作中读取的所有block的总次数了。因此,logicalreads自然也就可以通过如下公式算的:logicalreads= (db block gets + consistent gets) -physicalreads

由此,自然也就得出了cache命中率的公式:
Hit Ratio = (db block gets + consistent gets - physicalreads) / (db block gets + consistent gets)
OR
Hit Ratio = 1 – (physicalreads/(db block gets + consistent gets))

PS:
由于在Oracle中,取数据最后都是从Buffer中取,所以每出现一个physicalreads必然会出现一次 logicalreads,但是这里有一个需要注意的地方,就是当出现一个physicalreads后接着会有一个logicalreads这里,实际上这里只算了1 block(physicalreads)!

一致读是由于需要读取的数据已经被锁定或修改,需要从回滚段的缓冲区中读取,一致读保证了数据一致性,但是大量的一致读说明系统设计或内存配置出现了问题,也是需要特别注意的。

DB Block Gets是指用户请求而且又恰好在DB_BLOCK_BUFFER中的数据量,对于一个良好的系统,绝大部分都就应当是这种情况。

逻辑读 = 一致读 + DB Block Gets 逻辑读就是从内存在读取的数据块数,物理读就是从硬盘读取到缓冲区的数据块数。


原创粉丝点击