`

RAC环境下的sequence详解(原创)

 
阅读更多

在RAC环境中,序列的Cache问题可能会对性能有着决定性的影响,缺省的序列Cache值为20,这对RAC环境远远不够。
如果存在序列号使用的竞争,就可能在数据库中看到明显的队列等待:
enq: SQ - contention
在RAC情况下,可以将使用频繁的序列Cache值增加到10000,或者更高到50000,这些值在客户的环境中都有采用。
这是RAC设置和RAC使用的基本常识,不可或忘。
在以下测试中,可以显示Cache序列对于性能的影响:
http://space.itpub.net/14941137/viewspace-629941
摘要如下:
RAC两个会话分别处于不同node同时并发循环间断去取4万个值  :           
    nocache:               2100s
    cache =1000:         55s
差别却是好大。
单Instance数据库单会话循环不间断去1-4万个值  测试(在家里笔记本上测试结果)过程如下:
    nocache:             37.7s          10000   
    cache :20            4.31s          10000
    cache :100         2.92s           10000
    cache :1000       5.56s          40000
    nocache:             97.7s         40000
基本上cache 大于20的时候性能基本可以接受,最好设置100以上,
nocache的时候性能确实很差,最大相差20倍.
排序参数:oracle默认是NOORDER,如果设置为ORDER;在单实例环境没有影响,在RAC环境此时,多实例实际缓存相同的序列,此时在多个实例 并发取该序列的时候,会有短暂的资源竞争来在多实例之间进行同步。因次性能相比noorder要差,所以RAC环境非必须的情况下不要使用ORDER,尤 其要避免NOCACHE   ORDER组合;
在某些版本中存在BUG,会导致过度的 enq : SQ 竞争。

如在Oracle Database 11g中存在 IDGEN$ 序列 cache 设置过小问题,可能导致严重竞争,建议增加该序列的Cache值设置。

RAC环境下与sequence相关的锁

oracle为了在rac环境下为了sequence的一致性,使用了三种锁:row cache lock、SQ锁、SV锁。
row cache lock的目的是在sequence指定nocache的情况下调用sequence.nextval过程中保证序列的顺序性;
SQ锁是应用于指定了cache+noorder的情况下调用sequence.nextval过程中。
SV锁(dfs lock handel) 是调用sequence.nextval期间拥有的锁。前提是创建sequence时指定了cache 和order属性 (cache+order)。order参数的目的是为了在RAC上节点之间生成sequence的顺序得到保障。
创建sequence赋予的cache值较小时,有enq:sq-contention等待增加的趋势。
cache的缺省值是20.因此创建并发访问多的sequence时,cacheh值应取大一些。否则会发生enq:sq-contention等待事件。
rac上创建sequence时,如果指定了cache大小同时赋予了noorder属性,则各节点将会把不同范围的sequence值cache到内存上。

create sequence TX_SEND_SEQ_ACC
minvalue 1
maxvalue 999999999999999999999999999
start with 673560
increment by 1
cache 20;
RAC1取序列
SQL> select tx_send_seq_acc.nextval from dual;
   NEXTVAL
----------
    673560
SQL> select tx_send_seq_acc.nextval from dual;
   NEXTVAL
----------
    673561
RAC2取序列
SQL> select tx_send_seq_acc.nextval from dual;
   NEXTVAL
----------
    673580
SQL> select tx_send_seq_acc.nextval from dual;
   NEXTVAL
----------
    673581

若两个节点之间都必须通过依次递增方式使用sequence,必须赋予如下的order属性
如果是已赋予了cache+order属性的sequence,oracle使用SV锁进行同步。SV锁争用问题发生时的解决方法与sq锁的情况相同,就是将cache 值进行适当调整。
在RAC多节点环境下,Sequence的Cache属性对性能的影响很大。应该尽量赋予cache+noorder属性,并要给予足够的cache值。如果需要保障顺序,必须赋予cache+order属性。但这时为了保障顺序,实例之间需要不断的交换数据。因此性能稍差。

oracle RAC环境sequence不一致问题
Sequences in Oracle 10g RAC
Just recently I got a call from a developer. He had a table with a primary key populated by a sequence, a timestamp column with the current date and some other columns. He had a specific set of data that, when ordered by the primary key had out of order timestamps. He was puzzled how this could be. This is a RAC database and the sequence was created with the default values.  Not only the sequences cache was the default of 20, but it was “noordered”.  Being “noordered” Oracle will not guarantee the order in which numbers are generated.
Example of “noorder” sequence in 10g RAC:
Session 1 on node-A: nextval -> 101
Session 2 on node-A: nextval -> 102
Session 1 on node-B: nextval -> 121
Session 1 on node-B: nextval -> 122
Session 1 on node-A: nextval -> 103
Session 1 on node-A: nextval -> 104
The sequence cache is in the shared pool, therefore sessions on the same node can share the cached entry, but sessions on different nodes cannot. I wonder why Oracle doesnt make “ordered” the default for sequences.  So I explained to the developer how sequences work in RAC and how each node has its own “cache”.
We changed the sequence to “ordered” and increased the cache to 1000. Now selecting on either node gets the next number as he expected. I warned him that there would be some performance implications due to cluster synchronization. Him been a responsive developer, asked me what would be the impact, so I tested it out.
How does RAC synchronize sequences?
In Oracle 10g RAC, if you specify the “ordered” clause for a sequence, then a global lock is allocated by the node when you access the sequence.  This lock acquisition happens only at the first sequence access for the node (A), and subsequent uses of the sequence do not wait on this lock. If another node (B) selects from that sequence, it requests the same global lock and once acquired it returns the sequences next value.  The wait event associated with this activity is recorded as "events in waitclass Other" when looked in gv$system_event. So much for event groups, it couldn't be more obscure. That view shows overall statistics for the session.
However if you look in the gv$session_wait_history it shows as “DFS lock handle” with the “p1″ parameter been the object_id of the sequence. This second view has a sample of the last 10 wait events for a session.
In a SQL_TRACE with waitevents (10046 trace) it will be a "DFS lock handle" but in AWR or statspack reports it will be “events in waitclass Other”. So much for consistency.
How does that change our example?
Session 1 on node-A: nextval -> 101 (DFS Lock handle) (CR read)
Session 2 on node-A: nextval -> 102
Session 1 on node-B: nextval -> 103 (DFS Lock handle)
Session 1 on node-B: nextval -> 104
Session 1 on node-A: nextval -> 105 (DFS Lock handle)
Session 1 on node-A: nextval -> 106
(more selects)
Session 1 on node-A: nextval -> 998
Session 1 on node-B: nextval -> 999 (DFS Lock handle)
Session 1 on node-B: nextval -> 1000 (CR read)
The cache size also has some RAC synchronization implications. When the cached entries for the sequence are exhausted, the sequence object needs to be updated. This usually causes a remote CR (current read) over the interconnect for the block that has the specific sequence object. So a bit more activity here.
Test case:
create sequence test_rac;
declare
  dummy number;
begin
  for i in 1..50000 loop
    select test_rac.nextval into dummy from dual;
  end loop;
end;
/
Results:
50 000 loops with cache = 20 (default)
1 node = 5 seconds
2 nodes at same time = 14 seconds
2 nodes at same time ordered = 30 seconds
50 000 loops with cache = 1000
1 node = 1.5 seconds
2 nodes at same time = 1.8 seconds
2 nodes at same time ordered = 20 seconds
With a smaller cache, the “noordered” still has as significant impact as every 10 fetches (cache 20 divided by 2 nodes fetching) it has to synchronize between the 2 nodes
The conclusion
By default sequences in 10g RAC are created without ordering. Beware of using applications that rely on sequences to be ordered and using it in a RAC environment.  Consider changing all user sequences to “ordered” as a precaution and increasing the cache size.  The default cache value is still very low and even not-ordered sequences will cause contention in a highly-active sequence even in non-RAC and causing an additional block exchange every 20 values in RAC.
For high volume insert operations where ordering is not performed on the value returned from the sequence, consider leaving the sequence “noordered” but increasing the cache size significantly.
Either way, the sequence parameters should be reviewed, as chances are, the defaults are not what you need.  I remember reading somewhere that in Oracle 9i the “ordered” clause in RAC was equivalent to “nochache”.  I cant imagine how bad that would be in concurrent selects from the same sequence.  It would be interesting if someone running 9i RAC performs the test case and I would appreciate if you post the results in the comments.

 

参考至:http://www.aixchina.net/home/space.php?uid=20260&do=blog&id=26752

              http://blog.csdn.net/zftang/article/details/6321513

              http://www.eygle.com/archives/2012/05/oracle_rac_sequence_cache.html

              http://wenku.baidu.com/link?url=GpJLv3Lrl6YxxnYMkZZlMcKa9z9LCaAVyXsp-tjizHlvUluY3jQbPaZBAMiNvw1md9_yZEIWCl9CD2JeakbM--u8ngBz7Bs27lfLSNgEPg_

              http://bbs.csdn.net/topics/390310984

本文原创,转载请注明出处、作者

如有错误,欢迎指正

邮箱:czmcj@163.com

0
1
分享到:
评论

相关推荐

    ORACLE 11g RAC开启、修改归档方法

    本文将详细介绍如何在Oracle 11g RAC环境下启用、修改以及配置归档模式。 #### 二、环境准备 本文档基于以下环境进行操作: - 操作系统:RHEL6U8 - 数据库版本:Oracle 11g R2 - 实例名称:racdb1、racdb2 - 主机...

    Oracle_DataGuard 11g RAC+单机

    ### Oracle DataGuard 11g RAC+单机配置详解 #### 一、概述 本文主要介绍了Oracle DataGuard 11g环境下,采用RAC(Real Application Clusters)作为主库,而备用库则采用单机模式的具体部署过程。DataGuard是一种...

    rac数据库修改redolog日志组大小及数量.docx

    根据题目描述,在RAC环境下需要向现有数据库中添加四组Redo Log日志组。具体命令如下: ```sql ALTER DATABASE ADD LOGFILE THREAD 1 GROUP 5 ('+DATA/pracdb1/onlinelog/redo05_1.log', '+FLASH/pracdb1/onlinelog...

    the v$ views for oracle database 10gR2

    全球固定视图是在Oracle RAC(Real Application Clusters)环境中使用的,它们包含了来自集群中所有实例的信息。通过添加实例ID列,GV$ Views使得从多个实例中收集和分析数据成为可能。例如,`GV$SESSION` 就可以...

    oracle中创建序列及序列补零实例详解

    oracle中创建序列及序列补零实例详解 我们经常会在在DB中创建序列: -- Create sequence create sequence COMMON_SEQ minvalue 1 maxvalue 999999999 start with 1 increment by 1 cache 20 cycle; 我们的序列的...

    oracle 重置序列从指定数字开始的方法详解

    Oracle数据库中的序列(Sequence)是一种用于生成唯一数值的数据库对象,通常用于自动...不过,需要注意的是,如果在并发环境中操作序列,可能会导致序列值的不一致,因此最好在无事务或锁定序列的情况下执行这些操作。

    ORACLE 动态性能视图

    - **GV$视图**:在Oracle Real Application Clusters (RAC)环境中,GV$视图提供了跨多个实例的数据视图。与V$视图类似,每个V$视图都有一个对应的GV$视图版本,增加了`INST_ID`列,用于标识信息来自哪个实例。这使得...

    ORACLE常用动态性能表

    - **GV$视图**:在Oracle Real Application Clusters (RAC)环境中,除了V$视图之外,还有一类特殊的视图称为GV$视图(Global V$)。这些视图不仅包含了单个实例的数据,还可以跨多个实例提供统一视图,使得管理员...

    Oracle sqlldr导入一个日期列实例详解

    c1 "SEQ_test_c1.nextval" -- 字段c1使用序列SEQ_test_c1的下一个值填充 ) BEGINDATA AAAAAAAAAA 20051201 BBBBBBBBBB 20050112 ``` 控制文件中的每一行都代表一个操作。`INFILE *` 指示SQL*Loader使用标准输入或...

    ORACLE动态性能视图

    除了V$视图外,Oracle还提供了另一种类型的视图——GV$视图(Global V$),主要用于在RAC环境中获取所有实例的信息。 **特点:** - 对于大多数V$视图(除了V$CACHE_LOCK、V$LOCK_ACTIVITY、V$LOCKS_WITH_...

    附录A 动态性能视图.pdf

    4. **全局性**:除了本地视图外,还有全局视图(`GV$`),可以在RAC(Real Application Clusters)环境中查看所有节点的信息。 #### 三、V$视图与GV$视图 - **V$视图**:主要用于单个数据库实例的监控。这些视图...

    中国移动计费帐务管理系统

    《中国移动计费账务管理系统详解》 中国移动计费账务管理系统是一个专为中国移动设计的核心业务系统,用于处理海量的通信费用计算、账单生成、客户账务管理等关键任务。系统的稳定性和准确性对于保证中国移动的运营...

    Oracle 触发器的使用小结

    可以通过 `SELECT sequence_name.NEXTVAL FROM DUAL` 来获取序列的下一个值,用于自增列。 2. **创建触发器**: - `CREATE OR REPLACE TRIGGER` 用于创建或替换已存在的触发器。如: ```sql CREATE OR REPLACE ...

Global site tag (gtag.js) - Google Analytics