Sub topic 11
Tuning Identity Columns: START WITH, INCREMENT BY, CACHE
This chapter assumes that you know basic CREATE TABLE and INSERT syntax, understand primary keys, and have access to Oracle Database 12c or later. An identity column is a column whose value Oracle generates from an internal sequence.
The identity options control the values that Oracle generates:
START WITHsets the first generated value.INCREMENT BYsets the difference between generated values.CACHEsets how many sequence values Oracle keeps ready in memory.
These options affect generated values and performance. They do not make generated keys gap-free.
Set identity options when creating a table
The options appear after the identity clause. This example starts employee numbers at 1000, increases them by 1, and keeps 20 values ready:
CREATE TABLE employees (
employee_id NUMBER GENERATED ALWAYS AS IDENTITY
(START WITH 1000 INCREMENT BY 1 CACHE 20),
email VARCHAR2(255) NOT NULL,
CONSTRAINT employees_pk PRIMARY KEY (employee_id),
CONSTRAINT employees_email_uk UNIQUE (email)
);
INSERT INTO employees (email) VALUES ('[email protected]');
INSERT INTO employees (email) VALUES ('[email protected]');
SELECT employee_id, email
FROM employees
ORDER BY employee_id;The generated IDs are normally 1000 and 1001.
START WITH applies to the first value generated for the identity sequence. It is useful when imported data already uses lower numbers or when different tables need distinct numeric ranges.
INCREMENT BY determines the next value. With an increment of 10, generated values might be 1000, 1010, and 1020:
CREATE TABLE invoice_numbers (
invoice_id NUMBER GENERATED ALWAYS AS IDENTITY
(START WITH 1000 INCREMENT BY 10),
created_on DATE DEFAULT SYSDATE NOT NULL,
CONSTRAINT invoice_numbers_pk PRIMARY KEY (invoice_id)
);Use an increment greater than one only when the gaps are intentional. It does not create a stronger key or improve uniqueness.
Understand CACHE and gaps
CACHE 20 tells Oracle to allocate 20 sequence values in advance. Caching reduces the work needed to generate values, which can help when many sessions insert rows.
Cached values can be lost when the database instance stops unexpectedly. Values can also be consumed by an insert that later rolls back. Consequently, identity values can have gaps even with INCREMENT BY 1 and CACHE 1.
A larger cache can improve throughput but may produce larger gaps after an outage. A small cache reduces the number of values held in memory, but it can increase sequence-allocation overhead. NOCACHE avoids preallocating a range, but it does not remove gaps caused by rollbacks or concurrent activity.
| Option | Controls | Main trade-off |
|---|---|---|
| `START WITH` | First generated value | Helps avoid collisions with existing values |
| `INCREMENT BY` | Difference between values | Values are intentionally spaced |
| `CACHE` | Values prepared in memory | Better performance, possible larger loss after failure |
See the generated sequence metadata
Oracle exposes identity information through USER_TAB_IDENTITY_COLS. The view shows the generation mode and the system-managed sequence name:
SELECT table_name,
column_name,
generation_type,
sequence_name
FROM user_tab_identity_cols
WHERE table_name IN ('EMPLOYEES', 'INVOICE_NUMBERS');The sequence name is useful for inspection, but do not treat the internal sequence as an application-owned object. Use the identity column in inserts and let Oracle manage its generator.
Exercise
You need generated IDs beginning at 5000, increasing by 1, with 50 values cached. Write the identity clause and state whether the IDs are guaranteed to have no gaps.
Show answer
Use GENERATED ALWAYS AS IDENTITY (START WITH 5000 INCREMENT BY 1 CACHE 50). The IDs are not guaranteed to have no gaps because rollbacks and instance failures can consume or discard values.