Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle sequence caching

I am trying to implement a sequence in an Oracle database to act as a surrogate key creator for a table. For performance reasons, I want this sequence to be cached. I have read that there are potential pitfalls when using cached sequences since rollbacks and instance failures will result in missed values.

This got me to thinking. Let's say I create a sequence with a cache size of 100. Then I make a 50 record insert to my table, with the sequence value as the primary surrogate key. After the commit, the current value of the sequence would not yet have been written to disk. Suppose I were to have an instance failure at this point. When the database comes back up, it is my understanding that the current sequence value will be reset to the last value written to disk.

If I were to try inserting another 50 records into my table, will I now break the primary key constraint because the sequence was reset to its last state from disk and primary keys are now getting reused? If this is the case, how would I prevent this?

like image 701
Melvin Avatar asked Mar 04 '10 18:03

Melvin


People also ask

What is CACHE in SQL sequence?

The cache is maintained in memory by tracking the current value (the last value issued) and the number of values left in the cache. Therefore, the amount of memory used by the cache is always two instances of the data type of the sequence object.

What are the advantages of caching sequence values?

7. What are the advantages of caching sequence values? Using cache option gives a slight performance advantage as the numbers are pre-allocated and stored in-memory. 8.

Does Oracle have a CACHE?

Oracle Database Cache provides a middle-tier cache for storing data and an easy-to-use management interface for managing the Oracle Database Cache environment. Oracle Database Cache caches and manages data in entities called data sets.

Where are Oracle sequences stored?

Answer: The values for a sequence are internal to Oracle but most guru's says that the values are stored in the SYS. SEQ$ table with supplemental parts in the OBJ$ table, with the OBJ# column as the join key.


2 Answers

No, this will not be the case.

Your sequence will continue at 101, the values between 50 and 100 will be missing.

The only reason to disable sequence caching is when trying to avoid gaps in your sequence, which is not relevant for most Primary Keys.

You might be interested in this article, which states that

The downside of creating a sequence with a cache is that if a system failure occurs, all cached sequence values that have not be used, will be "lost". This results in a "gap" in the assigned sequence values. When the system comes back up, Oracle will cache new numbers from where it left off in the sequence, ignoring the so called "lost" sequence values.

like image 141
Peter Lang Avatar answered Nov 10 '22 10:11

Peter Lang


Say the cache has the values 101-200. The value written on disk is 201. Your insert uses 101-150. Instance goes down. Instance starts up. Next time the sequence is used 201-300 will be cached.

like image 20
Todd Pierce Avatar answered Nov 10 '22 08:11

Todd Pierce