1. 程式人生 > >【轉】(轉)Oracle Sequence Cache 引數說明

【轉】(轉)Oracle Sequence Cache 引數說明

RACLE SEQUENCE 介紹
http://blog.csdn.net/tianlesoftware/archive/2009/10/30/4745039.aspx

一. 理論知識先看一個建立Sequence的語句:
SQL> create sequence seq_tmp

  2  increment by 1

  3  start with 1

  4  nomaxvalue

  5  nocycle

  6  ;

序列已建立。
 

相關引數說明:
      INCREMENT BY 1 -- 每次加幾個

      START WITH 1 -- 從1開始計數

      NOMAXvalue -- 不設定最大值

      NOCYCLE -- 一直累加,不迴圈

      CACHE 10;  --設定快取cache個序列

      CURRVAL=返回 sequence的當前值

      NEXTVAL=增加sequence的值,然後返回 sequence 值

更多資訊,參考Oracle 聯機文件:
CREATE SEQUENCE

http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/statements_6015.htm#SQLRF01314

這裡對Cache 引數做一個說明:
      如果指定CACHE值,ORACLE就可以預先在記憶體裡面放置一些sequence,這樣存取的快些。cache裡面的取完後,oracle自動再取一組到cache。 使用cache或許會跳號, 比如我們在建立序列時指定Cache 為100. 在某一個時刻,序列使用到了80. 而在這個時刻,資料庫突然不正常down掉(shutdown abort),cache中的sequence就會丟失.  在下次啟動分配cache時,資料庫會從101 開始,在分配100個快取。即101--200. 而之前分配100箇中的80-100這20個因為意外宕機而丟失。 這種情況下就會出現跳號的現象。我們可以在create sequence的時候用nocache防止這種情況。 但是nocache 的效能較差。 如果指定cache而沒有設定cache值,預設cache是20個。 這個預設值對於大多數情況下都是夠用的。 除非那種每秒上萬次的select。 所以具體情況要具體對待。 對於哪些大併發的系統,最好設定在100以上。像移動的BOSS系統,以1000為單位。

CACHE Specify how many values of the sequence the database preallocates and keeps in memory for faster access. This integer value can have 28 or fewer digits. The minimum value for this parameter is 2. For sequences that cycle, this value must be less than the number of values in the cycle. You cannot cache more values than will fit in a given cycle of sequence numbers. Therefore, the maximum value allowed for CACHE must be less than the value determined by the following formula:

(CEIL (MAXVALUE - MINVALUE)) / ABS (INCREMENT)

If a system failure occurs, then all cached sequence values that have not been used in committed DML statements are lost. The potential number of lost values is equal to the value of the CACHE parameter.

Note:

Oracle recommends using the CACHE setting to enhance performance if you are using sequences in an Oracle Real Application Clusters environment.

NOCACHE  Specify NOCACHE to indicate that values of the sequence are not preallocated. If you omit both CACHE and NOCACHE, then the database caches 20 sequence numbers by default.

關於Order 引數的說明:
 

       序引數:oracle預設是NOORDER,如果設定為ORDER;在單例項環境沒有影響,在RAC環境此時,多例項實際快取相同的序列,此時在多個例項併發取該序列的時候,會有短暫的資源競爭來在多例項之間進行同步。因次效能相比noorder要差,所以RAC環境非必須的情況下不要使用ORDER,尤其要避免NOCACHE   ORDER組合。

ORDER Specify ORDER to guarantee that sequence numbers are generated in order of request. This clause is useful if you are using the sequence numbers as timestamps. Guaranteeing order is usually not important for sequences used to generate primary keys.

ORDER is necessary only to guarantee ordered generation if you are using Oracle Real Application Clusters. If you are using exclusive mode, then sequence numbers are always generated in order.

NOORDER  Specify NOORDER if you do not want to guarantee sequence numbers are generated in order of request. This is the default.

