Difficulty: Medium
Correct Answer: CACHE reserves a set of sequence numbers in memory to improve performance at the cost of possible gaps if the instance fails, while NOCACHE generates each value without preallocation, reducing gaps from crashes but with more overhead.
Explanation:
Introduction / Context:
When creating an Oracle sequence, you can choose CACHE or NOCACHE to control how sequence values are allocated and stored. These options influence both performance and the likelihood of gaps in the numeric series. This question asks what CACHE and NOCACHE mean and why you might choose one over the other for a particular application.
Given Data / Assumptions:
Concept / Approach:
The CACHE option instructs Oracle to preallocate a certain number of sequence values and keep them in memory, for example CACHE 20. When sessions request NEXTVAL, the database can return values from this in memory cache quickly without repeatedly accessing the data dictionary. However, if the instance shuts down unexpectedly, any unused cached values are lost, creating gaps in the sequence. NOCACHE disables this preallocation so that each NEXTVAL request causes the database to obtain the next value without using a cache, which may reduce gaps from crashes but increases overhead.
Step-by-Step Solution:
Step 1: Consider CREATE SEQUENCE order_seq START WITH 1 INCREMENT BY 1 CACHE 20; which caches twenty numbers in memory for quick allocation.Step 2: When many inserts happen, the cache reduces contention and IO, improving performance because the database does not need to update persistent storage for every single NEXTVAL.Step 3: Recognise that if the database instance fails before using all cached numbers, some values will never be issued, leading to gaps in the numeric sequence.Step 4: With NOCACHE, Oracle does not preallocate values, so each NEXTVAL requires additional work, which can slow down very high throughput systems but may slightly reduce the number of gaps introduced by instance failures.Step 5: Summarise that CACHE trades potential gaps for speed, while NOCACHE trades speed for slightly fewer crash related gaps, which matches option A.
Verification / Alternative check:
You can observe performance differences by benchmarking a large number of inserts using a cached sequence versus a non cached one. The cached sequence generally shows less contention and higher throughput. Simulating an abrupt instance shutdown in a test environment and then inspecting sequence values shows that some cached values may never appear in table data, confirming the possibility of gaps.
Why Other Options Are Wrong:
Option B incorrectly claims that CACHE guarantees no gaps, when in fact it can introduce more on failure. Option C misrepresents CACHE and NOCACHE as privilege controls, which they are not. Option D claims that the options apply only to table storage, ignoring their true role in sequence configuration.
Common Pitfalls:
A common mistake is expecting sequences to produce perfectly consecutive values without gaps and then being surprised by gaps after rollbacks or restarts. Another is using NOCACHE unnecessarily on heavily used sequences, harming performance. Best practice is to accept that sequences are primarily uniqueness generators and to choose CACHE or NOCACHE based on performance needs and tolerance for gaps.
Final Answer:
CACHE reserves a set of sequence numbers in memory to improve performance at the cost of possible gaps if the instance fails, while NOCACHE generates each value without preallocation, reducing gaps from crashes but with more overhead.
Discussion & Comments