分页和buffer_cache关系

今天老大说分页时候会不会cache select语句的信息,然后再到下一页会不会快点,我说不会,我做个试验给他看看
首先清理内存中信息(重启数据库或者手动情况都一样)

alter system flush buffer_cache;

做一个10046时间,观察trace内容

set linesize 200
set timing on
alter session set tracefile_identifier='fenyetest0';
alter session set events '10046 trace name context forever,level 4';
variable start_num  number;
variable end_num number;

--1. 10000-2000数据
exec :start_num := 10000;
exec :end_num := 20000;

select *
       from (
             select rownum rn,t.*
             from
             (select  OPERATE_TIME,SERVER_INFO ,ACCESS_IP ,USERID from bi_logfile where    OPERATE_TIME>=(sysdate-10)) t
             where rownum<:end_num)
       where rn >:start_num;  

--查看buffer_cache中的内容
SQL> SELECT o.OBJECT_NAME, COUNT(*) NUMBER_OF_BLOCKS
     FROM DBA_OBJECTS o, V$BH bh
    WHERE o.DATA_OBJECT_ID = bh.OBJD
      AND o.OWNER        ='JSCNBI'
    GROUP BY o.OBJECT_NAME
    ORDER BY COUNT(*);   

OBJECT_NAME                              NUMBER_OF_BLOCKS
---------------------------------------- ----------------
BI_LOGFILE_IDX1                                       128
BI_LOGFILE                                           1160

--2. 20000-3000数据
exec :start_num := 20000;
exec :end_num := 30000;
select *
       from (
             select rownum rn,t.*
             from
             (select  OPERATE_TIME,SERVER_INFO ,ACCESS_IP ,USERID from bi_logfile where    OPERATE_TIME>=(sysdate-10)) t
             where rownum<:end_num)
       where rn >:start_num;  

--查看buffer_cache中的内容
SQL> SELECT o.OBJECT_NAME, COUNT(*) NUMBER_OF_BLOCKS
     FROM DBA_OBJECTS o, V$BH bh
    WHERE o.DATA_OBJECT_ID = bh.OBJD
      AND o.OWNER        ='JSCNBI'
    GROUP BY o.OBJECT_NAME
    ORDER BY COUNT(*);     2    3    4    5    6

OBJECT_NAME                              NUMBER_OF_BLOCKS
---------------------------------------- ----------------
BI_LOGFILE_IDX1                                       197
BI_LOGFILE                                           1665

--3. 30000-40000数据
exec :start_num := 30000;
exec :end_num := 40000;
select *
       from (
             select rownum rn,t.*
             from
             (select  OPERATE_TIME,SERVER_INFO ,ACCESS_IP ,USERID from bi_logfile where    OPERATE_TIME>=(sysdate-10)) t
             where rownum<:end_num)
       where rn >:start_num;  

--查看buffer_cache中的内容
SQL> SELECT o.OBJECT_NAME, COUNT(*) NUMBER_OF_BLOCKS
     FROM DBA_OBJECTS o, V$BH bh
    WHERE o.DATA_OBJECT_ID = bh.OBJD
      AND o.OWNER        ='JSCNBI'
    GROUP BY o.OBJECT_NAME
    ORDER BY COUNT(*);  2    3    4    5    6

OBJECT_NAME                              NUMBER_OF_BLOCKS
---------------------------------------- ----------------
BI_LOGFILE_IDX1                                       222
BI_LOGFILE                                           1983


alter session set events '10046 trace name context off';


格式化trace内容,格式的时候不合并相同的sql,也不查看sys sql
[oracle@DB-87 udump]$ tkprof jscnbi_ora_27002_fenyetest0.trc  fenyetest0.sql sys=no aggregate=no

格式化以后的结果如下:
TKPROF: Release 10.2.0.4.0 - Production on Fri Jul 13 13:57:48 2012

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

Trace file: jscnbi_ora_27002_fenyetest0.trc
Sort options: default

********************************************************************************
count    = number of times OCI procedure was executed
cpu      = cpu time in seconds executing
elapsed  = elapsed time in seconds executing
disk     = number of physical reads of buffers from disk
query    = number of buffers gotten for consistent read
current  = number of buffers gotten in current mode (usually for update)
rows     = number of rows processed by the fetch or execute call
--------------------------------------------------------------------------------

*** SESSION ID:(309.11) 2012-07-13 13:56:17.864

********************************************************************************

BEGIN :start_num := 10000; END;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           1

Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 59  
********************************************************************************

BEGIN :end_num := 20000; END;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           1

Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 59  
********************************************************************************

select *
       from (
             select rownum rn,t.*
             from
             (select  OPERATE_TIME,SERVER_INFO ,ACCESS_IP ,USERID from bi_logfile where    OPERATE_TIME>=(sysdate-10)) t
             where rownum<:end_num)
       where rn >:start_num

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch      668      0.12       0.09       1274      11081          0        9999
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total      670      0.12       0.10       1274      11081          0        9999

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 59  

