自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Oracle數(shù)據(jù)庫consistent gets使用的特例

數(shù)據(jù)庫 Oracle
本文我們主要介紹了Oracle數(shù)據(jù)庫中使用consistent gets做性能比較的一個特殊的例子,并分析了其特殊的原因,通過本文我們能夠更深刻地理解consistent gets,希望能夠?qū)δ兴鶐椭?/div>

Oracle數(shù)據(jù)庫中,consistent gets在判斷一段SQL的性能時非常有用,通常來講比較兩段SQL的性能好壞不是看誰的執(zhí)行時間短,而是看誰的consistent gets小。不過這也不是絕對的,下面這個例子就是一個反例。

反例子如下:

  1. ETL@RACTEST> create table test( a int);  
  2. Table created. Elapsed: 00:00:00.05  
  3. ETL@RACTEST> ETL@RACTEST> begin  
  4.   2  for i in 1..10000 loop  
  5.   3  insert into test values (i);  
  6.   4  end loop;  
  7.   5  end;  
  8.   6  / PL/SQL procedure successfully completed. Elapsed: 00:00:00.44  
  9. ETL@RACTEST> set autot trace  
  10. ETL@RACTEST> ETL@RACTEST> select * from test;  
  11. 10000 rows selected. Elapsed: 00:00:00.05 Execution Plan Plan hash value: 1357081020 -------------------------------------------------------------  
  12. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     | |   0 | SELECT STATEMENT  |      | 10000 |   126K|     6   (0)| 00:00:01 |  
  13. |   1 |  TABLE ACCESS FULL| TEST | 10000 |   126K|     6   (0)| 00:00:01 |  
  14. -------------------------------------------------------------------------- Note    - dynamic sampling used for this statement  
  15. Statistics           0  recursive calls  
  16.           0  db block gets  
  17.         690  consistent gets  
  18.           0  physical reads  
  19.           0  redo size  
  20.      214231  bytes sent via SQL*Net to client  
  21.        7791  bytes received via SQL*Net from client  
  22.         668  SQL*Net roundtrips to/from client  
  23.           0  sorts (memory)  
  24.           0  sorts (disk)  
  25.       10000  rows processed 可以看到select *讀了690個內(nèi)存塊。 ETL@RACTEST> select * from test order by 1; 10000 rows selected. Elapsed: 00:00:00.04 Execution Plan Plan hash value: 2007178810 --------------------------------------------------------------------  
  26. | Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     | |   0 | SELECT STATEMENT   |      | 10000 |   126K|     7  (15)| 00:00:01 |  
  27. |   1 |  SORT ORDER BY     |      | 10000 |   126K|     7  (15)| 00:00:01 |  
  28. |   2 |   TABLE ACCESS FULL| TEST | 10000 |   126K|     6   (0)| 00:00:01 |  
  29. --------------------------------------------------------------------------- Note    - dynamic sampling used for this statement  
  30. Statistics           0  recursive calls  
  31.           0  db block gets  
  32.          23  consistent gets  
  33.           0  physical reads  
  34.           0  redo size  
  35.      174288  bytes sent via SQL*Net to client  
  36.        7791  bytes received via SQL*Net from client  
  37.         668  SQL*Net roundtrips to/from client  
  38.           1  sorts (memory)  
  39.           0  sorts (disk)  
  40.       10000  rows processed 

再看一下order by,竟然只有23個邏輯讀!

1. select * from test;

2. select * from test order by 1;

第1個SQL比第2個SQL效率高是毋庸置疑的。但是為什么第2個SQL的consistent gets如此之少,我起初也是百思不得其解,最終我在ASK TOM中找到了答案。

 

原因:

一:通常情況下,不在logical RAM buffer中的數(shù)據(jù)要通過physical reads來讀取,而physical reads后通常會緊跟著一個consistent gets。因此一般情況下consistent gets是要比physical reads大的。但是有一個特例,如果physical reads得到的數(shù)據(jù)直接用于HASH或者SORT,則只記為physical reads不記為consistent gets。所以加上order by后有可能physical reads多但consistent gets少。不過這個原因不是我這里現(xiàn)象產(chǎn)生的原因,因為我這個實驗里根本沒有physical reads。

二:arraysize的影響。arraysize是指讀取數(shù)據(jù)時一次讀取得到的行數(shù)。這個值默認為15,使用show arraysize命令可以查看。一個數(shù)據(jù)塊例如有100條記錄,那么并不是讀取這個塊一次就能取到所有數(shù)據(jù),以arraysize=15為例,就要有100/15=7次consistent gets。把arraysize設置得大一點可以降低consistent gets,不過有時候可能會消耗更多的資源。如果我們做select count(0) from test;操作,那么Oracle會把arraysize暫時設為test的行數(shù),因此consistent gets會很少:

