[20111229]Consistent gets from cache (fastpath).txt

lfree發表於2011-12-29
[20111229]Consistent gets from cache (fastpath).txt

SQL> select * from v$version ;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE    11.2.0.1.0      Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

SQL> select * from v$statname where NAME like 'consistent gets%';
STATISTIC# NAME                                                                  CLASS    STAT_ID
---------- ---------------------------------------------------------------- ---------- ----------
        67 consistent gets                                                           8            4162191256
        68 consistent gets from cache                                      8            2839918855
        69 consistent gets from cache (fastpath)                    8            2173751438
        70 consistent gets - examination                                  8            1701530557
        71 consistent gets direct                                                 8            420374750

如果對比10g可以發現,11G增加一個consistent gets from cache (fastpath)統計,google發現如下連結:

http://afatkulin.blogspot.com/2009/01/consistent-gets-from-cache-fastpath.html

1.重複測試:
create table t1 nologging as select rownum id ,a.* from all_objects a where rownum <=10000;
create table t2 nologging as select * from t1;
create index idx_t1_id on t1(id);
exec dbms_stats.gather_table_stats(user,'T1');
exec dbms_stats.gather_table_stats(user,'T2');

2.在11GR2下執行如下:
SQL> select b.name,a.value from v$mystat a, v$statname b where a.STATISTIC#=b.STATISTIC# and b.NAME like 'consistent gets%';
NAME                                                                           VALUE
---------------------------------------------------------------- ----------
consistent gets                                                              37
consistent gets from cache                                         37
consistent gets from cache (fastpath)                      33
consistent gets - examination                                      4
consistent gets direct                                                     0

SQL> select/*+use_nl(t2 t1)*/ count(*) from t1,t2 where t1.id = t2.id;
  COUNT(*)
----------
     10000

10:26:20 SQL> select b.name,a.value from v$mystat a, v$statname b where a.STATISTIC#=b.STATISTIC# and b.NAME like 'consistent gets%';
NAME                                                                            VALUE
---------------------------------------------------------------- ----------
consistent gets                                                          324(+287)
consistent gets from cache                                     324(+287)
consistent gets from cache (fastpath)                   298(+265)
consistent gets - examination                                        5(+1)
consistent gets direct                                                       0

執行計劃如下:
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  518mw211s3mmv, child number 0
-------------------------------------
select/*+use_nl(t2 t1)*/ count(*) from t1,t2 where t1.id = t2.id
Plan hash value: 3211355954
--------------------------------------------------
| Id  | Operation           | Name      | E-Rows |
--------------------------------------------------
|   0 | SELECT STATEMENT    |           |        |
|   1 |  SORT AGGREGATE     |           |      1 |
|   2 |   NESTED LOOPS      |           |  10000 |
|   3 |    TABLE ACCESS FULL| T2        |  10000 |
|*  4 |    INDEX RANGE SCAN | IDX_T1_ID |      1 |
--------------------------------------------------

3.在10G下執行:
SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
PL/SQL Release 10.2.0.3.0 - Production
CORE    10.2.0.3.0      Production
TNS for Linux: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production

SQL> select b.name,a.value from v$mystat a, v$statname b where a.STATISTIC#=b.STATISTIC# and b.NAME like 'consistent gets%';

NAME                                                                           VALUE
---------------------------------------------------------------- ----------
consistent gets                                                            51
consistent gets from cache                                       51
consistent gets - examination                                     5
consistent gets direct                                                    0

SQL> select/*+use_nl(t2 t1)*/ count(*) from t1,t2 where t1.id = t2.id;

  COUNT(*)
----------
     10000

SQL> select b.name,a.value from v$mystat a, v$statname b where a.STATISTIC#=b.STATISTIC# and b.NAME like 'consistent gets%';

NAME                                                                            VALUE
---------------------------------------------------------------- ----------
consistent gets                                                          10208(+10157)
consistent gets from cache                                     10208(+10157)
consistent gets - examination                                           6(+1)
consistent gets direct                                                          0

--可以發現邏輯讀在10g下增加許多,在11G下僅僅287個邏輯讀。11g下邏輯讀明顯減少。

4.對比執行計劃:

11G:

SQL> select/*+use_nl(t2 t1) gather_plan_statistics */ count(*) from t1,t2 where t1.id = t2.id;
  COUNT(*)
    ----------
     10000

SQL> @dpc
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  877z7w0x4d3rx, child number 0
-------------------------------------
select/*+use_nl(t2 t1) gather_plan_statistics */ count(*) from t1,t2 where t1.id = t2.id
Plan hash value: 3211355954
-------------------------------------------------------------------------------------------
| Id  | Operation           | Name      | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
-------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |           |      1 |        |      1 |00:00:00.03 |     287 |
|   1 |  SORT AGGREGATE     |           |      1 |      1 |      1 |00:00:00.03 |     287 |
|   2 |   NESTED LOOPS      |           |      1 |  10000 |  10000 |00:00:00.03 |     287 |
|   3 |    TABLE ACCESS FULL| T2        |      1 |  10000 |  10000 |00:00:00.01 |     139 |
|*  4 |    INDEX RANGE SCAN | IDX_T1_ID |  10000 |      1 |  10000 |00:00:00.02 |     148 |
-------------------------------------------------------------------------------------------

