关闭数据库方式对sequence的影响

create sequence seq1  
minvalue 1  
maxvalue 99999999999999999999  
start with 1  
increment by 1  
cache 1000; 

 select seq1.nextval into var from dual; 

 SQL> select * from user_sequences;

SEQUENCE_NAME			MIN_VALUE  MAX_VALUE INCREMENT_BY C O CACHE_SIZE LAST_NUMBER
------------------------------ ---------- ---------- ------------ - - ---------- -----------
SEQ1					1 1.0000E+20		1 N N	    1000	   1

SQL>  select seq1.nextval  from dual;

   NEXTVAL
----------
	 1

SQL>  select * from user_sequences;

SEQUENCE_NAME			MIN_VALUE  MAX_VALUE INCREMENT_BY C O CACHE_SIZE LAST_NUMBER
------------------------------ ---------- ---------- ------------ - - ---------- -----------
SEQ1					1 1.0000E+20		1 N N	    1000	1001

SQL>  select seq1.nextval  from dual;

   NEXTVAL
----------
	 2

SQL>  select * from user_sequences;

SEQUENCE_NAME			MIN_VALUE  MAX_VALUE INCREMENT_BY C O CACHE_SIZE LAST_NUMBER
------------------------------ ---------- ---------- ------------ - - ---------- -----------
SEQ1					1 1.0000E+20		1 N N	    1000	1001

此时关闭数据库呢?

SQL> shutdown abort;
ORACLE 例程已经关闭。
SQL> startup
ORACLE 例程已经启动。

Total System Global Area  301305856 bytes
Fixed Size		    1336148 bytes
Variable Size		  260050092 bytes
Database Buffers	   33554432 bytes
Redo Buffers		    6365184 bytes
数据库装载完毕。
数据库已经打开。
SQL> conn test/test
已连接。
SQL> set linesize 200
SQL>   select * from user_sequences;

SEQUENCE_NAME			MIN_VALUE  MAX_VALUE INCREMENT_BY C O CACHE_SIZE LAST_NUMBER
------------------------------ ---------- ---------- ------------ - - ---------- -----------
SEQ1					1 1.0000E+20		1 N N	    1000	1001

SQL>  select seq1.nextval  from dual;

   NEXTVAL
----------
      1001

SQL>  select * from user_sequences;

SEQUENCE_NAME			MIN_VALUE  MAX_VALUE INCREMENT_BY C O CACHE_SIZE LAST_NUMBER
------------------------------ ---------- ---------- ------------ - - ---------- -----------
SEQ1					1 1.0000E+20		1 N N	    1000	2001

如果指定CACHE值,Oracle就可以预先在内存里面放置一些Sequence,这样存取的快些。cache里面的取完后,Oracle自动再取一组到cache。

使用cache或许会跳号, 比如数据库突然不正常down掉(shutdown abort),cache中的Sequence就会丢失。

举个例子:比如你的sequence中cache 100,那当你sequence取到90时突然断电,那么在你重启数据库后,sequence的值将从101开始。

如果是正常关闭呢?

SQL> conn test/test
已连接。
SQL> set linesize 200
SQL> select seq1.nextval  from dual;

   NEXTVAL
----------
      1003

SQL>  select * from user_sequences;

SEQUENCE_NAME			MIN_VALUE  MAX_VALUE INCREMENT_BY C O CACHE_SIZE LAST_NUMBER
------------------------------ ---------- ---------- ------------ - - ---------- -----------
SEQ1					1 1.0000E+20		1 N N	    1000	2003

正常关闭数据库sequence正常,不丢失

原文地址:https://www.cnblogs.com/hzcya1995/p/13352011.html