(轉(zhuǎn))Oracle Sequence Cache 參數(shù)說(shuō)明

          Oracle Sequence Cache 參數(shù)說(shuō)明 收藏
           

                 公司上線一套R(shí)AC 系統(tǒng)。 Aston對(duì)Sequence 的 Cache 參數(shù)表示關(guān)注,建議并發(fā)大的系統(tǒng)對(duì)Cache設(shè)置大一點(diǎn)。 這樣可以提高性能。 和Aston 討論了一下。 又從網(wǎng)上查了一下。

           

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

           

          之前整理的一篇文章。 剛才看了一下,也是從網(wǎng)上轉(zhuǎn)的。 那是還是寫blog初期的作品。 2009年10月份的。 轉(zhuǎn)眼一年,寫B(tài)log 也比以前成熟了很多。
           

          一. 理論知識(shí)先看一個(gè)創(chuàng)建Sequence的語(yǔ)句:
          SQL> create sequence seq_tmp

            2  increment by 1

            3  start with 1

            4  nomaxvalue

            5  nocycle

            6  ;

          序列已創(chuàng)建。
           

          相關(guān)參數(shù)說(shuō)明:
                INCREMENT BY 1 -- 每次加幾個(gè)

                START WITH 1 -- 從1開始計(jì)數(shù)

                NOMAXvalue -- 不設(shè)置最大值

                NOCYCLE -- 一直累加,不循環(huán)

                CACHE 10;  --設(shè)置緩存cache個(gè)序列

                CURRVAL=返回 sequence的當(dāng)前值

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

           

          更多信息,參考Oracle 聯(lián)機(jī)文檔:
          CREATE SEQUENCE

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

           

           

          這里對(duì)Cache 參數(shù)做一個(gè)說(shuō)明:
                如果指定CACHE值,ORACLE就可以預(yù)先在內(nèi)存里面放置一些sequence,這樣存取的快些。cache里面的取完后,oracle自動(dòng)再取一組到cache。 使用cache或許會(huì)跳號(hào), 比如我們?cè)趧?chuàng)建序列時(shí)指定Cache 為100. 在某一個(gè)時(shí)刻,序列使用到了80. 而在這個(gè)時(shí)刻,數(shù)據(jù)庫(kù)突然不正常down掉(shutdown abort),cache中的sequence就會(huì)丟失.  在下次啟動(dòng)分配cache時(shí),數(shù)據(jù)庫(kù)會(huì)從101 開始,在分配100個(gè)緩存。即101--200. 而之前分配100個(gè)中的80-100這20個(gè)因?yàn)橐馔忮礄C(jī)而丟失。 這種情況下就會(huì)出現(xiàn)跳號(hào)的現(xiàn)象。我們可以在create sequence的時(shí)候用nocache防止這種情況。 但是nocache 的性能較差。 如果指定cache而沒(méi)有設(shè)定cache值,默認(rèn)cache是20個(gè)。 這個(gè)默認(rèn)值對(duì)于大多數(shù)情況下都是夠用的。 除非那種每秒上萬(wàn)次的select。 所以具體情況要具體對(duì)待。 對(duì)于哪些大并發(fā)的系統(tǒng),最好設(shè)置在100以上。像移動(dòng)的BOSS系統(tǒng),以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.

           

           

          關(guān)于Order 參數(shù)的說(shuō)明:
           

                 序參數(shù):oracle默認(rèn)是NOORDER,如果設(shè)置為ORDER;在單實(shí)例環(huán)境沒(méi)有影響,在RAC環(huán)境此時(shí),多實(shí)例實(shí)際緩存相同的序列,此時(shí)在多個(gè)實(shí)例并發(fā)取該序列的時(shí)候,會(huì)有短暫的資源競(jìng)爭(zhēng)來(lái)在多實(shí)例之間進(jìn)行同步。因次性能相比noorder要差,所以RAC環(huán)境非必須的情況下不要使用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 表的結(jié)構(gòu):
          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

           

           

          查看剛才創(chuàng)建的序列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

           

          這里有個(gè)CACHE_SIZE的值。 我們?cè)趧?chuàng)建sequence的時(shí)候,啟用了cache,但是沒(méi)有給它值。 所以這里的cache_size 就是系統(tǒng)的模式值。 即20個(gè)。

           

           

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

             NEXTVAL

          ----------

                   1

          SQL> select seq_tmp.nextval from dual;

             NEXTVAL

          ----------

                   2

           

           

          查看當(dāng)前sequence的值:
          SQL> select seq_tmp.currval from dual;

           

             CURRVAL

          ----------

           

          二. 實(shí)驗(yàn)一個(gè)網(wǎng)友RAC 系統(tǒng)上的測(cè)試時(shí)結(jié)果:
          nocache:               2100s

          cache =1000:          55s

          差別很明顯。

           

           

          測(cè)試一:
          SQL> create sequence seq_1 nocache;

          序列已創(chuàng)建。

          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 過(guò)程已成功完成。

           

          已用時(shí)間:  00: 00: 02.26

           

          測(cè)試二:
          SQL> create sequence seq_2 cache 20;

          序列已創(chuàng)建。

          已用時(shí)間:  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 過(guò)程已成功完成。

           

          已用時(shí)間:  00: 00: 00.46

           

          測(cè)試三:
          SQL> create sequence seq_3 cache 100;

           

          序列已創(chuàng)建。

           

          已用時(shí)間:  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 過(guò)程已成功完成。

           

          已用時(shí)間:  00: 00: 00.37

           

           

          測(cè)試四:
          SQL> create sequence seq_4 cache 1000;

          序列已創(chuàng)建。

          已用時(shí)間:  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 過(guò)程已成功完成。

           

          已用時(shí)間:  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 過(guò)程已成功完成。

           

          已用時(shí)間:  00: 00: 09.33

          SQL>

           

           

           

          小結(jié):

           

          在自己的本本上測(cè)試的,Oracle 11gR2.  單Instance數(shù)據(jù)庫(kù)單會(huì)話循環(huán)不間斷取1-4萬(wàn)個(gè)值。

          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的時(shí)候性能基本可以接受,nocache的時(shí)候性能確實(shí)很差.

           

          本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/tianlesoftware/archive/2010/11/08/5995051.aspx

          posted on 2011-01-31 11:49 liujg 閱讀(1820) 評(píng)論(0)  編輯  收藏 所屬分類: 數(shù)據(jù)庫(kù)

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          boddiy

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 岚皋县| 云浮市| 浙江省| 宜宾县| 陆丰市| 新民市| 康定县| 汶上县| 河北区| 麻江县| 司法| 成都市| 手游| 昆明市| 天台县| 威海市| 漳平市| 龙南县| 辽宁省| 怀仁县| 梨树县| 怀远县| 灯塔市| 乃东县| 彭山县| 分宜县| 弥勒县| 漯河市| 磴口县| 榆林市| 定边县| 和林格尔县| 滦平县| 类乌齐县| 耒阳市| 土默特右旗| 东方市| 策勒县| 乡宁县| 宝丰县| 邹城市|