天天看點

檢視oracle執行計劃方法( 二)

1、Cardinality(基數)/ rows

Cardinality值表示CBO預期從一個行源(row source)傳回的記錄數,這個行源可能是一個表,一個索引,也可能是一個子查詢。   在Oracle 9i中的執行計劃中,Cardinality縮寫成Card。 在10g中,Card值被rows替換。

這是9i的一個執行計劃,我們可以看到關鍵字Card:

       執行計劃

----------------------------------------------------------

   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=402)

   1    0   TABLE ACCESS (FULL) OF 'TBILLLOG8' (Cost=2 Card=1 Bytes=402)

Oracle 10g的執行計劃,關鍵字換成了rows:

執行計劃

Plan hash value: 2137789089

--------------------------------------------------------------------------------

| Id  | Operation                         | Name    | Rows  | Bytes | Cost (%CPU)| Time     |

---------------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT                  |         |  8168 | 16336 |    29   (0)| 00:00:01 |

|   1 |  COLLECTION ITERATOR PICKLER FETCH| DISPLAY |  8168 | 16336 |    29   (0)| 00:00:01 |

Cardinality的值對于CBO做出正确的執行計劃來說至關重要。 如果CBO獲得的Cardinality值不夠準确(通常是沒有做分析或者分析資料過舊造成),在執行計劃成本計算上就會出現偏差,進而導緻CBO錯誤的制定出執行計劃。

 在多表關聯查詢或者SQL中有子查詢時,每個關聯表或子查詢的Cardinality的值對主查詢的影響都非常大,甚至可以說,CBO就是依賴于各個關聯表或者子查詢Cardinality值計算出最後的執行計劃。

  對于多表查詢,CBO使用每個關聯表傳回的行數(Cardinality)決定用什麼樣的通路方式來做表關聯(如Nested loops Join 或 hash Join)。

對于子查詢,它的Cardinality将決定子查詢是使用索引還是使用全表掃描的方式通路資料。

2、執行計劃中字段解釋:

 ID: 一個序号,但不是執行的先後順序。執行的先後根據縮進來判斷。

       Operation: 目前操作的内容。

       Rows: 目前操作的Cardinality,Oracle估計目前操作的傳回結果集。

       Cost(CPU):Oracle 計算出來的一個數值(代價),用于說明SQL執行的代價。

       Time:Oracle 估計目前操作的時間。

2、 謂詞說明:

Predicate Information (identified by operation id):

---------------------------------------------------

   4 - access("A"."EMPNO"="B"."MGR")

       filter("A"."EMPNO"="B"."MGR")

   5 - filter("B"."MGR" IS NOT NULL)

       Access: 表示這個謂詞條件的值将會影響資料的通路路勁(表還是索引)。

       Filter:表示謂詞條件的值不會影響資料的通路路勁,隻起過濾的作用。

在謂詞中主要注意access,要考慮謂詞的條件,使用的通路路徑是否正确。

3. 統計資訊說明:

db block gets : 從buffer cache中讀取的block的數量    

consistent gets: 從buffer cache中讀取的undo資料的block的數量    

physical reads: 從磁盤讀取的block的數量    

redo size: DML生成的redo的大小    

sorts (memory) :在記憶體執行的排序量    

sorts (disk) :在磁盤上執行的排序量    

       Physical Reads通常是我們最關心的,如果這個值很高,說明要從磁盤請求大量的資料到Buffer Cache裡,通常意味着系統裡存在大量全表掃描的SQL語句,這會影響到資料庫的性能,是以盡量避免語句做全表掃描,對于全表掃描的SQL語句,建議增 加相關的索引,優化SQL語句來解決。

關于physical reads ,db block gets 和consistent gets這三個參數之間有一個換算公式:

       資料緩沖區的使用命中率=1 - ( physical reads / (db block gets + consistent gets) )。

用以下語句可以檢視資料緩沖區的命中率:

       SQL>SELECT name, value FROM v$sysstat WHERE name IN ('db block gets', 'consistent gets','physical reads');

       NAME      VALUE

---------------------------------------------------------------- ----------

db block gets     254764

consistent gets            

   1311538

physical reads      24232

查詢出來的結果Buffer Cache的命中率應該在90%以上,否則需要增加資料緩沖區的大小。

Recursive Calls:  Number of recursive calls generated at both the user and system level.    

