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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值