oracle的性能優化[1]

內容翻譯自: PLSQL User's Guide and Reference.pdf

程序問題
1.不要進行全表掃瞄.

2. Suppose the subprogram called most often by an application is a lookup function
with hundreds of possible targets. If that function could be written as a hash or a
binary search but, instead, is written as a linear search, overall performance suffers.

3.不要傳遞不使用的變量或者不使用的變量在循環裏初始化和計算.

4.不要編寫內置函數的副本,因爲它們更高效.

5.使用條件控制表達式if else,exit when 要注意迴路的優化.比如: 簡單的判斷 or 複雜的函數調用 ,
這樣如果簡單的判斷返回true時,複雜的函數調用就不需要計算.
同理: and, not

6.不要使用隱式的數據類型轉換
The integer literal 15 is represented internally as a signed 4-byte quantity, so
PL/SQL must convert it to an Oracle number before the addition. However, the
floating-point literal 15.0 is represented as a 22-byte Oracle number, so no
conversion is necessary.

DECLARE
n NUMBER;
c CHAR(5);
BEGIN
n := n + 15; -- converted
n := n + 15.0; -- not converted
...
END;

7. 使用PLS_INTEGER 來聲明整形變量,因爲它是最高效的.
When you
need to declare an integer variable, use the datatype PLS_INTEGER, which is the
most efficient numeric type. That is because PLS_INTEGER values require less
storage than INTEGER or NUMBER values. Also, PLS_INTEGER operations use
machine arithmetic, so they are faster than BINARY_INTEGER, INTEGER, or
NUMBER operations, which use library arithmetic.
  儘量不要使用以下數據類型來聲明變量.
INTEGER, NATURAL, NATURALN, POSITIVE, POSITIVEN, and
SIGNTYPE are constrained subtypes. So, their variables require precision checking
at run time, which can affect performance.

8.不要使用not null來聲明變量,因爲會增加內部的判斷消耗.使用程序進行判斷.
Because m is constrained by NOT NULL, the value of the expression a + b is
assigned to a temporary variable, which is then tested for nullity.

[不要使用]

PROCEDURE calc_m IS
m NUMBER NOT NULL := 0;
a NUMBER;
b NUMBER;
BEGIN
...
m := a + b;
...
END;

而要使用

[而要使用]
PROCEDURE calc_m IS
m NUMBER; -- no constraint
a NUMBER;
b NUMBER;
BEGIN
...
m := a + b;
IF m IS NULL THEN -- enforce constraint programmatically
...
END IF;
END;

一些定義爲not null的數據類型.

Note that the subtypes NATURALN and POSTIVEN are defined as NOT NULL. So,
using them incurs the same performance cost.

9.定義varchar2的大小時,大小要>=2000.>=2000的varchar2 oracle會自動分配空間.
The VARCHAR2 datatype involves a trade-off between memory use and efficiency.
For a VARCHAR2(>= 2000) variable, PL/SQL dynamically allocates only enough
memory to hold the actual value. However, for a VARCHAR2(< 2000) variable,
PL/SQL preallocates enough memory to hold a maximum-size value. So, for
example, if you assign the same 500-byte value to a VARCHAR2(2000) variable and
to a VARCHAR2(1999) variable, the latter uses 1499 bytes more memory.

10.確保oracle有足夠的內存池存放編譯後的子程序.這樣編譯後程序生命週期就會很長,
   而不會經常讀I/O(編譯).但是不需要過大造成浪費.

11.一些常用的包可以固定在內存池
When a package is pinned, it is not aged out by the least
recently used (LRU) algorithm that Oracle normally uses.
You can pin packages with the help of the supplied package DBMS_SHARED_POOL.

12.使用decode查詢一次來取代update.

select key_name,
       count(1) query_num,
       sum(decode(act_type, 1, 1, 0)) active_num
  from wap_search_log_pic_20071028
 where key_name is not null
   and search_type = 2
   and page_num = 1
 group by key_name

未完待續 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章