본문 바로가기
DBMS/ORACLE

시퀀스(sequence) last_number 의미와 alter sequence 시 변화

by 드바 2022. 5. 11.

 

dba_sequences.last_number 값의 의미

dba_sequences.last_number 값은 cache가 반영된 값이다. cache 가 20이고 last_number 11일 경우 nextval 하면 11이 채번된다.

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME		     MIN_VALUE						 MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- ------------------------------ ---------- --------------------------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1				     1			      9999999999999999999999999999	      1 N   N		20	    11

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


SQL> select sk.seq1.nextval from dual;

   NEXTVAL
----------
	11

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME		     MIN_VALUE						 MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- ------------------------------ ---------- --------------------------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1				     1			      9999999999999999999999999999	      1 N   N		20	    31

SQL> select sk.seq1.nextval from dual;

   NEXTVAL
----------
	12

SQL> select sk.seq1.nextval from dual;

   NEXTVAL
----------
	13

SQL> /

   NEXTVAL
----------
	14
...
...
   NEXTVAL
----------
	29

SQL> /

   NEXTVAL
----------
	30

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME		     MIN_VALUE						 MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- ------------------------------ ---------- --------------------------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1				     1			      9999999999999999999999999999	      1 N   N		20	    31

SQL> select sk.seq1.nextval from dual;

   NEXTVAL
----------
	31

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME		     MIN_VALUE						 MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- ------------------------------ ---------- --------------------------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1				     1			      9999999999999999999999999999	      1 N   N		20	    51

SQL>

 

사용 중인 시퀀스 alter로 변경 시 last_number 의 변화

alter sequnce 시 nextval 로 채번될 값으로 변경되는것이 확인 된다.

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

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME	   MIN_VALUE			    MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- -------------------- ---------- -------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1			   1	 9999999999999999999999999999		 1 N   N	   20	       51

SQL> select sk.seq1.nextval from dual;

   NEXTVAL
----------
	38

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME	   MIN_VALUE			    MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- -------------------- ---------- -------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1			   1	 9999999999999999999999999999		 1 N   N	   20	       51

SQL> alter sequence sk.seq1 order ;

Sequence altered.

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME	   MIN_VALUE			    MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- -------------------- ---------- -------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1			   1	 9999999999999999999999999999		 1 N   Y	   20	       39

SQL> select sk.seq1.currval from dual;

   CURRVAL
----------
	38

SQL> select sk.seq1.nextval from dual;

   NEXTVAL
----------
	39

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME	   MIN_VALUE			    MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- -------------------- ---------- -------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1			   1	 9999999999999999999999999999		 1 N   Y	   20	       59

SQL> select sk.seq1.currval from dual;

   CURRVAL
----------
	39

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME	   MIN_VALUE			    MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- -------------------- ---------- -------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1			   1	 9999999999999999999999999999		 1 N   Y	   20	       59

SQL> alter sequence sk.seq1 noorder ;

Sequence altered.

SQL> select sk.seq1.currval from dual;

   CURRVAL
----------
	39

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME	   MIN_VALUE			    MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- -------------------- ---------- -------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1			   1	 9999999999999999999999999999		 1 N   N	   20	       40

SQL> select sk.seq1.currval from dual;

   CURRVAL
----------
	39

SQL> select sk.seq1.nextval from dual;

   NEXTVAL
----------
	40

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME	   MIN_VALUE			    MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- -------------------- ---------- -------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1			   1	 9999999999999999999999999999		 1 N   N	   20	       60

SQL> alter sequence sk.seq1 cache 100 ;

Sequence altered.

SQL> select sk.seq1.currval from dual;

   CURRVAL
----------
	40

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME	   MIN_VALUE			    MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- -------------------- ---------- -------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1			   1	 9999999999999999999999999999		 1 N   N	  100	       41

SQL>  select sk.seq1.nextval from dual;

   NEXTVAL
----------
	41

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME	   MIN_VALUE			    MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- -------------------- ---------- -------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1			   1	 9999999999999999999999999999		 1 N   N	  100	      141

SQL>
SQL> select sk.seq1.nextval from dual;

   NEXTVAL
----------
	42

SQL> select sk.seq1.currval from dual;

   CURRVAL
----------
	42

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME	   MIN_VALUE			    MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- -------------------- ---------- -------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1			   1	 9999999999999999999999999999		 1 N   N	  100	      141

SQL> alter sequence sk.seq1 INCREMENT by 2 ;

