laineysgl  
          日歷
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789
          統計
          • 隨筆 - 0
          • 文章 - 2
          • 評論 - 1
          • 引用 - 0

          導航

          留言簿

          文章檔案

          搜索

          •  

          最新評論

           

          【轉】(轉)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的時候性能確實很差.

          此外,已緩存共享池中的序列值在數據庫的操作時可以造成老化??梢允褂肈MBS_SHARED_POOL 存儲過程,避免老化序列。

           

           

          更改序列的Cache SIZE值步驟
          1:

          ALTER SEQUENCE sde.connection_id_generator CACHE 1000
          ALTER SEQUENCE sde.state_id_generator_nc CACHE 1000
          ALTER SEQUENCE sde.version_id_generator CACHE 1000

          2:

          exec sys.DBMS_SHARED_POOL.KEEP('sde.connection_id_generator', 'Q')
          exec sys.DBMS_SHARED_POOL.KEEP('sde.state_id_generator_nc', 'Q')
          exec sys.DBMS_SHARED_POOL.KEEP('sde.version_id_generator', 'Q')


          posted on 2012-08-14 14:51 lainey 閱讀(427) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
           
          Copyright © lainey Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 渝北区| 凤翔县| 高邮市| 呼图壁县| 称多县| 得荣县| 文安县| 长乐市| 嘉禾县| 永宁县| 军事| 太谷县| 陵川县| 游戏| 永济市| 基隆市| 阿克苏市| 讷河市| 家居| 新巴尔虎左旗| 舒兰市| 米脂县| 钦州市| 卢湾区| 东港市| 大新县| 吕梁市| 博湖县| 宁海县| 宜兰市| 稷山县| 南充市| 东丽区| 浦东新区| 麻栗坡县| 出国| 肃宁县| 阿尔山市| 墨竹工卡县| 高邮市| 嘉荫县|