Oracle Database maintains tables used for internal processing. When it needs to change these tables, Oracle Database generates an internal SQL statement, which in turn generates a recursive call. In short, recursive calls are basically

SQL performed on behalf of your SQL. So, if you had to parse the query, for example, you might have had to run some other queries to get data dictionary information. These would be recursive calls. Space management, security checks, calling PL/SQL from

SQL—all incur recursive SQL calls。

DB Block Gets: Number of times a CURRENT block was requested.

Current mode blocks are retrieved as they exist right now, not in a consistent read fashion. Normally, blocks retrieved for a query are retrieved as they existed when the query began. Current mode blocks are retrieved as they exist right now, not from a

previous point in time. During a SELECT, you might see current mode retrievals due to reading the data dictionary to find the extent information for a table to do a full scan (because you need the "right now" information, not the consistent read). During a

modification, you will access the blocks in current mode in order to write to them. (DB Block Gets:請求的資料塊在buffer能滿足的個數)

       目前模式塊意思就是在操作中正好提取的塊數目,而不是在一緻性讀的情況下而産生的塊數。正常的情況下,一個查詢提取的塊是在查詢開始的那個時間點上存在的資料塊,目前塊是在這個時刻存在的資料塊,而不是在這個時間點之前或者之後的資料塊數目。

Consistent Gets: Number of times a consistent read was requested for a block.

This is how many blocks you processed in "consistent read" mode. This will include counts of blocks read from the rollback segment in order to roll back a block. This is the mode you read blocks in with a SELECT, for example. Also, when you do a searched

UPDATE/DELETE, you read the blocks in consistent read mode and then get the block in current mode to actually do the modification. (Consistent Gets: 資料請求總數在復原段Buffer中的資料一緻性讀所需要的資料塊)

       這裡的概念是在處理你這個操作的時候需要在一緻性讀狀态上處理多少個塊,這些塊産生的主要原因是因為由于在你查詢的過程中,由于其他會話對資料塊進行操作,而對所要查詢的塊有了修改,但是由于我們的查詢是在這些修改之前調用的,是以需要對復原段中的資料塊的前映像進行查詢,以保證資料的一緻性。這樣就産 生了一緻性讀。

Physical Reads: Total number of data blocks read from disk. This number equals the value of "physical reads direct" plus all reads into buffer cache. (Physical Reads:執行個體啟動後,從磁盤讀到Buffer

Cache資料塊數量)

就是從磁盤上讀取資料塊的數量,其産生的主要原因是:

       (1) 在資料庫高速緩存中不存在這些塊

       (2) 全表掃描

       (3) 磁盤排序

它們三者之間的關系大緻可概括為:

       邏輯讀指的是Oracle從記憶體讀到的資料塊數量。一般來說是'consistent gets' + 'db block gets'。當在記憶體中找不到所需的資料塊的話就需要從磁盤中擷取,于是就産生了'physical reads'。

Sorts(disk):

    Number of sort operations that required at least one disk write. Sorts that require I/O to disk are quite resource intensive. Try increasing the size of the initialization parameter SORT_AREA_SIZE.

bytes sent via SQL*Net to client:

    Total number of bytes sent to the client from the foreground processes.

bytes received via SQL*Net from client:

    Total number of bytes received from the client over Oracle Net.

SQL*Net roundtrips to/from client:

    Total number of Oracle Net messages sent to and received from the client.

更多内容參考Oracle聯機文檔:

       Statistics Descriptions

3. 動态分析

       如果在執行計劃中有如下提示:

              Note

              ------------

                     -dynamic sampling used for the statement

       這提示使用者CBO目前使用的技術,需要使用者在分析計劃時考慮到這些因素。 當出現這個提示,說明目前表使用了動态采樣。 我們進而推斷這個表可能沒有做過分析。

這裡會出現兩種情況:

(1)       如果表沒有做過分析,那麼CBO可以通過動态采樣的方式來擷取分析資料,也可以或者正确的執行計劃。

(2)       如果表分析過,但是分析資訊過舊,這時CBO就不會在使用動态采樣,而是使用這些舊的分析資料,進而可能導緻錯誤的執行計劃。

總結:

       在看執行計劃的時候,除了看執行計劃本身,還需要看謂詞和提示資訊。 通過整體資訊來判斷SQL 效率。