db buffer cache

数据库缓冲缓存  Database Buffer Cache
 是SGA组件中内存需求最大的
 从OS角度来讲数据是以块为单位存放在磁盘上的
 而oracle是依附于OS的,它也是以块进行存储的,但oracle中描述的块大小 通常是OS块大小的倍数
 例如:linux ext3大100M分区默认4K
      oracle的数据以文件方式存放在FS上就是4K的N倍 由db_block_size参数决定
 倘若每次获取数据信息都要从数据文件中加载这些块 那IO路程花费太大了
 于是oracle为这些块能在内存中尽量长久保存 而开辟了这个DB BUFFER CACHE区域
 这个区域的管理也是使用LRU算法 存放最近最常使用的磁盘数据块的副本
以前描述的例子:
  你已经和导购(电话销售员)建立了连接
  接下来你告知你需要的物品
  如果柜台里有,直接拿给你
  柜台里没有就去库存给你找货
 柜台 就是内存(准确的说是db buffer cache)
 库存 就是database(准确的说是数据文件)
 
Buffer cache里面有两个链表来管理 LRU和WLRU:
Write list:持有那些脏buffer,包含已经被修改的数据,但是还未被写入到disk
LRU list:里面有free buffer, pinned buffer , 以及还没被移到write list的dirty buffer。

db buffer cache内部又分为三部分:
 keep pool
 recycle pool
 default pool
keep和recycle是可选的
default pool:
 所有段块一般都缓存在这个池中
keep pool:
 对于访问相对频繁的段会放在这个池中,因为如果把这些段放在default pool中,尽管会频繁访问
 但也可以因为其他段需要空间而使其被挤出内存.
recyle pool:
 访问不频繁,例如全表扫描的产生大量数据
 这些数据并不会再以后被使用,要把这些数据装入default pool就会把常用的数据排挤出内存
 所以oracle开辟了 recyle pool,这样就不会导致default pool中的数据被这些全表扫描等语句的数据排挤
 
 
数据默认是放在default pool的
其他池的使用方法是建表时或改表的storage参数指明
create table TAB_NAME (n number) storage (buffer_pool keep);
alter table TAB_NAME storage (buffer_pool recycle);

查看buffer_cache中的缓存对象 要使用v$bh视图  bh既是buffer header的缩写
SQL> SELECT o.object_name, COUNT(1) number_of_blocks
   FROM DBA_OBJECTS o, V$BH bh                  
  WHERE o.object_id  = bh.objd                  
    AND o.owner     != 'SYS'                    
  GROUP BY o.object_name                        
  ORDER BY count(1);
OBJECT_NAME         NUMBER_OF_BLOCKS
------------------------------ ----------------
AQ$_QUEUE_TABLES_PRIMARY        1
DEF$_AQCALL          1
SYS_IOT_TOP_49762         1
AQ$_QUEUES_CHECK         1
AQ$_QUEUE_TABLES         1
REPCAT$_REPPROP          1
XDB$SCHEMA_URL          1
RLM$SCHACTIONORDER         1
DEF$_AQERROR          1
SYS_IOT_TOP_10095         1
SQLPLUS_PRODUCT_PROFILE         1
RLM$JOINQKEY          1
AQ$_QUEUES          2
XDB$CONFIG          6
EMP           7
SQL>
SQL> select * from scott.dept;
    DEPTNO DNAME   LOC
---------- -------------- -------------
 10 ACCOUNTING   NEW YORK
 20 RESEARCH   DALLAS
 30 SALES   CHICAGO
 40 OPERATIONS   BOSTON
SQL> SELECT o.object_name, COUNT(1) number_of_blocks
   FROM DBA_OBJECTS o, V$BH bh                  
  WHERE o.object_id  = bh.objd                  
    AND o.owner     != 'SYS'                    
  GROUP BY o.object_name                        
  ORDER BY count(1);
OBJECT_NAME         NUMBER_OF_BLOCKS
------------------------------ ----------------
AQ$_QUEUE_TABLES_PRIMARY        1
DEF$_AQCALL          1
AQ$_QUEUES_CHECK         1
AQ$_QUEUE_TABLES         1
REPCAT$_REPPROP          1
RLM$JOINQKEY          1
SYS_IOT_TOP_49762         1
XDB$SCHEMA_URL          1
RLM$SCHACTIONORDER         1
DEF$_AQERROR          1
SYS_IOT_TOP_10095         1
SQLPLUS_PRODUCT_PROFILE         1
AQ$_QUEUES          2
XDB$CONFIG          6
DEPT           6
EMP           7
16 rows selected.
SQL>
查看有多少脏块和非脏块
 select dirty, count(*) from v$bh group by dirty;
