I’ve develop the following sequence on a ORACLE 10 DB wich autoincrement an id column of a table named t_client by 1 (A trigger is developed also and execute when new data on this table is registered)
The issue is that sometimes the sequecce doesn’t increment by 1, if not in 21. I doesn’t really find the issue by it’s increment by 21. Here is the code sample
Secuence ClientConsecutive
CREATE SEQUENCE user_owner.ClientConsecutive
MINVALUE 0
MAXVALUE 999999999999999999999999999
INCREMENT BY 1
START WITH 1
CACHE 20
NOORDER NOCYCLE ;
Trigger_Client_ID
create or replace
TRIGGER user_owner.TRIGGER_CLIENT_ID
BEFORE INSERT ON T_CLIENT
REFERENCING NEW AS NEW FOR EACH ROW
DECLARE valueSequence NUMBER := 0;
BEGIN
SELECT ClientConsecutive.NEXTVAL INTO valorSecuencia FROM DUAL;
:NEW.ID_CLIENTE := valueSequence;
END;
When the application associated to this objects executes it, on the SQLdeveloper looking for the sequence details sometimes it visualizes
LAST_NUMBER 2
which is correct when I register new data on the empty table but sometimes says
LAST_NUMBER 21
Any ideas?
Oracle sequences are not guaranteed to be gap-free. In fact, you can all but guarantee that there will occasionally be gaps.
In your case, you’re using the default cache size of 20. When the database is shut down or the sequence cache is aged out of memory, whatever values were part of the sequence cache will be lost. So if the cache holds the numbers 1-20 initially, you do a
nextvalthat returns a value 1, and the cache ages out of SGA before you callnextvalagain, you’d expect to get a value of 21 the next time you callnextval.You can reduce the frequency of gaps by reducing the
CACHEsize on the sequence to 1. That will not eliminate gaps– there will still, for example, be cases where a transaction gets rolled back, for example. And it will decrease performance. In general, you shouldn’t be concerned with gaps since sequence-generated primary keys are, by definition, merely supposed to be unique identifiers.