Sequence altered.

SQL> SELECT * FROM DBA_SEQUENCES WHERE sequence_owner = 'SK' ;

SEQUENCE_OWNER	     SEQUENCE_NAME	   MIN_VALUE			    MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE LAST_NUMBER
-------------------- -------------------- ---------- -------------------------------- ------------ --- --- ---------- -----------
SK		     SEQ1			   1	 9999999999999999999999999999		 2 N   N	  100	       44

SQL> select sk.seq1.currval from dual;

   CURRVAL
----------
	42

SQL> select sk.seq1.nextval from dual;

   NEXTVAL
----------
	44

SQL>

 

 

아래는 어딘가에서 보고 저장해두었던 RAC 환경에서 시퀀스 cache 및 order 속성에 따른 동작 방식

Sequence Cache 속성
• CACHE속성
○ NOCAHCE
○ CACHE + NOOREDER
○ CACHE + ORDER
• 각 속성 유형마다 고유의 성능 문제 존재
• enq:SQ - contention 참조


Sequence Cache속성별 발생 Event

 

NOCACHE

sequence(nocache)


• nocache속성인 경우, 인스턴스 친화도는 0임
• 로컬자원은 존재하지 않으며, 시퀀스의 nextval를 호출할 때마다 row cache lock으로 동기화하여야 함

 

CACHE+NOORDER

sequence(cache+noorder)


• 2노드의 RAC에서 cache 속성 100의 시퀀스를 사용한다면,
○ 노드A에서 1~100사이 로컬 캐시 할당되고
○ 노드B에서 101~200사이 로컬 캐시 할당됨
• 로컬캐시가 모두 소진되면, 로컬캐시가 할당될 때까지 SQ락이 대기함
• 로컬캐시를 모두 소진하기 전까지는 nextval을 호출시 동기화 작업이 불필요하며 시퀀스 캐시 크기가 클 수록, 인스턴스 친화도가 좋아짐

 

CACHE+ORDER

sequence(cache+order)



• 2노드의 RAC에서 cache 100 + order속성의 시퀀스를 사용하면,
○ 노드A와 노드B 모두 1~100사이의 로컬캐시 집합을 사용함
• Nextval을 호출 할 때마다 SV락을 이용한 동기화가 필요하며, 이때 DFS lock handle을 대기함
• 10gR2부터 DFS lock handle 이벤트는 placeholder이벤트이고, events in waitclass other 라는 fixed-up이벤트로 기록됨


Sequence Cache속성 구현 Guide
• NOCACHE는 사용하지 말아야
• CACHE크기는 사용이 빈번할 수록 크게
○ SQ Lock과 SV Lock경합 해소
○ Index Block의 Global경합 해소
• ORDER속성은 꼭 필요한 경우에만 사용


Sequence와 Index Block경합
Cache=20인 경우
• Instance간에 같은 Leaf Block에 대한 경합 발생

sequence(cache)


• 예제
○ 시퀀스를 사용하여 기본키(pk-uk)를 생성한다면?
○ 캐시 크기 20의 시퀀스를 사용하는 경우
○ 노드 A는 1~20, 노드 B는 21~40 사이의 캐시값을 가지며 값의 차이가 그리 크지 않으므로, 두 노드가 동일 인덱스 리프 블록을 변경하게되며, 블록 경합이 유발됨
○ 인덱스 리프 블록 경합으로 인하여 buffer busy waits또는 TX-index contention이 발생

 

Cache=10000인 경우

• Instance간에 서로 다른 Leaf Block을 사용

sequence_cache_10000


• 예제
○ 시퀀스를 사용하여 기본키(pk-uk)를 생성한다면?
○ 캐시 크기 10000의 시퀀스를 사용하는 경우
○ 노드 A는 1~10000, 노드 B는 10001~20000 사이의 캐시값을 가지며 값의 차이가 그리 크므로, 두 노드가 다른 인덱스 리프 블록을 변경하게됨
○ 따라서 인덱스 리프 블록에 대한 경합이 발생할 확률이 적어짐

 

'DBMS > ORACLE' 카테고리의 다른 글

선분이력 테이블  (0) 2022.05.25
RAC relink 작업  (0) 2022.05.17
ASM DISK 사이즈 다른 경우 DATAFILE 추가 테스트  (0) 2022.05.10
ARCHIVE(아카이브) HANG 조치, ORA-16020  (0) 2022.03.28
redo log 사이즈 변경 RAC ASM  (0) 2022.03.25

댓글