關於查詢表資料,列與CPU開銷的計算

liglewang發表於2011-11-10

在效能最佳化藝術一書中看到:

Joze Senegacnik指出,從Oracle 10gR2開始,可以透過如下這個公式計算出訪問一個列的CPU開銷。

cpu_cost = column_position * 20
公式:估算訪問一個列的CPU開銷跟這個列在表中的位置有關。這個公式給出了訪問一行所用的開銷。如果訪問多行資料,CPU開銷按比例增加(為什麼會增加,後續再研究~)。

下面一個實驗證明一下:

建立一張表t1,插入一行記錄,執行explain plan得到執行計劃,獲取依次取出第一列到第九列的CPU開銷。

SQL> create table t1 (
  2  c1 number,
  3  c2 number,
  4  c3 number,
  5  c4 number,
  6  c5 number,
  7  c6 number,
  8  c7 number,
  9  c8 number,
 10  c9 number);
Table created.
SQL> insert into t1 values (1,2,3,4,5,6,7,8,9);
1 row created.
SQL> explain plan set statement_id 'c1' for select c1 from t1;
SQL> explain plan set statement_id 'c2' for select c2 from t1;
SQL> explain plan set statement_id 'c3' for select c3 from t1;
SQL> explain plan set statement_id 'c4' for select c4 from t1;
SQL> explain plan set statement_id 'c5' for select c5 from t1;
SQL> explain plan set statement_id 'c6' for select c6 from t1;
SQL> explain plan set statement_id 'c7' for select c7 from t1;
SQL> explain plan set statement_id 'c8' for select c8 from t1;
SQL> explain plan set statement_id 'c9' for select c9 from t1;
SQL> select statement_id,cpu_cost as total_cpu_cost,
  2  cpu_cost-lag(cpu_cost) over (order by statement_id) as cpu_cost_1_coll,
  3  io_cost
  4  from plan_table
  5  where id=0
  6  order by statement_id;
STATEMENT_ID                   TOTAL_CPU_COST CPU_COST_1_COLL    IO_COST
------------------------------ -------------- --------------- ----------
c1                                      35757                          3
c2                                      35777              20          3
c3                                      35797              20          3
c4                                      35817              20          3
c5                                      35837              20          3
c6                                      35857              20          3
c7                                      35877              20          3
c8                                      35897              20          3
c9                                      35917              20          3
9 rows selected.

請注意,初始讀取表資料的CPU開銷是35757,每往後面一列,CPU開銷加20。同時I/O開銷不變,這是因為所有列的資料基本儲存在同一個資料塊,全部查詢的物理讀的次數是一樣的。

摘錄、參考於Oracle效能診斷藝術

Ligle WANG

2011-11-10

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

相關文章