Oracle Sequence Cache 參數說明 收藏
公司上線一套RAC 系統。 Aston對Sequence 的 Cache 參數表示關注,建議并發大的系統對Cache設置大一點。 這樣可以提高性能。 和Aston 討論了一下。 又從網上查了一下。
RACLE SEQUENCE 介紹
http://blog.csdn.net/tianlesoftware/archive/2009/10/30/4745039.aspx
之前整理的一篇文章。 剛才看了一下,也是從網上轉的。 那是還是寫blog初期的作品。 2009年10月份的。 轉眼一年,寫Blog 也比以前成熟了很多。
一. 理論知識先看一個創建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