Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

An Oracle RAC system shows heavy 'enq: SQ - contention' and index block contention on the primary key of a hot orders table during peak inserts. How would you address it?

There are two related hot spots. 'enq: SQ - contention' means sessions are queuing to refresh the sequence cache, so we increase the sequence CACHE (for example to 1000), use NOORDER unless strict ordering is required, and raise Hibernate's allocationSize so the application calls NEXTVAL far less. The index contention comes from monotonic keys: every insert lands in the rightmost leaf block of the primary key index, which RAC nodes must pass back and forth, and we fix it with a reverse key index, a hash-partitioned index, or Oracle 18c+ scalable sequences, measuring each step with AWR or ASH.

Why this question is asked

Peak-time insert contention is one of the classic RAC problems, and it surprises teams because a single instance handled the same load fine. On RAC, two or more database servers share one database, and anything they all touch at once must be coordinated over the cluster interconnect. Interviewers ask this to see whether we can read wait events, understand both causes, and pick fixes knowing what each one costs. In this answer, we will recap what the two waits mean, look at why monotonic keys hurt RAC, go through the fixes for the sequence and for the index, and end with how to measure the result.

1A Quick Recap of the Two Waits

A wait event is Oracle's record of what a session was waiting for. 'enq: SQ - contention' is a wait on the SQ enqueue, the lock that protects a sequence while its cache is refreshed. Each instance keeps a range of sequence values in memory; when the range runs out, one session updates the sequence in the data dictionary to claim a new range while the others wait. With the default CACHE 20 and thousands of inserts per second, that refresh happens constantly.

The index side shows up as waits such as 'enq: TX - index contention' and global cache waits like 'gc buffer busy acquire' on the primary key index blocks. These mean sessions are fighting over the same few index blocks, often while those blocks travel between instances.

2Why Monotonic Keys Hurt on RAC

A primary key filled from a sequence is monotonic: each new value is larger than the last. In a B-tree index, larger keys always go to the rightmost leaf block. So every insert, from every session, wants to modify the same block. On a single instance that is a hot block in memory. On RAC, the block has to be shipped across the interconnect each time a session on another instance wants to change it, and when the block fills and splits, everyone waits for the split.

Step throughTwo RAC instances inserting increasing keys
Instance 1Rightmost index blockInstance 2
insert id 100001: needs the block in current mode
block shipped to instance 1
insert id 100002: same block
block shipped to instance 2, instance 1 waits
insert id 100003: same block again
block shipped back

6 messages in order. Play them, or step through them one at a time.

Every insert competes for one block, and each handover crosses the interconnect.

3Fixing the Sequence Side

The first step is cheap and safe. A larger cache means each instance refreshes its range rarely, and NOORDER lets each instance hand out its own range without asking the others. ORDER forces the instances to agree on every single value, which is very expensive on RAC, so it should only be used when the business truly needs strict time order across the cluster, which is rare for surrogate keys.

SQL
ALTER SEQUENCE orders_seq CACHE 1000 NOORDER;
-- and in the application, fewer NEXTVAL calls
@SequenceGenerator(name = "orders_gen", sequenceName = "orders_seq", allocationSize = 100)
-- the sequence must then be INCREMENT BY 100 to match allocationSize

On the application side, Hibernate's pooled optimizer already reduces NEXTVAL calls by a factor of allocationSize. Raising it from 1 or 50 to 100 or more cuts sequence traffic further, as long as the sequence's INCREMENT BY is changed to the same value in the same release. A larger cache and allocation size mean bigger gaps after restarts, which is harmless for surrogate keys.

4Fixing the Index Side

With the sequence calm, the rightmost block is still hot. There are three main options, and each moves cost somewhere else.

A reverse key index stores the bytes of each key reversed, so 100001 and 100002 land in different leaf blocks and inserts spread across the whole index. The catch is that range scans on the key no longer work, because neighbouring values are no longer neighbours in the index. That is usually fine for a surrogate id that is only looked up by equality, but not for anything queried with BETWEEN or ORDER BY.

A hash-partitioned global index splits the index into, say, 16 partitions by a hash of the key, so there are 16 rightmost blocks instead of one. Range scans still work within each partition, at the cost of probing all partitions. Note that this needs the Partitioning option, which is a separately licensed Enterprise Edition feature.

