当前位置:操作系统 > Unix/Linux >>

【每日一摩斯】-【序列】-续-RAC and Sequences (853652.1)

【每日一摩斯】-【序列】-续-RAC and Sequences (853652.1)
 
【每日一摩斯】-RAC and Sequences (853652.1)
http://www.zzzyk.com/database/201308/239400.html
 
一个简单的sequence有什么可以说的呢?如果再这样认为就大错特错了。。。
 
也许以下几点高人们都很清楚,但至少对于我来说,之前是陌生的,或者说是忽略的。
 
1、CREATE SEQUENCE seq;,不带任何参数,那么默认建立的SQL语句是:
 
-- Create sequence 
create sequence SEQ
minvalue 1
maxvalue 999999999999999999999999999
start with 21
increment by 1
cache 20;

 

 
这里显示的start with是21,increment by是1,cache是20。
 
黄勇大师的解释是“Looks like the reverse-engineered sequence definition takes "start with" (1 by default) as "start with" value before the sequence is ever used, but takes "increment by" x "cache" + "start with" after it's used once or a few times.”
 
序列在未使用之前,序列使用"start with(默认是1)"作为起始点,在使用一次或几次之后,序列就会用"increment by "x" cache和"start with"来计数与取值了。
 
 
 
2、序列的定义,包括从dba_sequences中看到的内容,都是存储在Shared Pool的Data Dictionary中。
 
 
 
3、官方未公布的sys.v$_sequences表也会记录一些关于sequence的信息,这些都是从Shared Pool中获得的。
 
 
 
4、dba_sequences中的last_number字段,指高水位线,或者更准确地应该叫存储于Shared Pool中最大的序列值。
 
 
 
5、使用cache的序列,可能会出现断层,原因就是cache的值被purge了。像flush shared pool这种操作就可能purge缓存cache。例如当Shared Pool满了,新的对象(例如SQL)又来了,此时旧的对象就会被aged out,或者使用的ASSM,buffer cache需要更多的SGA空间,因此shrink了Shared Pool。
 
序列出现断层的原因可能有:
 
ROLLBACK。
 
PMON进程执行清理。
 
元数据置换出row cache(也就是Data Dictionary)。
 
实例关闭。
 
 
 
示例:
 
SQL> create sequence seq_1;
Sequence created.

SQL> select seq_1.currval from dual;
select seq_1.currval from dual
       *
ERROR at line 1:
ORA-08002: sequence SEQ_1.CURRVAL is not yet defined in this session

SQL> select seq_1.nextval from dual;
   NEXTVAL
----------
         1
SQL> select seq_1.nextval from dual;
   NEXTVAL
----------
         2
...
SQL> select seq_1.nextval from dual;
   NEXTVAL
----------
        20
SQL> SELECT last_number FROM dba_sequences WHERE sequence_name='SEQ_1';
LAST_NUMBER
-----------
         21

 


CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,