暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

Oracle buffer cache

bjgzxx 2023-12-22
206



一. Keep Pool 说明

在我之前的Blog里对DB buffer 进行了一个说明,参考:

Oracle Buffer Cache 原理

http://www.cndba.cn/Dave/article/1398



Oracle的buffer cache 由三个部分组成: default pool,keep pool 和Recycle pool. 每个Pool 都有自己的LRU来管理.

(1)The default pool is for everything else.

(2)The recycle pool is for larger objects.

(3)The keep pool's purpose is to take small objects that shouldalways be cached, for example Look Up Tables.



在这篇文章里我们重点看一下KeepPool的说明。



1.1 相关理论知识

1.1.1 BUFFER_POOL
The BUFFER_POOL clause letsyou specify a default buffer pool (cache) for aschema object. All blocks for the object are stored in the specified cache.
-- 缓冲池子句可指定一个数据库对象的默认缓冲池。这个对象的所有数据块存储在指定的缓存中。

If you define a buffer poolfor a partitioned table or index, then the partitions inherit the buffer poolfrom the table or index definition, unless overridden by a partition-leveldefinition.
--如果给一个分区表或索引指定了缓冲池,那么该表或索引的分区也同样使用指定的缓冲池,除非在分区的定义中指定分区使用的缓冲池。

For an index-organized table,you can specify a buffer pool separately for the index segment and the overflowsegment.
--对于一个索引组织表,可以为索引段和溢出段分别指定缓冲池。

(1)Restrictionson BUFFER_POOL
You cannot specify this clausefor a cluster table. However, you can specify it for a cluster.
--不能在聚集表上指定缓冲池,但是,可以在一个聚集上指定缓冲池。

You cannot specify this clausefor a tablespace or for a rollback segment.
--不能在表空间或回滚段上指定缓冲池。

(2)KEEP
Specify KEEP to put blocksfrom the segment into the KEEP buffer pool. Maintaining an appropriately sizedKEEP buffer pool lets Oracle retain the schema object in memory to avoid I/Ooperations. KEEP takes precedence over any NOCACHEclause you specify for a table, cluster, materialized view, ormaterialized view log.
--指定KEEP将把数据块放入KEEP缓冲池中。维护一个适当尺寸的KEEP缓冲池可以使Oracle在内存中保留数据库对象而避免I/O操作。在表、聚集、实体化视图或实体化视图日志上,KEEP子句的优先权大于NOCACHE子句。

(3)RECYCLE
Specify RECYCLE to put blocksfrom the segment into the RECYCLE pool. An appropriately sized RECYCLE poolreduces the number of objects whose default pool is the RECYCLE pool fromtaking up unnecessary cache space.
--指定RECYCLE将把数据块放入RECYCLE缓冲池中。一个适当尺寸的RECYCLE缓冲池可以减少默认缓冲池为RECYCLE缓冲池的数据库对象的数量,以避免它们占用不必要的缓冲空间。

(4) DEFAULT
Specify DEFAULT to indicatethe default buffer pool. This is the default for objects not assigned to KEEPor RECYCLE.
--指定DEFAULT将适用默认的缓冲池。这个选项适用于没有分配给KEEP缓冲池和RECYCLE缓冲池的其它数据库对象。

(5)buffer pool 说明
在没有多个缓冲池的数据库中,所有的数据库对象使用同样的缓冲池,这样就会形成一种情况:
当希望某个频繁使用的数据库对象一直保留在缓冲池中时,一个大的、不经常使用的数据库对象会把它“挤”出缓冲池。这样就会降低缓冲池的效率,增加额外的I/O操作。
使用多个缓冲池后,可以更精确的调整缓冲池的使用,频繁使用数据库对象的缓冲池放在KEEP缓冲池中,大的、不经常使用的数据库对象放在RECYCLE缓冲池中,其它的数据库对象放在DEFAULT缓冲池中。



1.1.2 CACHE | NOCACHE | CACHE READS
Use the CACHE clauses toindicate how Oracle should store blocks in the buffer cache. If you specifyneither CACHE nor NOCACHE:
--使用CACHE子句可制定Oracle在缓冲中如何存贮数据块。如果没有指定CACHE或NOCACHE:



In a CREATE TABLE statement, NOCACHE is thedefault
In an ALTER TABLE statement, the existing value is not changed.
--在CREATETABLE语句中,默认为NOCACHE。
--在ALTER TABLE语句中,不会改变当前表的CACHE/NOCACHE值。

(1)CACHE Clause
For data that is accessedfrequently, this clause indicates that the blocks retrieved for this table are placedat the most recently used end of the least recentlyused (LRU) list in the buffer cache when a full table scan is performed.This attribute is useful for small lookup tables.

--对于那些访问频繁的数据,这个子句可以指定当执行一个全表扫描时,将从表中获取的数据块放在缓冲中LRU列表的"最新使用"的一端。这个属性对小的查找表有用。

As a parameter in theLOB_storage_clause, CACHE specifies that Oracle places LOB data values in thebuffer cache for faster access.
--作为LOB存储子句的一个参数,CACHE可指定Oracle将LOB数据放在缓冲中,以便访问得更快。

(2)Restriction on CACHE
You cannot specify CACHE foran index-organized table. However, index-organized tables implicitly provideCACHE behavior.
--不能在索引组织表上使用CACHE。但是,索引组织表隐式的提供了CACHE的效果。