Scalable sequences, added in Oracle 18c, put a prefix made from the instance number and session into each value, so different instances and sessions insert into different parts of the index naturally. The values are much larger numbers and are not ordered across sessions; check your version for the exact format and options.

SQL
-- Option A: reverse key index
ALTER INDEX orders_pk REBUILD REVERSE;
-- Option B: hash-partitioned global index (Partitioning option required)
CREATE UNIQUE INDEX orders_pk_hash ON orders(id)
GLOBAL PARTITION BY HASH (id) PARTITIONS 16;
-- Option C: scalable sequence (Oracle 18c and later)
ALTER SEQUENCE orders_seq SCALE;
Options for the hot index block
OptionSpreads insertsCost
Larger CACHE and NOORDERSequence onlyBigger gaps, no global order
Larger allocationSizeSequence onlyMust match INCREMENT BY
Reverse key indexYes, across all blocksNo range scans on the key
Hash-partitioned indexYes, across N partitionsPartitioning licence, probes N partitions
Scalable sequence (18c+)Yes, by instance and sessionLarge, unordered values

5Measuring Before and After

None of these changes should be made on instinct. Before touching anything, we take an AWR report for a peak hour and look at the top wait events and the 'Segments by Global Cache Buffer Busy' section, which names the hot index. ASH (active session history) lets us see which SQL statements and which objects the waiting sessions were on. After each change, we take the same report for a comparable peak and compare.

One practical note: AWR and ASH are part of the Diagnostics Pack, which is licensed separately. If the company does not license it, Statspack gives a similar, free report. In a recent case the order was simple: first the sequence cache and allocationSize, because they are cheap and reversible; then measure; and only if index waits remain, choose between reverse key and hash partitioning based on whether anything range-scans the key.

6Follow-up questions

Why not just route all inserts for the table to one RAC instance?

That is a valid option called service affinity: a database service that runs on one instance handles the order inserts, so the hot block never travels. It removes the cross-instance cost but also caps that workload at one node's capacity and needs failover planning. It is often a good short-term fix while the key design is changed.

Would a UUID key solve it?

A random key spreads inserts across the whole index, so the right-hand hot block disappears. But it replaces it with random I/O, a larger index and worse cache use, and the 16-byte key makes every foreign key bigger. On Oracle I would prefer a reverse key or scalable sequence for the same effect with a numeric key.

Does a larger cache risk duplicates?

No. Each instance claims its range from the data dictionary under the SQ lock, so ranges never overlap. The only effect is that unused cached values are lost on shutdown or when aged out, creating gaps.

7Conclusion

In this answer, we saw that 'enq: SQ - contention' comes from sessions waiting to refresh a small sequence cache, made worse by ORDER on RAC, and that index contention comes from monotonic keys sending every insert to one rightmost block that the instances pass back and forth. We fix the sequence with a large CACHE, NOORDER and a larger Hibernate allocationSize matched to INCREMENT BY, and the index with a reverse key index, a hash-partitioned index or a scalable sequence, each with its own cost. We measure every step with AWR, ASH or Statspack.

What to say in the interviewAbout 90 seconds aloud

Open with

I see two problems that feed each other. The SQ enqueue wait means sessions are queuing to refresh the sequence cache, which on a hot table with the default cache of 20 happens constantly, and if the sequence is ORDER the instances must coordinate on every value.

Then, if they want more

So first I would alter the sequence to a large cache, say 1000, and NOORDER, since surrogate keys rarely need global ordering, and raise Hibernate's allocationSize, changing INCREMENT BY to match, so the application calls NEXTVAL far less.

The index contention is right-hand index contention: monotonic keys send every insert to the rightmost leaf block of the primary key, and on RAC that block is shipped between instances on every insert.

The options are a reverse key index, which spreads inserts but kills range scans on the key, a hash-partitioned global index, which needs the Partitioning licence, or a scalable sequence on 18c and later, which prefixes values by instance and session. Service affinity, routing those inserts to one instance, is a good stopgap.

I would take an AWR or ASH report at peak before changing anything, apply the cheap sequence changes first, measure again, and only then pick an index option based on whether anything range-scans the id. If Diagnostics Pack is not licensed, I would use Statspack.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "An Oracle RAC system shows heavy 'enq: SQ - contention' and index block contention on...": https://scrivio-interview-library.netlify.app/databases/oracle-and-sql-server/e1f1a75316/