在ORACLE数据库中,序列(SEQUENCE)是使用非常频繁的一个数据库对象,但是有时候会遇到序列(SEQUECNE)跳号(skip sequence numbers)的情形,那么在哪些情形下会遇到跳号呢?
事务回滚引起的跳号
不管序列有没有CACHE、事务回滚这种情况下,都会引起序列的跳号。如下实验所示:
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:
sql;">
exec dbms_lock.sleep(2); --延迟2秒执行,根据你实验情况调整
/
begin
for i in 1 .. 2000 loop
dbms_output.put_line(my_sequence.nextval);
end loop;
end;
/
会话窗口B:
sql;">
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),当然正常情况下,很难遇到这种情况。
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
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跟踪事件,看看正常数据库关闭情况下,会对序列做一些啥操作
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导致跳跃的情况
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跳号总结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程之家网站的支持!
原文链接:https://www.f2er.com/oracle/64799.html