Rows     Row Source Operation
-------  ---------------------------------------------------
   9999  VIEW  (cr=11081 pr=1274 pw=0 time=84845 us)
  19999   COUNT STOPKEY (cr=11081 pr=1274 pw=0 time=81070 us)
  19999    TABLE ACCESS BY INDEX ROWID BI_LOGFILE (cr=11081 pr=1274 pw=0 time=81068 us)
  19999     INDEX RANGE SCAN BI_LOGFILE_IDX1 (cr=743 pr=127 pw=0 time=676 us)(object id 52405)

********************************************************************************

BEGIN :start_num := 20000; END;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           1

Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 59  
********************************************************************************

BEGIN :end_num := 30000; END;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           1

Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 59  
********************************************************************************

select *
       from (
             select rownum rn,t.*
             from
             (select  OPERATE_TIME,SERVER_INFO ,ACCESS_IP ,USERID from bi_logfile where    OPERATE_TIME>=(sysdate-10)) t
             where rownum<:end_num)
       where rn >:start_num

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch      668      0.15       0.11        586      16100          0        9999
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total      670      0.15       0.11        586      16100          0        9999

Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 59  

Rows     Row Source Operation
-------  ---------------------------------------------------
   9999  VIEW  (cr=16100 pr=586 pw=0 time=111363 us)
  29999   COUNT STOPKEY (cr=16100 pr=586 pw=0 time=120067 us)
  29999    TABLE ACCESS BY INDEX ROWID BI_LOGFILE (cr=16100 pr=586 pw=0 time=90064 us)
  29999     INDEX RANGE SCAN BI_LOGFILE_IDX1 (cr=781 pr=78 pw=0 time=74 us)(object id 52405)

********************************************************************************

BEGIN :start_num := 30000; END;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           1

Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 59  
********************************************************************************

BEGIN :end_num := 40000; END;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           1

Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 59  
********************************************************************************

select *
       from (
             select rownum rn,t.*
             from
             (select  OPERATE_TIME,SERVER_INFO ,ACCESS_IP ,USERID from bi_logfile where    OPERATE_TIME>=(sysdate-10)) t
             where rownum<:end_num)
       where rn >:start_num

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch      668      0.13       0.11        345      20964          0        9999
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total      670      0.13       0.11        345      20964          0        9999

Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 59  

Rows     Row Source Operation
-------  ---------------------------------------------------
   9999  VIEW  (cr=20964 pr=345 pw=0 time=109073 us)
  39999   COUNT STOPKEY (cr=20964 pr=345 pw=0 time=120041 us)
  39999    TABLE ACCESS BY INDEX ROWID BI_LOGFILE (cr=20964 pr=345 pw=0 time=120035 us)
  39999     INDEX RANGE SCAN BI_LOGFILE_IDX1 (cr=817 pr=16 pw=0 time=109 us)(object id 52405)

********************************************************************************

alter session set events '10046 trace name context off'


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           0

Misses in library cache during parse: 0
Parsing user id: 59  



********************************************************************************

OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse       10      0.00       0.00          0          0          0           0
Execute     10      0.01       0.01          0          0          0           6
Fetch     2004      0.41       0.32       2205      48145          0       29997
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total     2024      0.42       0.34       2205      48145          0       30003

Misses in library cache during parse: 7
Misses in library cache during execute: 6


OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse       15      0.00       0.00          0          0          0           0
Execute    220      0.06       0.06          0          0          0           0
Fetch      236      0.01       0.01         48        699          0        2077
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total      471      0.08       0.08         48        699          0        2077

Misses in library cache during parse: 13
Misses in library cache during execute: 13

   10  user  SQL statements in session.
  220  internal SQL statements in session.
  230  SQL statements in session.
********************************************************************************
Trace file: jscnbi_ora_27002_fenyetest0.trc
Trace file compatibility: 10.01.00
Sort options: default

       1  session in tracefile.
      10  user  SQL statements in trace file.
     220  internal SQL statements in trace file.
     230  SQL statements in trace file.
      21  unique SQL statements in trace file.
    7288  lines in trace file.
      35  elapsed seconds in trace file.


      
从上面的结果看出,1、2、3次分页过程Fetch disk中内容并没有减少。

后来想想其实根本没有必要这么麻烦,我们只要取0行数据,看buffer_cache有没有就ok了,
--取0行数据
select *
       from (
             select rownum rn,t.*
             from
             (select  OPERATE_TIME,SERVER_INFO ,ACCESS_IP ,USERID from bi_logfile where    OPERATE_TIME>=(sysdate-10)) t
             where rownum<2)
       where rn >1;
--查看buffer cache中有没有bi_logfile表的数据

SQL> SELECT o.OBJECT_NAME, COUNT(*) NUMBER_OF_BLOCKS
     FROM DBA_OBJECTS o, V$BH bh
    WHERE o.DATA_OBJECT_ID = bh.OBJD
      AND o.OWNER        ='JSCNBI'
    GROUP BY o.OBJECT_NAME
    ORDER BY COUNT(*); 


结果里面是没有的,说明我的推断是正确的。       

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值