在ORACLE数据库中,序列(SEQUENCE)是使用非常频繁的一个数据库对象,但是有时候会遇到序列(SEQUECNE)跳号(skip sequence numbers)的情形,那么在哪些情形下会遇到跳号呢?
创新互联公司是一家集网站建设,黎平企业网站建设,黎平品牌网站建设,网站定制,黎平网站建设报价,网络营销,网络优化,黎平网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
事务回滚引起的跳号
不管序列有没有CACHE、事务回滚这种情况下,都会引起序列的跳号。如下实验所示:
SQL> create sequence my_sequence 2 start with 1 3 increment by 1 4 maxvalue 99999 5 nocache; Sequence created. SQL> create table test(id number(10), name varchar2(32)); Table created. SQL> insert into test 2 select my_sequence.nextval , 'kerry' from dual; 1 row created. SQL> SQL> rollback; Rollback complete. SQL> select my_sequence.nextval from dual; NEXTVAL ---------- 3 SQL>
并发访问序列引起的跳号
并发访问序列引起的跳号,其实不算真正的跳号,而只是逻辑跳号,只是序列值被其它并发会话使用了。我们来构造一起并发访问序列引起的跳号,我们开启两个会话窗口,循环获取序列的值,模拟并发出现的场景。
会话窗口A:
exec dbms_lock.sleep(2); --延迟2秒执行,根据你实验情况调整 / begin for i in 1 .. 2000 loop dbms_output.put_line(my_sequence.nextval); end loop; end; /
会话窗口B:
spool test.txt; begin waitfor delay '00:00:10'; for i in 1 .. 2000 loop dbms_output.put_line(my_sequence.nextval); end loop; end; / spool off;
如下所示,我构造的实验当中,你会看到序列的跳号情况。
FLUSH SHARED_POOL会导致CACHE的序列跳号
实验测试如下所示(序列的CACHE值必须大于0),当然正常情况下,很难遇到这种情况。
SQL> select test.my_sequence.nextval from dual; NEXTVAL ---------- 17004 SQL> alter sequence test.my_sequence cache 40; Sequence altered. SQL> select test.my_sequence.nextval from dual; NEXTVAL ---------- 17005 SQL> alter system flush share_pool; alter system flush share_pool * ERROR at line 1: ORA-02000: missing SHARED_POOL/BUFFER_CACHE/GLOBAL CONTEXT keyword SQL> alter system flush shared_pool; System altered. SQL> select test.my_sequence.nextval from dual; NEXTVAL ---------- 17045
数据库实例异常关闭导致跳号
如下实验所示,当数据库使用shutdown abort命令关闭后,重新启动实例,序列缓存在shared pool里面没有用过的值都没有了。一下子从17045跳到17085
SQL> select test.my_sequence.currval from dual; CURRVAL ---------- 17045 SQL> select object_id from dba_objects where object_name=upper('my_sequence'); OBJECT_ID ---------- 97760 SQL> select increment$, minvalue, maxvalue,highwater, cache 2 from seq$ where obj#=97760; INCREMENT$ MINVALUE MAXVALUE HIGHWATER CACHE ---------- ---------- ---------- ---------- ---------- 1 1 99999 17085 40 SQL> shutdown abort; ORACLE instance shut down. SQL> startup; ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance ORACLE instance started. Total System Global Area 1620115456 bytes Fixed Size 2213816 bytes Variable Size 1258293320 bytes Database Buffers 352321536 bytes Redo Buffers 7286784 bytes Database mounted. Database opened. SQL> select test.my_sequence.currval from dual; select test.my_sequence.currval from dual * ERROR at line 1: ORA-08002: sequence MY_SEQUENCE.CURRVAL is not yet defined in this session SQL> select test.my_sequence.nextval from dual; NEXTVAL ---------- 17085 SQL>
另外,我们也来看看正常关闭数据库的情况下,序列会不会出现跳号,我们采用10046跟踪事件,看看正常数据库关闭情况下,会对序列做一些啥操作
SQL> select test.my_sequence.nextval from dual; NEXTVAL ---------- 17085 SQL> alter session set events '10046 trace name context forever, level 4'; Session altered. SQL> shutdown immediate; Database closed. Database dismounted. ORACLE instance shut down. SQL> startup ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance ORACLE instance started. Total System Global Area 1620115456 bytes Fixed Size 2213816 bytes Variable Size 1258293320 bytes Database Buffers 352321536 bytes Redo Buffers 7286784 bytes Database mounted. Database opened. SQL> select test.my_sequence.currval from dual; select test.my_sequence.currval from dual * ERROR at line 1: ORA-08002: sequence MY_SEQUENCE.CURRVAL is not yet defined in this session SQL> select test.my_sequence.nextval from dual; NEXTVAL ---------- 17086 SQL>
Trace文件中有更新seq$数据字典表,如果你看过我这篇文章ORACLE中seq$表更新频繁的分析,基本上就知道其实seq$中维护的是序列的一些信息。通过跟踪文件,我们知道在数据库正常关闭的情况下,会触发一个update seq$的操作,把当前的sequence.nextval的值更新到seq$.highwater中,从而使得sequence在有cache的情况下,数据库正常关闭未出现nextval跳跃(currval也同样不跳跃);而在数据库异常关闭之时,数据库不能及时将sequence.nextval更新到eq$.highwater从而引起sequence cache中的值丢失,从而可能出现了sequence使用cache导致跳跃的情况
===================== PARSING IN CURSOR #25 len=129 dep=1 uid=0 oct=6 lid=0 tim=1504236336294194 hv=2635489469 ad='bf780410' sqlid='4m7m0t6fjcs5x' update seq$ set increment$=:2,minvalue=:3,maxvalue=:4,cycle#=:5,order$=:6,cache=:7,highwater=:8,audit$=:9,flags=:10 where obj#=:1 END OF STMT PARSE #25:c=0,e=21,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=4,plh=1935744642,tim=1504236336294194 BINDS #25: Bind#0 oacdty=02 mxl=22(02) mxlc=00 mal=00 scl=00 pre=00 oacflg=18 fl2=0001 frm=00 csi=00 siz=24 off=0 kxsbbbfp=bf45ca48 bln=24 avl=02 flg=09 value=1 Bind#1 oacdty=02 mxl=22(02) mxlc=00 mal=00 scl=00 pre=00 oacflg=18 fl2=0001 frm=00 csi=00 siz=24 off=0 kxsbbbfp=bf45ca5a bln=24 avl=02 flg=09 :/17086 Bind#3 oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00 oacflg=08 fl2=0001 frm=00 csi=00 siz=24 off=0 kxsbbbfp=2b7d80f57350 bln=24 avl=01 flg=05 value=0 Bind#4 oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00 oacflg=08 fl2=0001 frm=00 csi=00 siz=24 off=0 kxsbbbfp=2b7d80f57320 bln=24 avl=01 flg=05 value=0 Bind#5 oacdty=02 mxl=22(02) mxlc=00 mal=00 scl=00 pre=00 oacflg=18 fl2=0001 frm=00 csi=00 siz=24 off=0 kxsbbbfp=bf47b85e bln=24 avl=02 flg=09 value=40 Bind#6 oacdty=02 mxl=22(04) mxlc=00 mal=00 scl=00 pre=00 oacflg=18 fl2=0001 frm=00 csi=00 siz=24 off=0 kxsbbbfp=bf47b870 bln=24 avl=04 flg=09 value=17086 Bind#7 oacdty=01 mxl=32(32) mxlc=00 mal=00 scl=00 pre=00 oacflg=18 fl2=0001 frm=01 csi=873 siz=32 off=0 kxsbbbfp=bf47b882 bln=32 avl=32 flg=09 value="--------------------------------" Bind#8 oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00 oacflg=08 fl2=0001 frm=00 csi=00 siz=24 off=0 kxsbbbfp=2b7d80f572f0 bln=24 avl=02 flg=05 value=8 Bind#9 oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00 oacflg=08 fl2=0001 frm=00 csi=00 siz=24 off=0 kxsbbbfp=2b7d80f57380 bln=22 avl=04 flg=05 value=97760 EXEC #9:c=999,e=709,p=0,cr=1,cu=2,mis=0,r=1,dep=1,og=4,plh=1935744642,tim=1504236336297033 CLOSE #9:c=0,e=2,dep=1,type=3,tim=1504236336297058 mealink上提到了使用dbms_shared_pool.keep将对象在锁定在shared pool 中,永远不释放。这样可以防止FLUSH SHARED POOL导致序列跳号,但是这个无法避免数据库异常关闭或CRASH引起的跳号 SQL> select test.my_sequence.currval from dual; CURRVAL ---------- 17086 SQL> exec dbms_shared_pool.keep('test.my_sequence','q'); PL/SQL procedure successfully completed. SQL> alter system flush shared_pool; System altered. SQL> select test.my_sequence.currval from dual; CURRVAL ---------- 17086 SQL> shutdown abort ORACLE instance shut down. SQL> startup ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance ORACLE instance started. Total System Global Area 1620115456 bytes Fixed Size 2213816 bytes Variable Size 1258293320 bytes Database Buffers 352321536 bytes Redo Buffers 7286784 bytes Database mounted. Database opened. SQL> select test.my_sequence.nextval from dual; NEXTVAL ---------- 17126
其实如果业务允许,单号出现跳号也无所谓的情形最好,如果碰到业务要求绝对不能出现单号出现跳号的情况,那么就不能使用序列号了,就必须使用其它替代方案,此处不做展开说明!
总结
以上所述是小编给大家介绍的分享ORACLE SEQUENCE跳号总结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!
当前文章:分享ORACLESEQUENCE跳号总结
当前路径:http://scpingwu.com/article/ipcscg.html