檢視user_sequences 表的結構:
SQL> desc user_sequences;

 名稱                                      是否為空? 型別
 ----------------------------------------- -------- ---------------

 SEQUENCE_NAME                             NOT NULL VARCHAR2(30)

 MIN_VALUE                                          NUMBER

 MAX_VALUE                                          NUMBER

 INCREMENT_BY                              NOT NULL NUMBER

 CYCLE_FLAG                                         VARCHAR2(1)

 ORDER_FLAG                                         VARCHAR2(1)

 CACHE_SIZE                                NOT NULL NUMBER

 LAST_NUMBER                               NOT NULL NUMBER

檢視剛才建立的序列seq_tmp 的值:
SQL> select * from user_sequences where sequence_name='SEQ_TMP';

SEQUENCE_N  MIN_VALUE MAX_VALUE INCREMENT_BY C O CACHE_SIZE LAST_NUMBER

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

SEQ_TMP             1 1.0000E+28            1 N N         20          21

這裡有個CACHE_SIZE的值。 我們在建立sequence的時候,啟用了cache,但是沒有給它值。 所以這裡的cache_size 就是系統的模式值。 即20個。

取下一個sequence的值:
SQL> select seq_tmp.nextval from dual;

   NEXTVAL

----------

         1

SQL> select seq_tmp.nextval from dual;

   NEXTVAL

----------

         2

檢視當前sequence的值:
SQL> select seq_tmp.currval from dual;

   CURRVAL

----------

二. 實驗一個網友RAC 系統上的測試時結果:
nocache:               2100s

cache =1000:          55s

差別很明顯。

測試一:
SQL> create sequence seq_1 nocache;

序列已建立。

SQL> set timing on;

SQL> declare

  2  x number;

  3  begin

  4  for i in 1 .. 10000 loop

  5  select seq_1.nextval into x from dual;

  6  end loop;

  7  end;

  8  /

PL/SQL 過程已成功完成。

已用時間:  00: 00: 02.26

測試二:
SQL> create sequence seq_2 cache 20;

序列已建立。

已用時間:  00: 00: 00.01

SQL> declare

  2  x number;

  3  begin

  4  for i in 1 .. 10000 loop

  5  select seq_2.nextval into x from dual;

  6  end loop;

  7  end;

  8  /

PL/SQL 過程已成功完成。

已用時間:  00: 00: 00.46

測試三:
SQL> create sequence seq_3 cache 100;

序列已建立。

已用時間:  00: 00: 00.05

SQL> declare

  2  x number;

  3  begin

  4  for i in 1 .. 10000 loop

  5  select seq_3.nextval into x from dual;

  6  end loop;

  7  end;

  8  /

PL/SQL 過程已成功完成。

已用時間:  00: 00: 00.37

測試四:
SQL> create sequence seq_4 cache 1000;

序列已建立。

已用時間:  00: 00: 00.04

SQL> declare

  2  x number;

  3  begin

  4  for i in 1 .. 40000 loop

  5  select seq_4.nextval into x from dual;

  6  end loop;

  7  end;

  8  /

PL/SQL 過程已成功完成。

已用時間:  00: 00: 01.31

SQL> declare

  2  x number;

  3  begin

  4  for i in 1 .. 40000 loop

  5  select seq_1.nextval into x from dual;

  6  end loop;

  7  end;

  8  /

PL/SQL 過程已成功完成。

已用時間:  00: 00: 09.33

SQL>

小結:

在自己的本本上測試的,Oracle 11gR2.  單Instance資料庫單會話迴圈不間斷取1-4萬個值。

nocache:             2.26s          10000   

cache:20              0.46s          10000

cache:100             0.37s          10000

cache:1000            1.31s          40000

nocache:             9.33s         40000

基本上cache 大於20的時候效能基本可以接受,nocache的時候效能確實很差.

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/tianlesoftware/archive/2010/11/08/5995051.aspx