代碼如下:

  1. ETL@RACTEST> select count(0) from test; Elapsed: 00:00:00.00 Execution Plan Plan hash value: 1950795681 --------------  
  2. | Id  | Operation          | Name | Rows  | Cost (%CPU)| Time     | |   0 | SELECT STATEMENT   |      |     1 |     6   (0)| 00:00:01 |  
  3. |   1 |  SORT AGGREGATE    |      |     1 |            |          |  
  4. |   2 |   TABLE ACCESS FULL| TEST | 10000 |     6   (0)| 00:00:01 |  
  5. ------------------------------------------------------------------- Note    - dynamic sampling used for this statement  
  6. Statistics           0  recursive calls  
  7.           0  db block gets  
  8.          23  consistent gets  
  9.           0  physical reads  
  10.           0  redo size  
  11.         515  bytes sent via SQL*Net to client  
  12.         465  bytes received via SQL*Net from client  
  13.           2  SQL*Net roundtrips to/from client  
  14.           0  sorts (memory)  
  15.           0  sorts (disk)  
  16.           1  rows processed 

可以看到select count(0)只需要23個邏輯讀。一共10000條數(shù)據(jù),10000/15=666.667 ,好,667+23=690!和第1個SQL的consistent gets竟然驚人的一致!這不是巧合,這就是consistent gets的計算公式。我們還可以發(fā)現(xiàn)select count(0)和第2個SQL的consistent gets竟然也驚人地一致,都是23!

TOM的解釋是:
在select * from test order by 1;時,Oracle也把arraysize臨時設為test表的行數(shù),它把所有數(shù)據(jù)先全部取出來放到sort區(qū)做排序,而在sort區(qū)的讀取就不算在consistent gets里了。所以雖然第2個SQL和select count(0)的consistent gets相同,但它的效率一定比select count(0)低,我們看執(zhí)行計劃里的COST便可以得知,第2個SQL的COST為7,select count(0)的COST為6,第1個SQL的COST也為6。(COST相同并不代表執(zhí)行效率完全相同)

關于Oracle數(shù)據(jù)庫consistent gets的知識就介紹到這里了,希望本次的介紹能夠?qū)δ兴斋@!

【編輯推薦】

  1. Oracle數(shù)據(jù)庫增刪集合元素的Java實現(xiàn)方法
  2. Oracle數(shù)據(jù)庫中TNSListener無法啟動的解決方案
  3. Oracle數(shù)據(jù)庫通過在線重定義的方法新增字段詳解
  4. Oracle數(shù)據(jù)庫的decode、sign、trunc函數(shù)使用詳解
  5. Oracle臨時表游標未釋放導致回滾段空間不足的解決方案
責任編輯:趙鵬 來源: 火魔網(wǎng)
相關推薦

2011-08-11 16:55:34

Oracle數(shù)據(jù)庫AWR

2009-03-10 09:38:02

oraclepython數(shù)據(jù)庫

2011-02-28 17:12:20

Oracle數(shù)據(jù)庫

2010-05-05 14:13:52

Oracle數(shù)據(jù)

2011-04-08 16:00:11

Oracle數(shù)據(jù)庫外部表

2009-05-13 10:28:30

OracleDUAL數(shù)據(jù)庫

2010-05-04 11:58:38

Oracle數(shù)據(jù)庫

2010-04-23 09:23:44

Oracle 數(shù)據(jù)庫

2011-05-26 10:30:12

Oracle數(shù)據(jù)庫約束

2015-08-21 12:59:38

Oracle數(shù)據(jù)庫

2011-03-10 13:24:26

2011-08-02 11:16:08

Oracle數(shù)據(jù)庫歸檔日志

2011-03-16 08:54:45

Oracle數(shù)據(jù)庫索引

2011-05-19 13:25:14

Oracle數(shù)據(jù)庫

2010-04-22 16:16:35

Oracle數(shù)據(jù)庫

2009-09-02 14:55:19

Oracle數(shù)據(jù)庫

2011-05-26 14:43:49

ORACLE數(shù)據(jù)庫異常處理

2010-10-26 16:27:37

連接Oracle數(shù)據(jù)庫

2011-08-16 13:17:29

2010-04-15 15:42:11

Oracle數(shù)據(jù)庫
點贊
收藏

51CTO技術棧公眾號