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

Oracle学习之路【三】缓存表

=========================================================================

oracle 提供了BUFFER CACHE 的多缓冲池技术。
把 buffer cache 分为:default,keep ,recycle 。默认情况下所有表都在default下。

Oracle的db_buffer_pool由三部分组成
1. buffer_pool_default
2. buffer_pool_keep
3. buffer_pool_recycle
--表缓存
alter table ..... storage(buffer_pool keep);  
--查看哪些表被放在缓存区 但并不意味着该表已经被缓存
select table_name from dba_tables where buffer_pool='keep';
--查询到该表是否已经被缓存
select table_name,cache,buffer_pool from user_TABLES where cache like '%Y';
--已经加入到KEEP区的表想要移出缓存,使用
alter table table_name nocache;
--批量插入ORACLE建议用 insert all into ... insert into ...select 1 from dual;
--查询当前用户下表的情况
select table_name,cache,buffer_pool from user_TABLES;
--对于普通LOB类型的segment的cache方法
alter table t2 modify lob(c2) (storage (buffer_pool keep) cache);
--取消缓存
alter table test modify lob(address) (storage (buffer_pool keep) nocache);
--查询段
select segment_name,segment_type,buffer_pool from user_segments;
--对基于CLOB类型的对象的cache方法  
alter table lob1 modify lob(c1.xmldata) (storage (buffer_pool keep) cache); 
 
--查询该用户下所有表内的大字段情况
select column_name,segment_name from user_lobs;


SELECT  name,value   FROM   V$sysstat
    WHERE   name   in   ('dbblock gets','consistent gets','physical reads');
 通过查询结果命中率=1-physical   reads/(dbblock   gets+consistent   gets)   如果命中率<0.6~0.7,则应增大Db_block_buffers。


Oracle 10g中,与内存相关的参数可以归为两类: 

自动调优的SGA参数:包括DB_CACHE_SIZE、SHARED_POOL_SIZE、LARGE_POOL_SIZE和JAVA_POOL_SIZE。 

手动SGA参数:包括LOG_BUFFER、STREAMS_POOL、DB_NK_CACHE_SIZE、DB_KEEP_CACHE_SIZE和DB_RECYCLE_CACHE_SIZE。


查看用户表、索引、分区表占用空间

select segment_name, sum(bytes)/1024/1024 Mbytese from user_segments group by segment_name;

1、表占用空间
--查看某张表
SELECT SUM(BYTES)/1024/1024 M FROM USER_SEGMENTS WHERE SEGMENT_NAME = 'WORKINFO';

--查看该用户下所有表
select segment_name, sum(bytes)/1024/1024 Mbytese from user_segments where segment_type='TABLE' group by segment_name;

2、索引占用空间select segment_name ,sum(bytes)/1024/1024 from user_segments where segment_type ='INDEX' group by segment_name;

3、分区表TABLE PARTITION占用空间

select segment_name,sum(bytes)/1024/1024 Mbytes from user_segments where segment_type='TABLE PARTITION' group by segment_name;


v$db_object_cache 一般用于查询shared_pool,
数据缓冲区一般用V$BH或V$CACHE这两个数据字典.


块缓冲区包括:
          keep pool
          recycle pool
          default pool
      keep pool 和default pool的管理是一样的,都通过所谓的LRU机制管理。如果频繁地使用一个块,它就被高速缓存。如果由一段时间不接触某个块,它就可能被其他地块挤出缓冲区。只不过keep pool 的竞争只发生在以这个池为目标的段中;而回收池(recycle pool)里,块的老化与KEEP池中的不同。在KEEP池中,块是尽量保持,在RECYCLE池中块一旦不用,就马上老化掉。

 
ORACLE推荐使用
DB_KEEP_CACHE_SIZE = integer [K | M | G]
DB_RECYCLE_CACHE_SIZE = integer [K | M | G]
前后两者不能同时出现在init.ora中


oracle每行有一个初始序号


================
db_16k_cache_size big integer 0
db_2k_cache_size big integer 0
db_32k_cache_size big integer 0
db_4k_cache_size big integer 0
db_8k_cache_size big integer 0
db_cache_size big integer 956301312
db_keep_cache_size big integer 0
db_recycle_cache_size big integer 0
SQL> SELECT 128*1024*1024 FROM DUAL;

128*1024*1024
-------------
134217728

ALTER SYSTEM SET DB_CACHE_SIZE = 822083584;

ALTER SYSTEM SET DB_KEEP_CACHE_SIZE = 134217728;

========================
在Oracle的文档中有这样的解释:
db block gets:Number of times a CURRENT block was requested.
consistent gets:Number of times a consistent read was requested for a block.
physical reads:Total number of data blocks read from disk. This number equals the value of "physical reads direct" plus all reads into buffer cache.
---------------------------------------------
针对以上3个概念进行的说明解释及关系如下:
1、DB Block Gets(当前请求的块数目)
当前模式块意思就是在操作中正好提取的块数目,而不是在一致性读的情况下而产生的块数。正常的情况下,一个查询提取的块是在查询开始的那个时间点上存在的数据块,当前块是在这个时刻存在的数据块,而不是在这个时间点之前或者之后的数据块数目。

2、Consistent Gets(数据请求总数在回滚段Buffer中的数据一致性读所需要的数据块)
这里的概念是在处理你这个操作的时候需要在一致性读状态上处理多少个块,这些块产生的主要原因是因为由于在你查询的过程中,由于其他会话对数据块进行操作,而对所要查询的块有了修改,但是由于我们的查询是在这些修改之前调用的,所以需要对回滚段中的数据块的前映像进行查询,以保证数据的一致性。这样就产生了一致性读。

3、Physical Reads(物理读)
就是从磁盘上读取数据块的数量,其产生的主要原因是:
1、 在数据库高速缓存中不存在这些块
2、 全表扫描
3、 磁盘排序

它们三者之间的关系大致可概括为: