SEQUENCE CACHE
제품 : ORACLE SERVER
작성날짜 : 2003-08-04
Sequence Cache
오라클 sequence를 사용할 때에 cache라는 옵션을 가지고 있다.
이러한 옵션을 사용할때 가끔 수자가 이어지지 않고 끊어지는 경우가 발생할 수
있는데 여기서는 어떤 상황에서 수자가 없어지며 이러한 상황을 최소화할수 있는
방법을 찾아보고자 한다.
non-cache mode 인 경우에 'nextval' 을 요구하게 되면 current value에서 increment
만큼의 값이 증가될 것이다. 예를 들어 current value는 0이고 increment는 1이며
cache가 아닐 때 'nextval'을 요구하게 되면 1이 return 될 것이다.
cache option과 같이 사용될 때 sequence에 대해서 'nextval'을 요구하게 되면
cache 되는 만큼의 값에서부터 차례대로 next 값을 가져오게 되며 cache된 값을
모두 사용하고 난 다음은 다음 caching 을 하게 된다.
만약 다음과 같이 sequence가 선언되었다고 하자.
create sequence seq increment by 1 cache 5;
NUMBER SEQUENCE CACHE
RETURNED CURRENT CURRENT
start none 0 none
1st access 1 5 1
2nd access 2 5 2
3rd access 3 5 3
4th access 4 5 4
5th access 5 5 5
6th access 6 10 6
7th access 7 10 7
cache되어 있는 값들이 절대로 없어지지 않는다고 가정하면 출력되는 결과 값들은
non-cache 의 그것과 다를 것이 없을 것이다.
그러나 sequence cache도 역시 다른 cached information과 같이 shared pool 에 저장된다.
이것은 다른 shared pool에 있는 procedure처럼 자주 access 되지 않으면 age out될 수
있음을 의미하기도 한다. 또한 shutdown 시에는 cache에 있는 모든 것을 잃어버리게
된다. 여기서 주로 shutdown에 의해 sequence의 번호가 skip 될수 있으므로 skip되어서는
안되는 번호를 가진 응용프로그램에서는 non-cached sequence 를 사용해야 한다.
다음에는 cache age out과 shutdown이후에 sequence number의 변화를 예를 들어 설명한다.
NUMBER SEQUENCE CACHE
RETURNED CURRENT CURRENT
start none 0 none
1st access 1 5 1
2nd access 2 5 2
cache aged out
3rd access 5 10 5
4th access 6 10 6
shutdown
5th access 10 15 10
6th access 11 15 11
7th access 12 15 12
aging 문제를 해결하기 위해서 7.3 이후에서 부터는 dbms_shared_pool 이라는 package 를
이용해서 shared pool내에 다음과 같이 고정시켜 놓을수 있다.
dbms_shared_pool.keep('seq','Q');
Reference Documents
<Note:62002.1>
제품 : ORACLE SERVER
작성날짜 : 2003-08-04
Sequence Cache
오라클 sequence를 사용할 때에 cache라는 옵션을 가지고 있다.
이러한 옵션을 사용할때 가끔 수자가 이어지지 않고 끊어지는 경우가 발생할 수
있는데 여기서는 어떤 상황에서 수자가 없어지며 이러한 상황을 최소화할수 있는
방법을 찾아보고자 한다.
non-cache mode 인 경우에 'nextval' 을 요구하게 되면 current value에서 increment
만큼의 값이 증가될 것이다. 예를 들어 current value는 0이고 increment는 1이며
cache가 아닐 때 'nextval'을 요구하게 되면 1이 return 될 것이다.
cache option과 같이 사용될 때 sequence에 대해서 'nextval'을 요구하게 되면
cache 되는 만큼의 값에서부터 차례대로 next 값을 가져오게 되며 cache된 값을
모두 사용하고 난 다음은 다음 caching 을 하게 된다.
만약 다음과 같이 sequence가 선언되었다고 하자.
create sequence seq increment by 1 cache 5;
NUMBER SEQUENCE CACHE
RETURNED CURRENT CURRENT
start none 0 none
1st access 1 5 1
2nd access 2 5 2
3rd access 3 5 3
4th access 4 5 4
5th access 5 5 5
6th access 6 10 6
7th access 7 10 7
cache되어 있는 값들이 절대로 없어지지 않는다고 가정하면 출력되는 결과 값들은
non-cache 의 그것과 다를 것이 없을 것이다.
그러나 sequence cache도 역시 다른 cached information과 같이 shared pool 에 저장된다.
이것은 다른 shared pool에 있는 procedure처럼 자주 access 되지 않으면 age out될 수
있음을 의미하기도 한다. 또한 shutdown 시에는 cache에 있는 모든 것을 잃어버리게
된다. 여기서 주로 shutdown에 의해 sequence의 번호가 skip 될수 있으므로 skip되어서는
안되는 번호를 가진 응용프로그램에서는 non-cached sequence 를 사용해야 한다.
다음에는 cache age out과 shutdown이후에 sequence number의 변화를 예를 들어 설명한다.
NUMBER SEQUENCE CACHE
RETURNED CURRENT CURRENT
start none 0 none
1st access 1 5 1
2nd access 2 5 2
cache aged out
3rd access 5 10 5
4th access 6 10 6
shutdown
5th access 10 15 10
6th access 11 15 11
7th access 12 15 12
aging 문제를 해결하기 위해서 7.3 이후에서 부터는 dbms_shared_pool 이라는 package 를
이용해서 shared pool내에 다음과 같이 고정시켜 놓을수 있다.
dbms_shared_pool.keep('seq','Q');
Reference Documents
<Note:62002.1>
Similar Messages
-
Sequence caching with toplink and oracle caching
Hi,
In my application, I use toplink as a JPA provider. For performance reasons, I defined an allocationSize on sequence generator for some persistence objects. I read some documentation to understand how it works, the result of my research is that the allocationSize of sequence generator must match with the increment value of the sequence in database.
So if I want to cache 1000 of values, I must define on my sequence generator allocationSize of 1000 and create a sequence like this : create sequence myseq increment by 1000. Am I right ?
Second question, what if my sequence define a cache in the database. For example, create sequence myseq increment by 1000 cache 20.
Does it mean that 20 values will be cached or 1000*20 values will be cached ? Is the cache size defined per session or for all the sessions of the database ?
Thanks.
Will.Thank you for your response.
I used the default parameter of toplink (increment by 50) and I left a cache size of 20 on database side.
But I noticed that whenever the server restarts, I "lost" between 900 and 1000 ids (nearly 20*50). So even if sequence cache on oracle size is non transactional, I think there is a kind of prefetch somewhere on the server side. I must not lost more than 50 ids when a server restarts.
If I remove the cache on database side, then the behavior is correct (the expected one to be more precise).
Is there any kind of sequence caching in oracle jdbc drivers ?
Thanks.
Will. -
Oracle 10g sequence cache aging
I have been reading some about how sequences work and why you can end up with gaps in the sequence numbers. It is my understanding that you could 'lose' certain sequence numbers when the library cache ages/expires. What I can't find is where this cache is configured. Where do you define when the cache will expire and thus clear out? Seems like it's happening very quickly in one of our databases, but much more slowly in another. Don't know where to look for this setting. Can anyone help point me in the right direction? Thanks!
- The size of the sequence cache is driven by the CACHE parameter (default 20) when you create the sequence
- Oracle manages the library cache automatically-- there are no parameters to set here for the timeout. When a particular object is aged out is going to depend on, among other things, the size of the library cache, the frequency a particular object is used, and the number of other objects competing for space in the library cache. If one sequence is constantly being used, it will stay in the library cache much longer than the cache for a less frequently used sequence. If one database's library cache is under pressure because you're constantly loading new objects, cached objects will be aged out far more quickly than in a database where the library cache is not under pressure.
Justin -
if there is a sequence with CACHE 20, are numbers that are allocated from the cache going to be ordered?. So if I have values 61..80 in the sequence cache, is the next NEXTVAL call GUARANTEED to get 61, or will it get any random value from the cache
thanksIn a RAC environment, by default, sequence values can be returned out of order. There is an ORDER attribute that can be added to he DDL to create a sequence to force sequence values to be returned in order (though I'd wager this has a decent performance cost).
In a single instance database, sequence values will be returned sequentially whether the sequence is created as ORDER or NOORDER.
Of course, even if sequences come in order, it's possible that when you have concurrent transactions, the one with the later sequence value manages to commit before the one with the earlier sequence value. This may or may not be a concern in your environment.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
From the oracle documentation, http://docs.oracle.com/cd/E11882_01/server.112/e10701/original_export.htm#BABJHCDH
it says that sequence numbers that have been allocated are available for use in the current database.
I have a table Test which has a column test_id having a sequence test_sequence.
Max value in the test_id is 20 and assume if test_sequence is cached for 20 in a 2 RAC node with each node caching numbers from 20-30 and 30-40.
If I do an export and import the sequences to a new database, will my next sequence number in the new database be 41 ?
If so, what will be the LAST_NUMBER in the dba_Sequences table in the source database.
Thanks in advance.Hi,
if you set CACHE 20 in a RAC environment each node will cache 20 number. In your case - cached numbers will be 21-40 and 41-60 and the dba_sequences.last_number=60.
If you do export/import all the cached numbers will be lost and your first number will be last_number+1. -
I created my sequence to increment by 1 with a default cache size of 20. This works fine as long as all my entries are done in the same session ( n + 1, n + 1... and so on). However, when I exit/return to my app the new sequence begins like: Last n + 20, n + 1, n + 1. . . and so on. Is it possible to reduce the cache size of a sequence?
Leo,
Please see this post:
Sequence problem
Particularly, Tom Kyte's explanation here:
http://asktom.oracle.com/pls/ask/f?p=4950:8:::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:530735152441
Thanks,
- Scott - -
Hi,
In my application, I use toplink as a JPA provider. For performance reasons, I defined an allocationSize on sequence generator for some persistence objects. I read some documentation to understand how it works, the result of my research is that the allocationSize of sequence generator must match with the increment value of the sequence in database.
So here is my question, what if my sequence define a cache in the database ? For a cache of 1000 values on the application (or server) side, I can create a sequence like this : sequence myseq increment by 1000 cache 20.
Does it mean that 20 values will be cached or 1000*20 values will be cached ? Is the cache size defined per session or for all the sessions of the database ?
Thanks.
Will.ok.... so if my sequence starts with 10000 and has a cache of 5, values from 11000 to 15000 will be cached, not values from 10000 to 100020 ?"5" values will be cached. You will find the following example to be helpful.
[cache]
SQL> create sequence myseq start with 10000 cache 5;
Sequence created.
SQL> select increment_by, cache_size, last_number from user_sequences where sequence_name = 'MYSEQ';
INCREMENT_BY CACHE_SIZE LAST_NUMBER
1 5 10000
SQL> select myseq.nextval from dual;
NEXTVAL
10000
SQL> /
NEXTVAL
10001
SQL> /
NEXTVAL
10002
SQL> /
NEXTVAL
10003
SQL> /
NEXTVAL
10004
SQL> select increment_by, cache_size, last_number from user_sequences where sequence_name = 'MYSEQ';
INCREMENT_BY CACHE_SIZE LAST_NUMBER
1 5 10005
SQL> select myseq.nextval from dual;
NEXTVAL
10005
SQL> select increment_by, cache_size, last_number from user_sequences where sequence_name = 'MYSEQ';
INCREMENT_BY CACHE_SIZE LAST_NUMBER
1 5 10010
SQL>
Asif Momen
http://momendba.blogspot.com -
I am CONFUSED SEQUENCE CACHE BUG OR NOT
I HAVE CONFUSED, I HAVE CREATE ONE SEQUENCE AND I HAVE TEST BELOW PRACTICAL OF SEQUENCE
STEP 1 --
CREATE SEQUENCE SEQ_TEST
START WITH 1
MAXVALUE 999999
CACHE 20 ;
STEP 2 --
CREATE TABLE TEST_SEQUENCE (NO NUMBER) ;
STEP-3 --
INSERT INTO TEST_SEQUENCE VALUES(SEQ_TEST.NEXTVAL);
STEP-4 --
SELECT * FROM TEST_SEQUENCE;
STEP --5
CREATE SEQUENCE SEQ_TEST
START WITH 1
MAXVALUE 999999
CACHE 20 ;
ORA-00955: NAME IS ALREADY USED BY AN EXISTING OBJECT
STEP-6 --
INSERT INTO TEST_SEQUENCE VALUES(SEQ_TEST.NEXTVAL);
STEP-7 --
SELECT * FROM TEST_SEQUENCE;
I AM NOT SURE, SO PLEASE SUGGESTIONHi gunjan ,
I copied all your code and executed ,its working file there is nothing wrong .
But the problem is, as you are using cache ,some times it will skip 20 nos .
visit the beloiw link for further explanation
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:530735152441
Plz mark answered .
Regards
Timir -
Increase sequence cache - DWH Environment
Hello everyone,
I am writing to ask a question on the sequences.
We work on a RAC with two nodes.
We have some sequences, highly used by several job.
The cache of these sequences is set with the default value.
We would increase the performance of access to the sequences.
Is it worth it?
How can we make an estimate on the correct or most plausible value of the cache?
Thanks
Greetings
SandroHi,
It all depends on how are you using the sequences. You need to think about two main points: ordering and caching. Do the sequences MUST be ordered? If that's true than those sequences have to be created using ORDER and NOCACHE clauses but this will lead to poor performance on a RAC environment because you will increase the overhead to maintain consistency about the sequence. If the sequences don't need to be ordered and you can have different numbers accross the instances, than you should use NORDER and CACHE for best performance.
Take a look at the doc in MOS Doc 853652.1 (RAC and Sequences)
Regards. -
Sequence cache (can it cause lock when.....)
it is set to 0?
We have a sequence that is used as a key in a table. If many users try to select next val from it at once and the cache is 0, can this cause the application to lock?
(I know this sounds far fitch but the only question not ask is a stupid question)I would also ask why you were setting a sequence to
NOCACHE if there is the potential for many users to
access it simultaneously. Frequently used sequences
ought to have appropriately sized caches.
Folks that set sequences to NOCACHE are frequently
trying to prevent gaps in their sequences. While
setting a sequence to NOCACHE will tend to decrease
the probability of gaps, it will not eliminate them.
If your application absolutely requires a gap-free
e sequence, you cannot use Oracle sequences and you
have to deal with the (significant) performance
penalty of serializing all transactions on the next
sequence value.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
It was not the idea of the dba here, but the application programmers (we should be shot for allowing them to tell us this). They did not believe us when we told them that the sequences would not roll back... I reset the cache to a number other than 0...
Thanks all for the feedback -
Hi All,
Production server configuration is 4 node RAC. Version is 11.2.0.3. Linux Platform. We are using sequences in DB.
I was reading some blogs on sequence working on RAC env. I came to know a sequence with cache noorder will improve the performance.But i have not seen how can we cache a sequence properly in RAC.Can you please let me know your suggestions for the below questions?
When we create any sequence, how can we cache a sequence properly on 4 instances?
How can we calculate the cache for each instance?
How shall we know whether sequence is using or not?
Thanks,
ManiProduction server configuration is 4 node RAC. Version is 11.2.0.3. Linux Platform. We are using sequences in DB.
I was reading some blogs on sequence working on RAC env. I came to know a sequence with cache noorder will improve the performance.But i have not seen how can we cache a sequence properly in RAC.Can you please let me know your suggestions for the below questions?
When we create any sequence, how can we cache a sequence properly on 4 instances?
How can we calculate the cache for each instance?
How shall we know whether sequence is using or not?
The 'user' doesn't cache the sequence, Oracle does. Just specify the CACHE value when you create the sequence.
Oracle will use the SAME cache value for each instance. So if the cache value is 20 one instance might cache 1-20, the next instance cache 21-40 and so on. When an instance needs more values it will get the next set of 20 values that are available for that sequence.
Which 'cache' gets used depends on which instance a query runs on. A query that executes on instance #2 will use the cache on instance #2. -
Hi All,
I am getting contention problem on SEQ$ table becuase of sequence numbers are generated in huge amount for some sequences everyday. Below I mentioned the figures. I am planning to cache the more sequence number instead of default 20.
I want to know, Is there any drowback to cache more sequence number? Is there any formula or calculation or thumbrule that we can figure out how many number should we cache?
Please also suggest me if any good documnet available on that.
Thanks in advance.
Here is the sequence statistics.
Sequence Maxval (21/06/07) Maxval(22/06/07) Diff(F-E)
Seq-1 967989200 975861637 7872437
Seq-2 95990500 96766097 775597
Seq-3 73371394 73944908 573514
Seq-4 47959122 48336988 377866
Seq-5 33055165 33327676 272511
Seq-6 10407032 10488940 81908
Seq-7 8027641 8108770 81129
Seq-8 8031524 8109764 78240
Seq-9 8311000 8379200 68200
Seq-10 7204295 7256597 52302
Seq-11 5511145 5546865 35720
Seq-12 1594791 1617743 22952
Seq-13 1375542 1385085 9543
Seq-14 1802450 1808251 5801
Seq-15 240624 242455 1831
Seq-16 152600 153843 1243
Seq-17 157491 158605 1114
Seq-18 28701 28840 139
Seq-19 43367 43501 134
Seq-20 28459 28584 125In a very busy environment cache default 20 to my mind definitely is too low. We had an app on RAC (which means each serialization point hit even harder) and increasing cache really made faster inserts especially bigger loads. Depending on the types of tables (i.e. how much inserts) we increased cache even till 10K. I personally don't know about any drawbacks except that you'll loose more values in case of db restart. But for example for number(15) even if you loose 10K values each second your sequence will work for 999999999999999/10000/60/60/24/365 ~=~ 3171 years. That should be enough even for the most exigent future planner on the earth :)
Gints Plivna
http://www.gplivna.eu -
Hi, I have some sequences in cache and i'm losing the cache with the instance "ON".
I'm using 10g.
The sequence's cache uses LRU list? What can I do?
Thanks.The sequence cache is in the library cache.
You will loose sequences by bouncing the instance amongst other.
This is not a problem, as sequences are used for surrogate keys, and as surrogate keys don't have a meaning (except for bean counters), you can have a surrogate key with holes.
If you want to minimize the impact create your sequences with the nocache clause.
If you want a surrogate without holes, revert to the sequence table. Remember this 'solution' scales badly.
Sybrand Bakker
Senior Oracle DBA -
Oracle 11g : Sequence Issues after impdp
Hi All,
We are migrating from Oracle 10g to Oracle 11g. As part of this, we take a expdp from 10g and successfully did an import using impdp on to Oracle 11g database.
But the problem is, there were few Primary key violations which occurred and all of them relate to sequences.
The Maximum data in the tables and the last number in the sequences differed which caused the issue.
Upon investigation and browsing the Web, http://www.nerdliness.com/article/2009/03/18/my-oracle-sequencedatapump-shenanigans ; I understood it could be because of the export taken while the application is online and writing to the database.
I reset all the failing sequences manually and it is fine now.
My questions now are
*1) Can we ascertain that the export taken when the source database is offline would eliminate the sequences issue.*
2) As this is being done in Production, I would like to make few checks to ensure that the sequences are properly imported. ---
Again reading few websites and oracle Forums, i found the below sql's..
select table_name, column_name, utl_raw.cast_to_number(high_value) as highval
from dba_Tab_columns
where owner = 'PRODUCTION_OWNER'
AND DATA_TYPE= 'NUMBER'
AND (OWNER, TABLE_NAME, COLUMN_NAME) IN
(SELECT CC.OWNER, CC.TABLE_NAME, CC.COLUMN_NAME
FROM DBA_CONS_COLUMNS CC
JOIN DBA_CONSTRAINTS C
ON CC.OWNER=C.OWNER
AND CC.CONSTRAINT_NAME=C.CONSTRAINT_NAME
WHERE C.CONSTRAINT_TYPE ='P'
ORDER BY 3;
SELECT SEQUENCE_NAME, MIN_VALUE, MAX_VALUE, LAST_NUMBER
FROM DBA_SEQUENCES
WHERE SEQUENCE_OWNER = 'PRODUCTION_OWNER'
ORDER BY LAST_NUMBER
If I relate the last_number with the highval and if they are same, does that mean the sequences are imported properly.
Note: We have sequence caching done and we are on RAC.
Edited by: ramakrishnavydyula on Jan 30, 2013 9:36 AMWe are migrating from Oracle 10g to Oracle 11g. As part of this, we take a expdp from 10g and successfully did an import using impdp on to Oracle 11g database.
But the problem is, there were few Primary key violations which occurred and all of them relate to sequences.
The Maximum data in the tables and the last number in the sequences differed which caused the issue.
it could be because of the export taken while the application is online and writing to the database.I don't know what's your database's size , and what's acceptable downtime for you, but I could never propose such a migration tecnique to any of my customers, due to excessive downtime when databases are quite large.
It's quite obvious that you get such errors when the export is done while people are working, unless you use FLASHBACK_SCN or FLASHBACK_TIME parameter while exporting. But in this case, assuming you don't get errors (e.g. ORA-01555), you'll probably lose a lot ot transactions.
Assuming your 10g database is running in archivelog mode (which should be the default in production DBs) did you think of using RMAN to do the migration ? -
Why will this error cause a sequence gap?
I am learning the sequence object and found this weird thing happen. Can anyone explain to me why the next value jump to 21 after the error? I guess it may be related to the sequence cache which defaults to 20.
C:\>sqlplus hr/hr@ora11gr2
SQL*Plus: Release 11.2.0.1.0 Production on Fri Apr 20 17:23:27 2012
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> create table t (col number);
Table created.
SQL> create sequence t_seq;
Sequence created.
SQL> insert into t values (t_seq.nextval);
1 row created.
SQL> select * from t;
COL
1
SQL> create sequence t_seq;
create sequence t_seq
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL> insert into t values (t_seq.nextval);
1 row created.
SQL> select * from t;
COL
1
21Interesting, my sequence starts with 2:
BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
5 rows selected.
SQL> drop sequence t_seq;
drop sequence t_seq
ERROR at line 1:
ORA-02289: sequence does not exist
SQL> drop table t purge;
drop table t purge
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> create table t (col number);
Table created.
SQL> create sequence t_seq;
Sequence created.
SQL> insert into t values (t_seq.nextval);
1 row created.
SQL> select * from t;
COL
2
1 row selected.
SQL> create sequence t_seq;
create sequence t_seq
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL> insert into t values (t_seq.nextval);
1 row created.
SQL> select * from t;
COL
2
21
2 rows selected.However, I agree with Justin anyway: when using sequences, you shouldn't worry about gaps.
Maybe you are looking for
-
Vendor Master (Purchasing) Field selection change
I'm trying to change a field in the vendor master (Field=Industry/BRSCH). I would like to change this only for specific Purchase Orgs. But the config only seems to allow me to change by Account groups, Company code or activity/ transaction code. Is
-
Manipulate values in payload without mapping?
Hello everybody, is it possible to manipulate values in an interface (payload) without mapping? Regards Mario
-
Hi BAPI_ENTRYSHEET_CREATE Problem
Hi abapers we have requirement to call BAPI_ENTRYSHEET_CREATE from JAVA throguh JCO. but the COMMIT is already writeen by sap inisde ABAP Source code. but when JCO called BAPI_ENTRYSHEET_CREATE Multiple times java people getting the error messgae as
-
What kind of check should I do? Substring helping
Hi guys, I'm struggling to fin a solution to this problem: I'm building a package in SSIS that allow me to load, transform, check thousand of xls file. Everything is ok but now I ran into a logic issue ( and maybe I'm too tired to concentrating on pr
-
Hello, Time Machine quit backing up a few days ago, and I realized my external backup hard drive icon had disappeared from my desktop. Tried rebooting, etc. but no change. Here's the perplexing part...System Profiler shows "Unknown Device" under Fire