手动刷新Buffer Cache主要的原因是为了排除Cache对于测试的影响,以此促使Oracle重新执行物理访问。以下为具体的示例参考:
◆Oracle 9i里,Oracle提供了一个内部事件,用来强制刷新Buffer Cache,它的语法如下:
alter session set events 'immediate trace name flush_cache level 1';
或者如下所示:
alter session set events = 'immediate trace name flush_cache';
◆同样我们也可以使用alter system系统级来设置。
alter system set events = 'immediate trace name flush_cache';
◆ Oracle 10g里,Oracle提供一个新的特性,可以通过以下的命令来刷新Buffer Cache:
alter system flush buffer_cache;
下面这个示例演示了刷新Cache后的作用:
(1)首先我们来创建测试表
SQL> create table t as select * from dba_objects;
Table created.
SQL> analyze table t compute statistics;
Table analyzed.
SQL> select blocks,empty_blocks from dba_tables
2 where table_name='T' and owner='SYS';
BLOCKS EMPTY_BLOCKS---------- ------------
78 1
表T共有79个Block.
(2)x$bh
SQL> select count(*) from x$bh;
COUNT(*)
----------
14375SQL>
select count(*) from x$bh where state=0;
-- state =0 is free
COUNT(*)
----------
13960SQL>
alter system set events = 'immediate trace name flush_cache';
System altered.
SQL> select count(*) from x$bh where state=0;
COUNT(*)
----------
14375
在这里你可以发现flush_cache以后,所有的Buffer都被标记为free。
(3)最后请留意flush_cache对于查询的影响
SQL> set autotrace trace stat
SQL> select count(*) from t;
Statistics
----------------------------------
0 recursive calls
0 db block gets
81 consistent gets
79 physical reads
0 redo size
....
SQL>
SQL> select count(*) from t;
Statistics-----------------------
0 recursive calls
0 db block gets
81 consistent gets
0 physical reads
0 redo size
....
SQL> alter system set events = 'immediate trace name flush_cache';
System altered.SQL> select count(*) from t;
Statistics
---------------------------------- 0 recursive calls
0 db block gets
81 consistent gets
79 physical reads
0 redo size
....
SQL>