SQL> select dirty, count(*) from v$bh group by dirty;
D   COUNT(*)
- ----------
Y   60
N 5326
SQL> alter system flush buffer_cache;
System altered.
SQL> select dirty, count(*) from v$bh group by dirty;
D   COUNT(*)
- ----------
N 5386
SQL> update scott.emp set sal=sal+1;
14 rows updated.
SQL> select dirty, count(*) from v$bh group by dirty;
D   COUNT(*)
- ----------
N 5386
SQL> commit;
Commit complete.
SQL> select dirty, count(*) from v$bh group by dirty;
D   COUNT(*)
- ----------
Y    3
N 5383
SQL> alter system checkpoint;
System altered.
SQL> select dirty, count(*) from v$bh group by dirty;
D   COUNT(*)
- ----------
N 5386
SQL>

keep表实验  即使是自动管理 oracle也不会为我们管理keep buffer
SQL> select component,current_size from v$sga_dynamic_components where component='KEEP buffer cache';
COMPONENT      CURRENT_SIZE
-------------------- ------------
KEEP buffer cache  0
SQL>
查看参数值默认是0 就代表没开启keep功能 设置一个值就开启了
SQL> show parameter db_keep_cache_size
NAME         TYPE  VALUE
------------------------------------ ----------- ------------------------------
db_keep_cache_size       big integer 0
SQL>
开启KEEP池,空间来自于default buffer
SQL> select component,current_size from v$sga_dynamic_components where component in  ('DEFAULT buffer cache','KEEP buffer cache');
COMPONENT      CURRENT_SIZE
-------------------- ------------
DEFAULT buffer cache 205520896
KEEP buffer cache  0
SQL> alter system set db_keep_cache_size=20M;
System altered.
SQL> select component,current_size from v$sga_dynamic_components where component in  ('DEFAULT buffer cache','KEEP buffer cache');
COMPONENT      CURRENT_SIZE
-------------------- ------------
DEFAULT buffer cache 184549376
KEEP buffer cache  20971520
SQL>
创建T1表 附加 storage(buffer_pool keep)
SQL> drop table t1 purge;
Table dropped.
SQL> create table t1 storage (buffer_pool keep) as select * from dba_objects;
Table created.
SQL> select table_name,cache,blocks from dba_tables where buffer_pool='KEEP';
TABLE_NAME    CACHE      BLOCKS
------------------------- -------------------- ----------
T1         N
SQL>
将表变成使用状态的标志 这里只是个标志 为了以后方便查询哪些表是CACHE在BUFFER的一个查询标志
SQL> alter table t1 cache;
Table altered.
SQL> select table_name,cache,blocks from dba_tables where buffer_pool='KEEP';
TABLE_NAME    CACHE      BLOCKS
------------------------- -------------------- ----------
T1         Y
查看当前KEEP池的使用情况:
SQL> select p.name,a.cnum_repl "total buffers",a.anum_repl "free buffers" from x$kcbwds a, v$buffer_pool p where a.set_id=p.LO_SETID and     p.name='KEEP';
NAME         total buffers free buffers
-------------------------------------------------- ------------- ------------
KEEP           1497  1497
SQL> insert into t1 select * from t1;
SQL> select p.name,a.cnum_repl "total buffers",a.anum_repl "free buffers" from x$kcbwds a, v$buffer_pool p where a.set_id=p.LO_SETID and     p.name='KEEP';
NAME         total buffers free buffers
-------------------------------------------------- ------------- ------------
KEEP           1497  1491
SQL> alter system set db_keep_cache_size=0;
System altered.
SQL> select p.name,a.cnum_repl "total buffers",a.anum_repl "free buffers" from x$kcbwds a, v$buffer_pool p where a.set_id=p.LO_SETID and     p.name='KEEP';
no rows selected
SQL>
SQL> select p.name,a.cnum_repl "total buffers",a.anum_repl "free buffers" from x$kcbwds a, v$buffer_pool p where a.set_id=p.LO_SETID and     p.name='DEFAULT';
NAME         total buffers free buffers
-------------------------------------------------- ------------- ------------
DEFAULT          19960 19907
SQL> alter system set db_keep_cache_size=0;
System altered.
SQL> select p.name,a.cnum_repl "total buffers",a.anum_repl "free buffers" from x$kcbwds a, v$buffer_pool p where a.set_id=p.LO_SETID and     p.name='DEFAULT';
NAME         total buffers free buffers
-------------------------------------------------- ------------- ------------
DEFAULT          21457 21404
SQL>
SQL> select component,current_size from v$sga_dynamic_components where component in  ('DEFAULT buffer cache','KEEP buffer cache');
COMPONENT      CURRENT_SIZE
-------------------- ------------
DEFAULT buffer cache 205520896
KEEP buffer cache  0
SQL>
 