(3)NOCACHE Clause
For data that is not accessedfrequently, this clause indicates that the blocks retrieved for this table areplaced at the least recently used end of the LRU listin the buffer cache when a full table scan is performed.
--对于那些访问不频繁的数据,这个子句可以指定当执行一个全表扫描时,将从表中获取的数据块放在缓冲中LRU列表的“最久使用”的一端。

As a parameter in theLOB_storage_clause, NOCACHE specifies that the LOB value is either not broughtinto the buffer cache or brought into the buffer cache and placed at the leastrecently used end of the LRU list. (The latter is the default behavior.)
--作为LOB存储子句的一个参数,NOCACHE可指定Oracle将LOB数据不放在缓冲中或者放在缓冲中LRU列表的“最久使用”的一端。(后者是默认的做法。)

(4)Restriction on NOCACHE
You cannot specify NOCACHE forindex-organized tables.
--不能在索引组织表上使用CACHE。

(5)CACHE READS
CACHE READS applies only toLOB storage. It specifies that LOB values are brought into the buffer cacheonly during read operations, but not during write operations.
--CACHEREADS只适用于LOB存储。它指定当进行读操作时,将LOB放在缓冲中,而写操作时不这样做。

(6)说明
当BUFFER_POOL和CACHE同时使用时,KEEP比NOCACHE有优先权。
BUFFER_POOL用来指定存贮的缓冲池,而CACHE/NOCACHE指定存储的方式。



1.2 官网说明

Ifthere are certain segments in your application that are referenced frequently,then store the blocks from those segments in a separate cache called the KEEPbuffer pool. Memory is allocated to the KEEP buffer pool by setting theparameter DB_KEEP_CACHE_SIZE to the required size. Thememory for the KEEP pool is not a subset of the default pool. Typical segmentsthat can be kept are small reference tables that are used frequently.Application developers and DBAs can determine which tables are candidates.

Youcan check the number of blocks from candidate tables by querying V$BH, asdescribed in "DeterminingWhich Segments Have Many Buffers in the Pool".



Note:

The NOCACHE clause has no effect on a table inthe KEEP cache.

--对象放到Keep Pool中,必需将这个对象缓存,因此Oracle对所有KEEP池中的对象采用了默认CACHE的方式。而忽略对象本身的CACHE和NOCACHE选项。



Thegoal of the KEEP buffer pool is toretain objects in memory, thus avoiding I/O operations. The size of the KEEPbuffer pool, therefore, depends on the objects to be kept in the buffer cache.You can compute an approximate size for the KEEP buffer pool by adding theblocks used by all objects assigned to this pool. Ifyou gather statistics on the segments, you can query DBA_TABLES.BLOCKS and DBA_TABLES.EMPTY_BLOCKSto determine the number of blocks used.

Calculatethe hit ratio by taking two snapshots of system performance at different times,using the previous query. Subtract the more recent values for physical reads, blockgets, and consistent gets from the older values, and use the results to computethe hit ratio.

Abuffer pool hit ratio of 100% might not be optimal. Often, you can decrease thesize of your KEEP buffer pool and still maintain a sufficiently high hit ratio.Allocate blocks removed from the KEEP buffer pool to other buffer pools.



Note:

Ifan object grows in size, then it might no longer fit in the KEEP buffer pool.You will begin to lose blocks out of the cache.

Each object kept in memory results in a trade-off. It is beneficial to keep frequently-accessed blocks in thecache, but retaining infrequently-used blocks results in less space forother, more active blocks.



From: http://download.oracle.com/docs/cd/E11882_01/server.112/e16638/memory.htm#PFGRF94285

1.3 Keep Buffer Pool 说明

KeepBuffer Pool 的作用是缓存那些需要经常查询的对象但又容易被默认缓冲区置换出去的对象,按惯例,Keep pool设置为合理的大小,以使其中存储的对象不再age out,也就是查询这个对象的操作不会引起磁盘IO操作,可以极大地提高查询性能。



注意一点,不是设置了keep pool 之后,热点表就一定能够缓存在 keep pool ,keep pool 同样也是由LRU 链表管理的,当keep pool 不够的时候,最先缓存到 keep pool 的对象会被挤出,不过与default pool 中的 LRU 的管理方式不同,在keep pool 中表永远是从MRU 移动到LRU,不会由于你做了FTS(全表扫描)而将表缓存到LRU端,在keep pool中对象永远是先进先出。



因为这个原因,如果keep pool 空间比table 小,导致不能完全把table keep下,那么在keep pool 中最早使用的数据还是有可能被清洗出去的。还是会产生大量的逻辑读,这样就起不到作用,所以,如果采用keep,就必须全部keep下,要么就不用keep。

关于altertable cache, 全表扫描数据在正常情况下是放到LRU的冷端,使其尽快page out(这是default pool的默认策略), 而指定了alter table cache后,该表的全表扫描数据就不是放到LRU的冷端, 而是放到热端(MRU)了,从而使该得数据老化较慢,即保留的时间长.
但对于keep pool来说, 默认策略并不相同,所有数据总是放到热端的, 包括全表扫描数据。



在Oracle 10g中SGA自动管理,ORACLE并不会管理keep pool,ORACLE只会管理default pool。



默认的情况下db_keep_cache_size=0,未启用,如果想要启用,需要手工设置db_keep_cache_size的值,设置了这个值之后 db_cache_size 会减少。


文章转载自bjgzxx,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论