10G:
SQL> select/*+use_nl(t2 t1) gather_plan_statistics */ count(*) from t1,t2 where t1.id = t2.id;

  COUNT(*)
----------
     10000

SQL> select * from table(dbms_xplan.display_cursor(NULL,NULL,'ALLSTATS LAST OUTLINE PEEKED_BINDS'));
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  877z7w0x4d3rx, child number 0
-------------------------------------
select/*+use_nl(t2 t1) gather_plan_statistics */ count(*) from t1,t2 where t1.id =t2.id

Plan hash value: 3211355954

-------------------------------------------------------------------------------------------
| Id  | Operation           | Name      | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
-------------------------------------------------------------------------------------------
|   1 |  SORT AGGREGATE     |           |      1 |      1 |      1 |00:00:00.09 |   10157 |
|   2 |   NESTED LOOPS      |           |      1 |  10000 |  10000 |00:00:00.12 |   10157 |
|   3 |    TABLE ACCESS FULL| T2        |      1 |  10000 |  10000 |00:00:00.01 |     135 |
|*  4 |    INDEX RANGE SCAN | IDX_T1_ID |  10000 |      1 |  10000 |00:00:00.07 |   10022 |
-------------------------------------------------------------------------------------------

--可以發現執行計劃一樣的,差別主要在掃描IDX_T1_ID索引上的buffers,可以知道11G改進了一些東西。因為T2插入是按照的id號的順序插入的,在掃描t1的IDX_T1_ID索引時id=1,id=2.....應該在同一索引資料塊上。

試想一下,如果T2表插入的資料不是有序的,邏輯讀沒有這麼少了。


5.建立表T3,打亂id的順序。重複以上的測試:

11G:
SQL> create table t3 nologging as select * from t1 order by dbms_random.random;
SQL> exec dbms_stats.gather_table_stats(user,'T3');

SQL> select b.name,a.value from v$mystat a, v$statname b where a.STATISTIC#=b.STATISTIC# and b.NAME like 'consistent gets%';

NAME                                                                           VALUE
---------------------------------------------------------------- ----------
consistent gets                                                          37
consistent gets from cache                                     37
consistent gets from cache (fastpath)                   33
consistent gets - examination                                   4
consistent gets direct                                                  0

SQL> select/*+use_nl(t3 t1)  gather_plan_statistics */ count(*) from t1,t3 where t1.id = t3.id;

  COUNT(*)
----------
     10000

SQL> select b.name,a.value from v$mystat a, v$statname b where a.STATISTIC#=b.STATISTIC# and b.NAME like 'consistent gets%';

NAME                                                                            VALUE
---------------------------------------------------------------- ----------
consistent gets                                                            10198
consistent gets from cache                                       10198
consistent gets from cache (fastpath)                     10193
consistent gets - examination                                            5
consistent gets direct                                                          0

PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  cwdqmh7ryywx8, child number 0
-------------------------------------
select/*+use_nl(t3 t1)  gather_plan_statistics */ count(*) from t1,t3 where t1.id = t3.id
Plan hash value: 1864576943
-------------------------------------------------------------------------------------------
| Id  | Operation           | Name      | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
-------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |           |      1 |        |      1 |00:00:00.04 |   10161 |
|   1 |  SORT AGGREGATE     |           |      1 |      1 |      1 |00:00:00.04 |   10161 |
|   2 |   NESTED LOOPS      |           |      1 |  10000 |  10000 |00:00:00.05 |   10161 |
|   3 |    TABLE ACCESS FULL| T3        |      1 |  10794 |  10000 |00:00:00.01 |     139 |
|*  4 |    INDEX RANGE SCAN | IDX_T1_ID |  10000 |      1 |  10000 |00:00:00.04 |   10022 |
-------------------------------------------------------------------------------------------

10g下:
SQL> select b.name,a.value from v$mystat a, v$statname b where a.STATISTIC#=b.STATISTIC# and b.NAME like 'consistent gets%';

NAME                                                                           VALUE
---------------------------------------------------------------- ----------
consistent gets                                                          51
consistent gets from cache                                     51
consistent gets - examination                                   5
consistent gets direct                                                  0

SQL> select/*+use_nl(t3 t1) gather_plan_statistics */ count(*) from t1,t3 where t1.id = t3.id;

  COUNT(*)
----------
     10000

SQL> select b.name,a.value from v$mystat a, v$statname b where a.STATISTIC#=b.STATISTIC# and b.NAME like 'consistent gets%';

NAME                                                                           VALUE
---------------------------------------------------------------- ----------
consistent gets                                                            10208
consistent gets from cach                                         10208
consistent gets - examinaton                                             6
consistent gets direct                                                           0



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-714135/,如需轉載,請註明出處,否則將追究法律責任。

相關文章