SQL> show parameter cache_size
NAME         TYPE  VALUE
------------------------------------ ----------- ------------------------------
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 0
db_keep_cache_size       big integer 0
db_recycle_cache_size       big integer 0
SQL>
Oracle 9i以后还提供了可以设置多种数据块尺寸(2、4、8、16 或 32)的buffer cache,
以便存放不同数据块尺寸的表空间中的对象。
使用初始化参数db_Nk_cache_size来指定不同数据块尺寸的buffer cache,
这里的N就是2、4、8、16 或 32。创建数据库时,
使用初始化参数db_block_size所指定默认的标准数据块尺寸,标准数据块尺寸用于system表空间。
然后可以指定最多4个不同的、非标准数据块尺寸的表空间。
每种数据块尺寸的表空间必须对应一种不同尺寸的buffer cache,也就是说对应块大小的池要先设置好,否则不能创建不同数据块尺寸的表空间。
SQL> alter system set db_16k_cache_size=10M;
SQL> create tablespace tbs_test_16k datafile '/u01/oracle/oradata/ora10g/tbs_test_16k.dbf' size 10M  blocksize 16k;

查看DB_buffer_cache大小
SQL> SELECT COMPONENT,CURRENT_SIZE FROM V$SGA_DYNAMIC_COMPONENTS where COMPONENT like '%buffer cache';
COMPONENT         CURRENT_SIZE
------------------------------ ------------
DEFAULT buffer cache    205520896
KEEP buffer cache     0
RECYCLE buffer cache     0
DEFAULT 2K buffer cache     0
DEFAULT 4K buffer cache     0
DEFAULT 8K buffer cache     0
DEFAULT 16K buffer cache    0
DEFAULT 32K buffer cache    0
8 rows selected.
Elapsed: 00:00:00.01
SQL>

如何设置db_buffer_cache大小
SQL> show parameter advice
NAME         TYPE  VALUE
------------------------------------ ----------- ------------------------------
db_cache_advice        string  ON
select size_for_estimate, estd_physical_read_factor,estd_physical_reads
from v$db_cache_advice
where name = 'DEFAULT';
SIZE_FOR_ESTIMATE ESTD_PHYSICAL_READ_FACTOR ESTD_PHYSICAL_READS
----------------- ------------------------- -------------------
        16       3.0119    23164
        32       1.2479     9597
        48       1.0781     8292
        64     1     7691
        80     1     7691
        96     1     7691
       112     1     7691
       128     1     7691
       144     1     7691
       160     1     7691
       176     1     7691
       192     1     7691
       196     1     7691
       208     1     7691
       224     1     7691
       240     1     7691
       256     1     7691
       272     1     7691
       288     1     7691
       304     1     7691
       320     1     7691
21 rows selected.
Elapsed: 00:00:00.02
SQL>
ESTD = estimated缩写 评估
这里的字段estd_physical_read_factor表示在相应buffer cache的尺寸(由字段size_for_estimate表示 M单位),
根据oracle的建议 我们看到设置到64M后因子FACTOR就是1,以后加大也不会有性能提升(因为是测试库,所以这么小)

10G自动管理设置好 SGA_TARGET 即可
手动管理(9i后)使用 db_cache_size 参数来管理大小
SQL> show parameter db_cache_size
NAME         TYPE  VALUE
------------------------------------ ----------- ------------------------------
db_cache_size        big integer 0
SQL>

数据块缓冲缓存区(Data block buffers cache):
  Data buffers在8i中是Db_block_buffers*Db_block_size,
  9i及以后版本用Db_cache_size来代替这个参数
  10G中,这个参数是由oracle本身自动管理的,可以不用手工设置.
  10g手动管理依旧使用db_cache_size设置
 

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24756465/viewspace-717612/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/24756465/viewspace-717612/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值